• Re: Collection utilities

    From B. Pym@21:1/5 to Software Scavenger on Sat Sep 7 02:25:31 2024
    XPost: comp.lang.scheme

    Software Scavenger wrote:

    LOOP is powerful, general, and standard. E.g.

    CL-USER 5 > (loop as i below 5 collect i collect (* i 2))
    (0 0 1 2 2 4 3 6 4 8)

    Gauche Scheme

    (use srfi-42) ; append-ec

    (append-ec (: i 5) (list i (* i 2)))

    (0 0 1 2 2 4 3 6 4 8)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to All on Mon Jun 23 12:55:11 2025
    * (with-collectors (foo)
    (loop for pos upfrom 1
    for l in '((a b c) (one two three) (you and me) (girl))
    do (collect pos :into foo)
    do (loop for sym in l
    do (collect pos :into foo))
    finally (return foo)))
    (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

    ;;; Or....

    * (loop for pos upfrom 1
    for l in '((a b c) (one two three) (you and me) (girl))
    collect pos
    append l)
    (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

    Gauche Scheme

    (append-map
    cons
    (lrange 1) ;; Infinite but lazy.
    '((a b c) (one two three) (you and me) (girl)))

    ===>
    (1 a b c 2 one two three 3 you and me 4 girl)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Mon Jun 23 13:31:13 2025
    B. Pym wrote:

    * (with-collectors (foo)
    (loop for pos upfrom 1
    for l in '((a b c) (one two three) (you and me) (girl))
    do (collect pos :into foo)
    do (loop for sym in l
    do (collect pos :into foo))
    finally (return foo)))
    (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

    ;;; Or....

    * (loop for pos upfrom 1
    for l in '((a b c) (one two three) (you and me) (girl))
    collect pos
    append l)
    (1 A B C 2 ONE TWO THREE 3 YOU AND ME 4 GIRL)

    Gauche Scheme

    (append-map
    cons
    (lrange 1) ;; Infinite but lazy.
    '((a b c) (one two three) (you and me) (girl)))

    ===>
    (1 a b c 2 one two three 3 you and me 4 girl)

    Another way:

    (concatenate (map-with-index cons
    '((a b c) (one two three) (you and me) (girl))))

    ===>
    (0 a b c 1 one two three 2 you and me 3 girl)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Thu Jun 26 01:23:34 2025
    XPost: comp.lang.scheme

    B. Pym wrote:

    Software Scavenger wrote:

    LOOP is powerful, general, and standard. E.g.

    CL-USER 5 > (loop as i below 5 collect i collect (* i 2))
    (0 0 1 2 2 4 3 6 4 8)

    Gauche Scheme

    (use srfi-42) ; append-ec

    (append-ec (: i 5) (list i (* i 2)))

    (0 0 1 2 2 4 3 6 4 8)

    Shorter yet.

    (append-ec (: i 5) `(,i ,(* i 2)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)