3 ms·
The idea that you can replace nearly every loop with functional constructs really clicked for me when I saw that you can use zip in Haskell and each_cons in Rub
by michaelfeathers 11y ago
The idea that you can replace nearly every loop with functional constructs really clicked for me when I saw that you can use zip in Haskell and each_cons in Ruby to see each element and its successor at the same time.
[10,11,2,3,4,5].each_cons(2).map {|curr,succ| succ - curr } => [1,-9,1,1,1]
- justinhj 11y agoThat's cool. I once made a Common Lisp macro that iterated and gave you the pred and succ of each list element, but it was a lot more work than this. Although I think implementing each_cons was most of the work.
- gd1 11y agoOr just: (-)':(10 11 2 3 4 5) ...in K. Or: (-) prior (10 11 2 3 4 5) ..in Q, if you feel more comfortable with words than symbols.