listOf(1, 2, 3, 4).filter { it % 2 == 0 } list(1, 2, 3, 4).reduce(_ + _) == 10 list(1, 2, 3, 4).reduce{print(_) + _} == 10
Use it if you or your language hasn't defined such a function: list(1, 2, 3, 4).reduce{it:= _; println(it); it + _} == 10
In fact, any name for it will do. filter ((0==) . (%2)) [1, 2, 3, 4] .map_err(ToString::to_string)
as well. Works just fine with methods. .map_err(to_string)
When using the lambda the type is inferred, so why
should there be a need for the ToString?Functional style is hampered by excessive verbosity. (Non functional style is so verbose a bit of extra noise doesn't hurt _as much_.) Rust could use a lot more inference, custom operators[1], and so on. They seem to sort of agree, with auto-deref, auto-borrow, some type inference, but won't go all the way. I suppose being conservative can be defended -- can't go back without breaking code. Hopefully, in the future, the verbosity will annoy more people and there will be enough support to head in a more Haskell/ML direction. But they seem very opposed to it at the moment.
1: The rationale apparently being "someone might abuse it!" instead of "it makes good libraries even better". Parser combinators, UI toolkit code do great with custom operators. Require a method name (i.e. operator !!= as foo) if it's too great a concern. Can't save yourself from bad writers. Crippling yourself to avoid this seems like a poor tradeoff.
If there's only one to_string function or method, the compiler could just take this one.
Edit: Also, auto-borrow does not seem to work with this syntax.
This is a common enough pattern that reducing the visual noise will increase clarity.
Why add a feature for this - worse syntax, if you can just use an anonymous function?
Rust is already not the simplest of languages, adding further syntax and features of questionable benefit won't make the language any simpler or easier to understand.