For examining adjacent items in a sequence, there are a few functional
(i.e., no mutable state) approaches.
When the output is a sequence with an element for each adjacent pair:
(map (fn [a b] ...) s (next s))
When the output is a sequence with an element for each adjacent pair that
meets some criterion:
(remove #{::removeme}
(map
(fn [a b]
(if (criterion a b)
(foo a b)
::removeme))
s (next s)))
OR
(for [[a b] (map vector s (next s))
:when criterion]
foo a b)
When the output is a single accumulated value:
(reduce foo (map vector s (next s)))
OR
(reduce foo (partition 2 1 s))
When the output is an accumulated value, with short-circuit when some pair
meets some criterion:
(reduce foo (take-while not-criterion (map vector s (next s))))
(criterion in that case takes a single parameter to destructure into two,
e.g. (fn [[a b]] ...) rather than (fn [a b] ...).)
When the output is a seq that should short-circuit when some pair meets
some criterion:
(map foo
(take-while not-criterion
(map vector s (next s)))
OR
(for [[a b] (map vector s (next s))
:while not-criterion]
(foo a b))
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.