7.4 mischief/for: For Loops
(require mischief/for) | package: mischief-dev |
syntax
(for/hash! for-clauses . for-body)
syntax
(for*/hash! for-clauses . for-body)
syntax
(for/hasheq! for-clauses . for-body)
syntax
(for*/hasheq! for-clauses . for-body)
syntax
(for/hasheqv! for-clauses . for-body)
syntax
(for*/hasheqv! for-clauses . for-body)
> (define keys '(one two three four)) > (define vals '(1 2 3 4)) > (for/hash! {[k (in-list keys)] [v (in-list vals)]} (values k v)) '#hash((one . 1) (two . 2) (four . 4) (three . 3))
> (for/hasheq! {[k (in-list keys)] [v (in-list vals)]} (values k v)) '#hasheq((one . 1) (two . 2) (four . 4) (three . 3))
> (for/hasheqv! {[k (in-list keys)] [v (in-list vals)]} (values k v)) '#hasheqv((one . 1) (two . 2) (four . 4) (three . 3))
syntax
(for/dict init-expr for-clauses . for-body)
syntax
(for*/dict init-expr for-clauses . for-body)
syntax
(for/dict! init-expr for-clauses . for-body)
syntax
(for*/dict! init-expr for-clauses . for-body)
> (for/dict '([a one] [b two]) {[k (in-list '(b c))] [v (in-list '("B" "C"))]} (values k v)) '((a one) (b . "B") (c . "C"))
> (define vec (vector 0 0 0 0))
> (for/dict! vec {[i (in-range 4)]} (values i (* i i))) '#(0 1 4 9)
> vec '#(0 1 4 9)
syntax
(for/filter for-clauses . for-body)
syntax
(for*/filter for-clauses . for-body)
syntax
(for/filter-lists {list-id ...} for-clauses . for-body)
syntax
(for*/filter-lists {list-id ...} for-clauses . for-body)
> (for*/filter {[vowel (in-list '(a e i o u))] [word (in-list '([c a t] [b i r d] [m o u s e]))]} (memq vowel word)) '((a t) (e) (i r d) (o u s e) (u s e))
> (define (root x) (define y (sqrt x)) (and (exact-integer? y) y))
> (define (half x) (define y (/ x 2)) (and (exact-integer? y) y))
> (for/filter-lists {roots halves} {[i (in-range 10)]} (values (root i) (half i))) '(0 1 2 3)
'(0 1 2 3 4)
syntax
(for/append for-clauses . for-body)
syntax
(for*/append for-clauses . for-body)
syntax
(for/append-lists {list-id ...} for-clauses . for-body)
syntax
(for*/append-lists {list-id ...} for-clauses . for-body)
> (for/append {[str (in-list '("cat" "a" "log"))]} (string->list str)) '(#\c #\a #\t #\a #\l #\o #\g)
> (for/append-lists {evens odds} {[i (in-range 10)]} (partition even? (build-list i add1))) '(2 2 2 4 2 4 2 4 6 2 4 6 2 4 6 8 2 4 6 8)
'(1 1 1 3 1 3 1 3 5 1 3 5 1 3 5 7 1 3 5 7 1 3 5 7 9)
syntax
(for/partition for-clauses . for-body)
syntax
(for*/partition for-clauses . for-body)
syntax
(for/partition-lists {[yes-id no-id] ...} for-clauses . for-body)
syntax
(for*/partition-lists {[yes-id no-id] ...} for-clauses . for-body)
> (for/partition {[{i s} (in-dict (vector 'a 'e 'i 'o 'u))]} (values (even? i) s)) '(a i u)
'(e o)
> (for/partition-lists {[evens odds] [lefts rights]} {[{i s} (in-dict (vector 'a 'e 'i 'o 'u))]} (values (even? i) i s)) '(0 2 4)
'(a i u)
'(1 3)
'(e o)
syntax
(for/fold/lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(for*/fold/lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(for/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(for*/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(for/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(for*/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)
> (for/fold/lists {[len 0]} {syms} {[str (in-list '("one" "two" "three"))]} (values (+ len (string-length str)) (string->symbol str))) 11
'(one two three)
> (define table (hash 'a "apple" 'b "banana"))
> (for/fold/filter-lists {[longest 0]} {hits} {[key (in-list '(a b c))] #:when (dict-has-key? table key)} (define str (dict-ref table key)) (values (max longest (string-length str)) str)) 6
'("apple" "banana")
> (for/fold/append-lists {[count 0]} {chars} {[word (in-list '(cat a log))]} (values (add1 count) (string->list (symbol->string word)))) 3
'(#\c #\a #\t #\a #\l #\o #\g)
syntax
(define/for/fold {[x e] ...} for-clauses . for-body)
syntax
(define/for*/fold {[x e] ...} for-clauses . for-body)
syntax
(define/for/fold/lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(define/for*/fold/lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(define/for/lists {x ...} for-clauses . for-body)
syntax
(define/for*/lists {x ...} for-clauses . for-body)
syntax
(define/for/filter-lists {x ...} for-clauses . for-body)
syntax
(define/for*/filter-lists {x ...} for-clauses . for-body)
syntax
(define/for/append-lists {x ...} for-clauses . for-body)
syntax
(define/for*/append-lists {x ...} for-clauses . for-body)
syntax
(define/for/partition {x y} for-clauses . for-body)
syntax
(define/for*/partition {x y} for-clauses . for-body)
syntax
(define/for/partition-lists {[x y] ...} for-clauses . for-body)
syntax
(define/for*/partition-lists {[x y] ...} for-clauses . for-body)
syntax
(define/for/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(define/for*/fold/filter-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(define/for/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)
syntax
(define/for*/fold/append-lists {[x e] ...} {y ...} for-clauses . for-body)