• Re: MAP (and variants) vs LOOP - Popular opinion observation?

    From B. Pym@21:1/5 to Nathan Baum on Thu Aug 22 06:35:48 2024
    Nathan Baum wrote:

    Then suppose you later need the loop/map to collect some of the values
    under certain conditions. You might have

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x)
    if (test x)
    collect (foo x))

    newLISP

    (let (result)
    (dolist (x '(2 9 22 25 42 49 58))
    (println (format "%d - %d" $idx x))
    (when (odd? x) (push (sqrt x) result -1)))
    result)

    0 - 2
    1 - 9
    2 - 22
    3 - 25
    4 - 42
    5 - 49
    6 - 58
    (3 5 7)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to All on Sun Sep 8 12:06:16 2024
    XPost: comp.lang.scheme

    Then suppose you later need the loop/map to collect some of the values
    under certain conditions. You might have

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x)
    if (test x)
    collect (foo x))

    Gauche Scheme

    (use srfi-13) ;; string-upcase

    (filter-map
    (lambda (x i) (print i " - " x)
    (and (string? x) (string-upcase x)))
    '(foo "an" 8 "why")
    (lrange 0))

    0 - foo
    1 - an
    2 - 8
    3 - why
    ("AN" "WHY")

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Sun Sep 8 12:44:44 2024
    XPost: comp.lang.scheme

    B. Pym wrote:

    Then suppose you later need the loop/map to collect some of the values under certain conditions. You might have

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x)
    if (test x)
    collect (foo x))

    Gauche Scheme

    (use srfi-13) ;; string-upcase

    (filter-map
    (lambda (x i) (print i " - " x)
    (and (string? x) (string-upcase x)))
    '(foo "an" 8 "why")
    (lrange 0))

    0 - foo
    1 - an
    2 - 8
    3 - why
    ("AN" "WHY")

    Suppose you have

    (loop for x in (get-list)
    do (format t "~A~%" x))

    and then it turns out you need to print a numeric index. You can do

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x))

    If you start with

    (mapc (lambda (x) (format t "~A~%" x)) (get-list))

    it seems (to me) that it'd be harder to modify it as needed,

    (let ((list (get-list)))
    (mapc (lambda (i x) (format t "~A - ~A" i x))
    (range 0 (length list))
    list))

    (I'm assuming the toolkit includes a RANGE utility, or something similar.)

    Gauche Scheme

    Shorter than the loop:

    (for-each
    (pa$ format #t "~a - ~a\n")
    (lrange 0)
    '(a b c))

    0 - a
    1 - b
    2 - c

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Nathan Baum on Wed Jun 18 20:50:07 2025
    Nathan Baum wrote:

    Then suppose you later need the loop/map to collect some of the values
    under certain conditions. You might have

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x)
    if (test x)
    collect (foo x))

    Gauche Scheme

    (filter-map
    (lambda (x i)
    (print i " - " x)
    (and (odd? x) (square x)))
    '(0 2 3 5 6 9)
    (liota))

    ===>
    0 - 0
    1 - 2
    2 - 3
    3 - 5
    4 - 6
    5 - 9
    (9 25 81)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Pascal Costanza on Thu Jun 26 22:00:34 2025
    Pascal Costanza wrote:

    - More often than not, the different variables actually iterate over different kinds of values. Recently, I needed the following costruct
    quite often:

    (loop for x in some-list
    for i from 0
    collect `(,x ,i))

    This enumerates all elements in a list. You would have to express this completely manually without LOOP because none of the mapxyz functions
    help you here.

    Wrong.

    Gauche Scheme:

    (map list some-list (lrange 0))

    Another way:

    (use gauche.sequence :only (map-with-index))
    (use srfi-1 :only (xcons))

    (map-with-index xcons '(20 30 40 50))

    ((20 . 0) (30 . 1) (40 . 2) (50 . 3))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to Nathan Baum on Thu Jun 26 22:40:12 2025
    Nathan Baum wrote:

    Suppose you have

    (loop for x in (get-list)
    do (format t "~A~%" x))

    and then it turns out you need to print a numeric index. You can do

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x))

    If you start with

    (mapc (lambda (x) (format t "~A~%" x)) (get-list))

    it seems (to me) that it'd be harder to modify it as needed,

    (let ((list (get-list)))
    (mapc (lambda (i x) (format t "~A - ~A" i x))
    (range 0 (length list))
    list))

    (I'm assuming the toolkit includes a RANGE utility, or something similar.)

    Gauche Scheme

    Shorter than the loop:

    (for-each
    (cut print <> " - " <>)
    (lrange 0)
    '(a b c))

    0 - a
    1 - b
    2 - c


    (for-each
    (cut print <> " - " <> " - " <>)
    (lrange 0)
    '(a b c)
    '(! ? @))

    0 - a - !
    1 - b - ?
    2 - c - @



    (use srfi-42) ; do-ec

    (do-ec (:list x (index i) '(a b c)) (print i " - " x))

    0 - a
    1 - b
    2 - c

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Thu Jun 26 22:53:35 2025
    B. Pym wrote:

    Nathan Baum wrote:

    Suppose you have

    (loop for x in (get-list)
    do (format t "~A~%" x))

    and then it turns out you need to print a numeric index. You can do

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x))

    If you start with

    (mapc (lambda (x) (format t "~A~%" x)) (get-list))

    it seems (to me) that it'd be harder to modify it as needed,

    (let ((list (get-list)))
    (mapc (lambda (i x) (format t "~A - ~A" i x))
    (range 0 (length list))
    list))

    (I'm assuming the toolkit includes a RANGE utility, or something similar.)

    Gauche Scheme

    Shorter than the loop:

    (for-each
    (cut print <> " - " <>)
    (lrange 0)
    '(a b c))

    0 - a
    1 - b
    2 - c


    (for-each
    (cut print <> " - " <> " - " <>)
    (lrange 0)
    '(a b c)
    '(! ? @))

    0 - a - !
    1 - b - ?
    2 - c - @



    (use srfi-42) ; do-ec

    (do-ec (:list x (index i) '(a b c)) (print i " - " x))

    0 - a
    1 - b
    2 - c

    Nathan Baum wrote:

    Then suppose you later need the loop/map to collect some of the values
    under certain conditions. You might have

    (loop for x in (get-list)
    for i from 0
    do (format t "~A - ~A~%" i x)
    if (test x)
    collect (foo x))

    Gauche Scheme

    (use srfi-42)

    (list-ec (:list x (index i) '(0 -2 3 4 -7 9))
    (begin (print i " : " x))
    (if (negative? x))
    (abs x))

    0 : 0
    1 : -2
    2 : 3
    3 : 4
    4 : -7
    5 : 9
    (2 7)

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