I feel like there should be a simpler way to do this, but I haven't found
one despite much Googling and reading of docs about "for" and its
siblings.  Can someone point me the right way?


I'd like to be able to iterate over a list N elements at a time -- e.g.,
grab elements 1 and 2, do something with them, grab 3 and 4, etc.

This will do the job, but I suspect there's a more built-in way:

(define data '(("foo") ("bar")  ("baz")  ("jaz") ("quux") ("glug")))

(define (step-by-n func data [num 2])
  (if (null? data)
      (func '())
      (append (func (take data num))
              (step-by-n func (drop data num) num))))

(step-by-n (lambda (l)
             (if (null? l)
                 '()
                 (list (hash (car (first l))
                             (car (second l))))))
           data)

(step-by-n (lambda (l)
             (if (null? l)
                 '()
                 (list (hash (car (first l))
                             (list (car (second l))
                                   (car (third l)))))))
           data
           3)

OUTPUT:
'(#hash(("foo" . "bar")) #hash(("baz" . "jaz")) #hash(("quux" . "glug")))
'(#hash(("foo" . ("bar" "baz"))) #hash(("jaz" . ("quux" "glug"))))


(Note that the fact that it returns the results as a list was a deliberate
choice but not an essential one.)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to