5 ms·
The idea that each type has its own control flow primitives is bothersome. It's taken over Rust: argv.nth(1) .ok_or("Please give at least one argum
by Animats 10y ago
The idea that each type has its own control flow primitives is bothersome. It's taken over Rust:
argv.nth(1)
.ok_or("Please give at least one argument".to_owned())
.and_then(|arg| arg.parse::<i32>().map_err(|err| err.to_string()))
.map(|n| 2 * n)
I'm waiting for
date.if_weekday(|arg| ...)
Reading this kind of thing is hard. All those subexpressions are nameless, and usually comment-less. This isn't pure functional programming, either; those expressions can have side effects.
- koenigdavidmj 10y agoMost of this should be obviated when the ? operator is ready. But until then, there is no primitive for 'work on the type you wrapped in Option, short-circuiting and returning None at the first sign of failure', so it has to be done in the library.
- Animats 10y agoIf this is an accepted idiom, people will be using it for years to come. Sometimes only to be cool. With "?" and "try!()", Rust is sort of emulating exceptions in a weird way.
- TeMPOraL 10y agoI started using a similar idiom in Java 8 recently, inspired by Optional class and some new functions in Map interface. It took me an hour or three, but at some point I noticed that in few places I was writing like this just because it's "cool", even though it was less readable (and actually made it harder to use one code-navigation IDE extension I liked). A lesson this strengthens in me again is that sometimes a cool-looking idea turns out to be pretty bad in practice, but you only figure it out after you go ahead with it. It isn't bad to try out things (that's how we learn), but you need to be extra honest with yourself about how the thing really feels when you're first using it, and never ignore that sense this new idea actually doesn't fit well and should be rejected.
- steveklabnik 10y ago> Rust is sort of emulating exceptions in a weird way. Except at the level Rust operates at, the code generation for exceptions vs. not is a thing. Not relying on landing pads, etc, is a thing people care about, and is why there's significant difference between return values vs exceptions can matter, even if at some higher semantic level, they're roughly equivalent.
- dan00 10y agoI like it, because you get somehow the best of both worlds, on one side you're getting explicit error handling and on the other the convenience of exceptions, that you can just "raise" them and they propagate upwards in the calling stack.
- grblovrflowerrr 10y agoOh god it's like ActionSupport came back with a vengeance.
- steveklabnik 10y agoRust does not let you globally add things to existing stuff, so it's a very different situation than ActiveSupport, even if it may look superficially so.
- grblovrflowerrr 10y agoOh god it's like ActionSupport came back with a vengeance.
- DasIch 10y agoRust basically offers a Monad-like API there. That's perfectly fine and a well established pattern. That has nothing to do with primitive control flow nor is that an indication of if_weekday appearing anytime soon. That being said having primitive control flow implemented as methods also has precedent with languages like Smalltalk or Self. That may be unusual but I don't think that's necessarily bad. I would be interested in reading about why this is bad design though.
- dfox 10y agodate.if_weekday looks like something one would find in Smalltalk class library, but (at least for GNU Smalltalk) only such construct I've found is this: http://www.gnu.org/software/smalltalk/manual-base/html_node/RegexResults_002dtesting.html#index-ifMatched_003a_0040_002d-3356 http://www.gnu.org/software/smalltalk/manual-base/html_node/...
- ryeguy 10y agoI don't agree here at all. The methods you show operate on an Optional, and it's incredibly common to perform those kinds of comparisons so it makes sense that they have convenience methods. This is not at all comparable to something like if_weekday. This has not "taken over" rust. Result is another type that does this, but this makes sense for the same reasons.
- MichaelGG 10y agoThe annoying part there is the repeated "|x| x.". Rust should have syntax to reference a method of an object, instead of having to write a wrapper. So it'd look like .map_err(???.to_string()).
- kmiroslav 10y agoGroovy and Kotlin use the implicit "it" parameter for lambdas that take just one parameter, which is very convenient: listOf(1, 2, 3, 4).filter { it % 2 == 0 }
- smilekzs 10y agoso does LiveScript
- eru 10y agoAnd Haskell uses operator sections: (==0) would be similar to { it == 0 }. Alas, the section syntax get a bit cumbersome when you want to compose two or more of them, like in this translation of your example: filter ((0==) . (%2)) [1, 2, 3, 4]
- SatvikBeri 10y agoScala allows underscores, and sequential underscores refer to the next element, so you can do e.g. list(1, 2, 3, 4).reduce(_ + _) == 10
- kmiroslav 10y agoDoesn't that make the parameter anonymous, though? Can you println that _ and see the value of the current element?
- SatvikBeri 10y agoYeah, that's the tradeoff–gain the ability to work with multiple parameters but lose the ability to reuse a single one.
- eru 10y agoIn Haskell, realizing that data flow and control flow are of the same spirit and that data structures are control structures is one of the key epiphanies to be had. This article mentions 'if' and 'Boolean'. Loops and lists are another example. (And for the same reason that most languages make such extensive use of loops, Haskell programs can often have a lot of lists.)
- kzrdude 10y agoI'd prefer to use `if let` and/or `try!()` instead of the methods here.