12 for variants
| (require (planet untyped/unlib/for)) |
| (for/fold/reverse ([accum-id accum-expr] ...) ([sequence-id sequence-expr] ...) expr ...) |
Like for/fold, but calls reverse on each accum-id after iteration is complete.
Example: |
| > (for/fold/reverse | | ([even null] | | [odd null]) | | ([i (in-range 1 10)]) | | (if (even? i) | | (values (cons i even) odd) | | (values even (cons i odd)))) |
|
(2 4 6 8) |
(1 3 5 7 9) |
| (for/fold1 ([accum-id accum-expr] ...) ([sequence-id sequence-expr] ...) expr ...) |
Like for/fold, but returns only the value of the first accum-id.
Example: |
| > (for/fold1 | | ([even null] | | [odd null]) | | ([i (in-range 1 10)]) | | (if (even? i) | | (values (cons i even) odd) | | (values even (cons i odd)))) |
|
(8 6 4 2) |
| (for/fold1/reverse ([accum-id accum-expr] ...) ([sequence-id sequence-expr] ...) expr ...) |
Like for/fold1, but returns the reverse of the first accum-id.
Example: |
| > (for/fold1 | | ([even null] | | [odd null]) | | ([i (in-range 1 10)]) | | (if (even? i) | | (values (cons i even) odd) | | (values even (cons i odd)))) |
|
(8 6 4 2) |
| (for/filter ([sequence-id sequence-expr] ...) expr ...) |
Like for/list, but only accumulates non-#f return values.
Example: |
| > (for/filter ([i (in-range 1 10)]) | | (and (even? i) i)) |
|
(2 4 6 8) |
| (for/append ([sequence-id sequence-expr] ...) expr ...) |
Like for/list, but appends the results into a list.
Example: |
| > (for/append ([i (in-range 1 5)]) | | (list i (* i 2))) |
|
(1 2 2 4 3 6 4 8) |