Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get two elements from a sequence each time

Tags:

clojure

Does clojure have a powerful 'loop' like common lisp.

for example:

get two elements from a sequence each time

Common Lisp:

(loop for (a b) on '(1 2 3 4) by #'cddr collect (cons a b))

how to do this in Clojure?

like image 733
Kane Avatar asked Apr 05 '12 14:04

Kane


2 Answers

By leveraging for and some destructuring you can achieve your specific example:

(for [[a b] (partition 2 [1 2 3 4])](use-a-and-b a b))
like image 120
sw1nn Avatar answered Sep 18 '22 08:09

sw1nn


There is cl-loop, which is a LOOP workalike, and there are also clj-iter and clj-iterate, which are both based on the iterate looping construct for Common Lisp.

like image 25
Matthias Benkard Avatar answered Sep 21 '22 08:09

Matthias Benkard