News for hackers. Stuff that matters.
Re:Matt Godbolt sold me on Rust by showing me C++ by Ygg2 on Wednesday May 07, 2025 @03:39AM (#43913128)
> I like that Rust can pick good concepts and design coherent language from them without inventing its own "pragmatic" solution that breaks horribly in some use cases that some "practitioners" deem "too theoretical."

I've thought Rust picked some pretty nifty middle ground. On one side, it's not mindfucking unsafe like C. It picked to remove a set of problems like memory safety. On the other side, Rust didn't go for the highest of theoretical grounds. It's not guaranteeing much outside of it, and it also relies a bit on human help (unsafe blocks).

Re:Matt Godbolt sold me on Rust by showing me C++ by eptcyka on Wednesday May 07, 2025 @04:07AM (#43913258)
As per the article, Rust has benefits beyond the ones afforded by the borrow checker.
Re:Matt Godbolt sold me on Rust by showing me C++ by Ygg2 on Wednesday May 07, 2025 @05:33AM (#43913710)
Sure, but it is pragmatic in other ways as well :)

It takes ADT, but not function currying, and so on.

Re:Matt Godbolt sold me on Rust by showing me C++ by jeffparsons on Wednesday May 07, 2025 @05:44AM (#43913760)
I've occasionally wondered about the lack of currying in Rust; it feels like something that can be done mechanically at compile time, so why not support it? Perhaps to do it cleanly (without more magical privileged functions in core) would require variadic generics?
Re:Matt Godbolt sold me on Rust by showing me C++ by bunderbunder on Wednesday May 07, 2025 @11:35AM (#43917073)
You can absolutely curry functions in Rust. You just have to do it manually because there's no syntactic sugar for it.

I think that's a good thing. A curried function is a function that takes one argument, and returns a closure that captures the argument. That closure might itself take another argument, and return a new closure that captures both the original argument and the second one. And so on ad infinitum. How ownership and borrowing works across that chain of closures could easily become a touchy issue, so you probably want to be making it as explicit as possible.

Or perhaps better yet, find an easier way to accomplish the same task. Maybe use a struct to explicitly carry the arguments along until you're ready to call the function.

Re:Matt Godbolt sold me on Rust by showing me C++ by skybrian on Wednesday May 07, 2025 @01:11PM (#43918247)
This isn’t Rust-specific, but one reason a language designer might deliberately not implement currying is that it makes function call sites a bit harder to read. You need to already know how many arguments a function takes to make sense of the call’s return type.
Re:Matt Godbolt sold me on Rust by showing me C++ by int_19h on Wednesday May 07, 2025 @10:03PM (#43922424)
Currying is just syntactic sugar for a lambda that binds the first argument and passes the rest unchanged, and you can achieve the same level of conciseness in other ways - e.g. "pipeline" operators - but with a syntax that is more readable and more extensible. E.g. https://docs.rs/piping/latest/piping/
Re:Matt Godbolt sold me on Rust by showing me C++ by andrepd on Wednesday May 07, 2025 @06:02AM (#43913876)
I don't think currying is that big a deal, it's just syntactic sugar that might or might not make things easier to read, unlike ADTs or closures which are important core concepts.

I'd love to have a syntax like

    { foo(%1, bar) }
standing for

    |x| { foo(x, bar) }
though. I'm not aware of any language that has this!
Re:Matt Godbolt sold me on Rust by showing me C++ by masijo on Wednesday May 07, 2025 @08:10AM (#43914655)
Clojure has this

    user=> (#(println %1 %2) "Hello " "Clojure")
    Hello Clojure
Re:Matt Godbolt sold me on Rust by showing me C++ by Munksgaard on Wednesday May 07, 2025 @06:34AM (#43914073)
Elixir has this, which is close:

    &foo(&1, bar)
Re:Matt Godbolt sold me on Rust by showing me C++ by tcfhgj on Wednesday May 07, 2025 @07:17AM (#43914288)
Powershell has this, why do you like this?
Re:Matt Godbolt sold me on Rust by showing me C++ by tormeh on Wednesday May 07, 2025 @08:24AM (#43914762)
I'd argue that currying is actively harmful (I suspect you agree). I've seen functions take one argument in one source file, and the next argument in another source file. In JavaScript no less. Horrendous stuff. One of my most hated anti-features, along with Scala's implicits. These kinds of features are mostly misused rather than used.
Re:Matt Godbolt sold me on Rust by showing me C++ by bunderbunder on Wednesday May 07, 2025 @11:39AM (#43917127)
From what I've seen, partial application tends to be used for utmost good in dialects of ML, and utmost evil most everywhere else. Chaotic neutral in R/tidyverse.
Re:Matt Godbolt sold me on Rust by showing me C++ by tmoertel on Wednesday May 07, 2025 @08:30AM (#43914803)
Mathematica has this:

    In[1]:  Map((1 + #1)&, {a, b, c})
    Out[1]: {a + 1, b + 1, c + 1}
See https://reference.wolfram.com/language/ref/Function.html.en for the full story.
Re:Matt Godbolt sold me on Rust by showing me C++ by zelphirkalt on Wednesday May 07, 2025 @09:29PM (#43922222)
What is that & doing there? I thought it looks neat, but then that & ...
Re:Matt Godbolt sold me on Rust by showing me C++ by tmoertel on Friday May 09, 2025 @07:00PM (#43941722)
The & is a postfix operator that converts an expression into an anonymous function. See “Pure Functions”:

https://reference.wolfram.com/language/tutorial/FunctionalOp...

Re:Matt Godbolt sold me on Rust by showing me C++ by GrantMoyer on Wednesday May 07, 2025 @09:43AM (#43915598)
C++ has this[1], kind of, but please don't actually use it.

> for_each(a.begin(), a.end(), std::cout << _1 << ' ');

Also, Scala has something similar as a first class feature[2].

[1]: https://www.boost.org/doc/libs/1_88_0/doc/html/lambda.html

[2]: https://scala-lang.org/files/archive/spec/3.4/06-expressions...

Re:Matt Godbolt sold me on Rust by showing me C++ by icen on Wednesday May 07, 2025 @10:26AM (#43916123)
In K the arguments are named x y z by default, so you just write:

    { foo[x, bar] }
Re:Matt Godbolt sold me on Rust by showing me C++ by cobbal on Wednesday May 07, 2025 @04:27PM (#43920249)
swift has this as well

    { foo($0, bar) }
Re:Matt Godbolt sold me on Rust by showing me C++ by int_19h on Wednesday May 07, 2025 @10:10PM (#43922459)
As noted in other comments, a bunch of languages has it, but the problem with this syntax is that it doesn't generalize well. For example, let's say we have:

   foo(%1, bar(%1, 42)) 
does this desugar to:

   |x| foo(x, |y| bar(y, 42)) 
or to:

   |x| foo(x, bar(x, 42)) 
and do you trust the person reading this code later to remember which one it is?
Re:Matt Godbolt sold me on Rust by showing me C++ by _flux on Thursday May 08, 2025 @08:44AM (#43925563)
I think currying is an important concept and not sugar at all, as it means each function takes exactly one parameter, i.e. in OCaml foo bar baz is equal to (foo bar) baz. Of course, some other languages can try to emulate the use of currying and the original original idea no longer exists there.

I'm relatively sure there exists such a language you are looking for, but I've forgotten its name :(.

There was a syntax extension pa_holes for OCaml (which you perhaps know, as it is natively currying) that worked like

    (\ foo \1 bar)
and it would become

    fun x -> foo x bar
I'm sure the extension was inspired by some other language.. And the pa syntax extension extension mechanism for OCaml has been replaced by ppx a long time ago already; maybe the new system doesn't enable such succinct extensions.