You can try the following
(defn unique[s]
"Returns a lazy sequence with all consecutive duplicates removed"
(filter identity
(map #(when (not= %1 %2) %1)
s (cons nil s) )))
On Feb 18, 3:04 pm, Rowdy Rednose <[email protected]> wrote:
> The filter documentation reads:
>
> "Returns a lazy sequence of the items in coll for which (pred item)
> returns true. pred must be free of side-effects."
>
> So that means I should not write a function like this:
>
> (defn unique [sc]
> "Returns a lazy sequence with all consecutive duplicates removed"
> (let [last (atom (Object.))]
> (filter #(let [ok (not= @last %)] (reset! last %) ok) sc)))
>
> user=> (unique [1 2 3 3 4 5 5 6])
> (1 2 3 4 5 6)
>
> But in contrast to functions that can be retried (compare-and-swap
> etc.), I don't immediately see why having side effects in filter would
> be bad. Can anybody enlighten me? And how should I do this instead?
>
> Thanks
--
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