Le mercredi 4 mars 2015 22:08:13 UTC+1, Fluid Dynamics a écrit :
>
> 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 a b)]
> foo a b)
>
<snip/>
>
The are interesting examples.
For the use case above, I use the following instead of 'for' to avoid the
extra baggage that comes with it*:
(mapcat (fn [a b]
(when (criterion a b) [(foo a b)]))
s (next s))
But if the goal of the OP is to delete all consecutive duplicates in a
sequence then neither of these are very helpful. Instead, he could do:
user> (let [s [1 1 2 2 2 1 1 3 3 3 3 4 5]]
(map first (partition-by identity s)))
;; => (1 2 1 3 4 5)
* For those who wonder what goes under the hood when you use 'for', just
try this:
user> (macroexpand-1 '(for [[a b] (map vector s (next s))
:when (criterion a b)]
(foo a b)))
But beware that you might never see 'for' the same way after that :)
--
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.