3 ms·
I like functional code, except that because the use of lambdas is awkward to "escape" them: nums.iter().map(|x| if x > 10 break(?)) This little thing is t
by mamcx 5y ago
I like functional code, except that because the use of lambdas is awkward to "escape" them:
nums.iter().map(|x| if x > 10 break(?))
This little thing is the major problem of functional chains (this is only a toy version of the issue). Because lambdas are inside the scope is hard to make them truly part of the current execution flow.
So, sometimes I change a chain of iterators to simpler loops for things like this. IF iterators where part of current scope, it will look far more natural, IMHO...
- HKH2 5y agonums.iter().take_while(|x| x <= 10)
- mamcx 5y agoYeah, I'm aware of the other options, just pointing that the use of lambdas break the flow.
- emi2k01 5y agoRust just standardized `ControlFlow` which would let you pass equivalents of `break` and `continue` as values. x.try_for_each(|y| {...; ControlFlow::Break} )
- mamcx 5y agoYeah, that is cool, and also show the problem :). Internal iterators and/or lambdas are at odds with imperative code. Some guy also yesterday ponder about this stuff: https://www.reddit.com/r/ProgrammingLanguages/comments/pno38l/what_would_a_principled_imperative_language_look/ https://www.reddit.com/r/ProgrammingLanguages/comments/pno38...