3 ms·
I've looked at this before; I decided to implement it finally, calling it expand-right. (I use the "reduce" terminology rather than "fold", so the antonym has t
by kazinator 11y ago
I've looked at this before; I decided to implement it finally, calling it expand-right. (I use the "reduce" terminology rather than "fold", so the antonym has to be consistent).
With explicit lambdas, decrement from 5 to zero, not including it in list:
1> (expand-right (lambda (item) (if (zerop item) nil (cons item (pred item)))) 5)
(5 4 3 2 1)
Using functional combinators:
2> [expand-right [iff zerop false [callf cons identity pred]] 5]
(5 4 3 2 1)
Include zero; bit of a mouthful:
1> [expand-right [iff null nilf [callf cons identity [iff zerop nilf pred]]] 5]
(5 4 3 2 1 0)
Though it can do it, it doesn't look like a great interface when inclusion of the last element is required.
Comparison with the inclusive iterate (which I did implement and called "ginterate"--thanks for the nice name:
1> [ginterate [chain zerop not] pred 5]
(5 4 3 2 1 0)