Changeset 37764 in project


Ignore:
Timestamp:
07/05/19 12:18:23 (13 days ago)
Author:
juergen
Message:

list-comprehensions: for macro renamed collect

Location:
release/5/list-comprehensions
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • release/5/list-comprehensions/tags/1.2/list-comprehensions.egg

    r37752 r37764  
    55 (test-dependencies simple-tests)
    66 (author "[[/users/juergen-lorenz|Juergen Lorenz]]")
    7  (version "1.1")
     7 (version "1.2")
    88 (components (extension list-comprehensions))
    99)
  • release/5/list-comprehensions/tags/1.2/list-comprehensions.scm

    r37752 r37764  
    3333
    3434#|[
    35 This module implements some list creating routines, in particular a
    36 version of Clojure's equally named macro for, which is to replace
    37 list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0]
    38 of purely functional languages like Miranda.
     35This module implements some list creating routines, in particular
     36collect, a version of Clojure's macro for, which is to replace
     37list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0] of
     38purely functional languages like Miranda.
    3939]|#
    4040
    4141(module list-comprehensions
    42   (list-comprehensions for range repeat
     42  (list-comprehensions collect range repeat
    4343   iterate-while iterate-until iterate-times)
    4444
     
    109109(define iterate-times
    110110  (case-lambda
    111     ((fn times)
     111    ((fn k)
    112112     (lambda (start)
    113        (iterate-times fn times start)))
    114     ((fn times start)
    115      (let loop ((var start) (k 0) (result '()))
    116        (if (= k times)
     113       (iterate-times fn k start)))
     114    ((fn k start)
     115     (let loop ((var start) (n 0) (result '()))
     116       (if (= n k)
    117117         (reverse result)
    118          (loop (fn var) (+ k 1) (cons var result)))))
    119     ))
    120 
    121 ;;; (for item (var lst fltr ...) ....)
     118         (loop (fn var) (+ n 1) (cons var result)))))
     119    ))
     120
     121;;; (collect item (var lst fltr ...) ....)
    122122;;; ----------------------------------
    123 (define-syntax for
     123(define-syntax collect
    124124  (syntax-rules ()
    125125   ((_ item (var lst fltr ...))
     
    137137         (let ((var (car seq)))
    138138           (if (and fltr ...)
    139              (append (for item (var1 lst1 fltr1 ...) ...)
     139             (append (collect item (var1 lst1 fltr1 ...) ...)
    140140                     (recur (cdr seq)))
    141141             (recur (cdr seq)))))))
     
    167167    (iterate-times
    168168      procedure:
    169       (iterate-times fn times)
    170       (iterate-times fn times start)
     169      (iterate-times fn k)
     170      (iterate-times fn k start)
    171171      "returns a list by applying fn successively to start"
    172       "a number of times")
     172      "k times")
    173173    (iterate-while
    174174      procedure:
     
    183183      "returns a list by applying fn successively to start"
    184184      "until ok? succeeds")
    185     (for
     185    (collect
    186186      macro:
    187       (for item (var lst fltr ...) ....)
     187      (collect item (var lst fltr ...) ....)
    188188      "creates a new list by binding var to each element"
    189189      "of the list lst in sequence, and if it passes the checks,"
  • release/5/list-comprehensions/tags/1.2/tests/run.scm

    r37752 r37764  
    1212  (equal? (iterate-until sub1 zero? 5) '(5 4 3 2 1))
    1313  (equal? (iterate-while sub1 positive? 5) '(5 4 3 2 1))
    14   (equal? (for word (word '(the quick brown fox)))
     14  (equal? (collect word (word '(the quick brown fox)))
    1515          '(the quick brown fox))
    16   (equal? (for (add1 x) (x '(0 1 2 3))) ; map
     16  (equal? (collect (add1 x) (x '(0 1 2 3))) ; map
    1717          '(1 2 3 4))
    18   (equal? (for x (x '(0 1 2 3 4 5) (odd? x))) ; filter
     18  (equal? (collect x (x '(0 1 2 3 4 5) (odd? x))) ; filter
    1919          '(1 3 5))
    20   (equal? (for (* 10 n) (n '(0 1 2 3 4 5) (positive? n) (even? n)))
     20  (equal? (collect (* 10 n) (n '(0 1 2 3 4 5) (positive? n) (even? n)))
    2121          '(20 40))
    22   (equal? (for (list c k)
    23                (c '(A B C D E F G))
    24                (k '(1 2 3 4 5 6 7 8)))
     22  (equal? (collect (list c k)
     23                   (c '(A B C D E F G))
     24                   (k '(1 2 3 4 5 6 7 8)))
    2525          '((A 1) (A 2) (A 3) (A 4) (A 5) (A 6) (A 7) (A 8)
    2626            (B 1) (B 2) (B 3) (B 4) (B 5) (B 6) (B 7) (B 8)
     
    3030            (F 1) (F 2) (F 3) (F 4) (F 5) (F 6) (F 7) (F 8)
    3131            (G 1) (G 2) (G 3) (G 4) (G 5) (G 6) (G 7) (G 8)))
    32   (equal? (for (list c k)
    33                (c '(A B C D E F G)
    34                   (memv c '(A G)))
    35                (k '(1 2 3 4 5 6 7 8)
    36                   (memv k '(1 8))))
     32  (equal? (collect (list c k)
     33                   (c '(A B C D E F G)
     34                      (memv c '(A G)))
     35                   (k '(1 2 3 4 5 6 7 8)
     36                      (memv k '(1 8))))
    3737          '((A 1) (A 8) (G 1) (G 8)))
    38   (equal? (for (list c k)
    39                (c '(A B C D E F G))
    40                (k '(1 2 3 4 5 6 7 8)
    41                   (memv c '(A G))
    42                   (memv k '(1 8))))
     38  (equal? (collect (list c k)
     39                   (c '(A B C D E F G))
     40                   (k '(1 2 3 4 5 6 7 8)
     41                      (memv c '(A G))
     42                      (memv k '(1 8))))
    4343          '((A 1) (A 8) (G 1) (G 8)))
    44   (equal? (for (list x y z)
    45                (x '(A B))
    46                (y '(1 2))
    47                (z '(#f #t)))
     44  (equal? (collect (list x y z)
     45                   (x '(A B))
     46                   (y '(1 2))
     47                   (z '(#f #t)))
    4848          '((A 1 #f) (A 1 #t) (A 2 #f) (A 2 #t)
    4949            (B 1 #f) (B 1 #t) (B 2 #f) (B 2 #t)))
  • release/5/list-comprehensions/trunk/list-comprehensions.egg

    r37752 r37764  
    55 (test-dependencies simple-tests)
    66 (author "[[/users/juergen-lorenz|Juergen Lorenz]]")
    7  (version "1.1")
     7 (version "1.2")
    88 (components (extension list-comprehensions))
    99)
  • release/5/list-comprehensions/trunk/list-comprehensions.scm

    r37752 r37764  
    3333
    3434#|[
    35 This module implements some list creating routines, in particular a
    36 version of Clojure's equally named macro for, which is to replace
    37 list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0]
    38 of purely functional languages like Miranda.
     35This module implements some list creating routines, in particular
     36collect, a version of Clojure's macro for, which is to replace
     37list-comprehension syntax, e.g. [n^2 | n<-[1..10]; n mod 2 = 0] of
     38purely functional languages like Miranda.
    3939]|#
    4040
    4141(module list-comprehensions
    42   (list-comprehensions for range repeat
     42  (list-comprehensions collect range repeat
    4343   iterate-while iterate-until iterate-times)
    4444
     
    109109(define iterate-times
    110110  (case-lambda
    111     ((fn times)
     111    ((fn k)
    112112     (lambda (start)
    113        (iterate-times fn times start)))
    114     ((fn times start)
    115      (let loop ((var start) (k 0) (result '()))
    116        (if (= k times)
     113       (iterate-times fn k start)))
     114    ((fn k start)
     115     (let loop ((var start) (n 0) (result '()))
     116       (if (= n k)
    117117         (reverse result)
    118          (loop (fn var) (+ k 1) (cons var result)))))
    119     ))
    120 
    121 ;;; (for item (var lst fltr ...) ....)
     118         (loop (fn var) (+ n 1) (cons var result)))))
     119    ))
     120
     121;;; (collect item (var lst fltr ...) ....)
    122122;;; ----------------------------------
    123 (define-syntax for
     123(define-syntax collect
    124124  (syntax-rules ()
    125125   ((_ item (var lst fltr ...))
     
    137137         (let ((var (car seq)))
    138138           (if (and fltr ...)
    139              (append (for item (var1 lst1 fltr1 ...) ...)
     139             (append (collect item (var1 lst1 fltr1 ...) ...)
    140140                     (recur (cdr seq)))
    141141             (recur (cdr seq)))))))
     
    167167    (iterate-times
    168168      procedure:
    169       (iterate-times fn times)
    170       (iterate-times fn times start)
     169      (iterate-times fn k)
     170      (iterate-times fn k start)
    171171      "returns a list by applying fn successively to start"
    172       "a number of times")
     172      "k times")
    173173    (iterate-while
    174174      procedure:
     
    183183      "returns a list by applying fn successively to start"
    184184      "until ok? succeeds")
    185     (for
     185    (collect
    186186      macro:
    187       (for item (var lst fltr ...) ....)
     187      (collect item (var lst fltr ...) ....)
    188188      "creates a new list by binding var to each element"
    189189      "of the list lst in sequence, and if it passes the checks,"
  • release/5/list-comprehensions/trunk/tests/run.scm

    r37752 r37764  
    1212  (equal? (iterate-until sub1 zero? 5) '(5 4 3 2 1))
    1313  (equal? (iterate-while sub1 positive? 5) '(5 4 3 2 1))
    14   (equal? (for word (word '(the quick brown fox)))
     14  (equal? (collect word (word '(the quick brown fox)))
    1515          '(the quick brown fox))
    16   (equal? (for (add1 x) (x '(0 1 2 3))) ; map
     16  (equal? (collect (add1 x) (x '(0 1 2 3))) ; map
    1717          '(1 2 3 4))
    18   (equal? (for x (x '(0 1 2 3 4 5) (odd? x))) ; filter
     18  (equal? (collect x (x '(0 1 2 3 4 5) (odd? x))) ; filter
    1919          '(1 3 5))
    20   (equal? (for (* 10 n) (n '(0 1 2 3 4 5) (positive? n) (even? n)))
     20  (equal? (collect (* 10 n) (n '(0 1 2 3 4 5) (positive? n) (even? n)))
    2121          '(20 40))
    22   (equal? (for (list c k)
    23                (c '(A B C D E F G))
    24                (k '(1 2 3 4 5 6 7 8)))
     22  (equal? (collect (list c k)
     23                   (c '(A B C D E F G))
     24                   (k '(1 2 3 4 5 6 7 8)))
    2525          '((A 1) (A 2) (A 3) (A 4) (A 5) (A 6) (A 7) (A 8)
    2626            (B 1) (B 2) (B 3) (B 4) (B 5) (B 6) (B 7) (B 8)
     
    3030            (F 1) (F 2) (F 3) (F 4) (F 5) (F 6) (F 7) (F 8)
    3131            (G 1) (G 2) (G 3) (G 4) (G 5) (G 6) (G 7) (G 8)))
    32   (equal? (for (list c k)
    33                (c '(A B C D E F G)
    34                   (memv c '(A G)))
    35                (k '(1 2 3 4 5 6 7 8)
    36                   (memv k '(1 8))))
     32  (equal? (collect (list c k)
     33                   (c '(A B C D E F G)
     34                      (memv c '(A G)))
     35                   (k '(1 2 3 4 5 6 7 8)
     36                      (memv k '(1 8))))
    3737          '((A 1) (A 8) (G 1) (G 8)))
    38   (equal? (for (list c k)
    39                (c '(A B C D E F G))
    40                (k '(1 2 3 4 5 6 7 8)
    41                   (memv c '(A G))
    42                   (memv k '(1 8))))
     38  (equal? (collect (list c k)
     39                   (c '(A B C D E F G))
     40                   (k '(1 2 3 4 5 6 7 8)
     41                      (memv c '(A G))
     42                      (memv k '(1 8))))
    4343          '((A 1) (A 8) (G 1) (G 8)))
    44   (equal? (for (list x y z)
    45                (x '(A B))
    46                (y '(1 2))
    47                (z '(#f #t)))
     44  (equal? (collect (list x y z)
     45                   (x '(A B))
     46                   (y '(1 2))
     47                   (z '(#f #t)))
    4848          '((A 1 #f) (A 1 #t) (A 2 #f) (A 2 #t)
    4949            (B 1 #f) (B 1 #t) (B 2 #f) (B 2 #t)))
Note: See TracChangeset for help on using the changeset viewer.