On Friday, October 24, 2014 4:19:52 PM UTC-4, Fluid Dynamics wrote:
>
> (= s (distinct s)) looks to me like it should return true if it lacks
> duplicates, false if it has duplicates, and, due to how laziness and =
> work, short circuit as soon as any duplicate item is found.
>
> Your way sounds closer to how this version works:
>
> (defn dupes? [c]
> (->> c
> (frequencies)
> (vals)
> (apply max)
> (< 1)))
>
> which works (truthy when there are duplicates this time) but is less
> efficient. A low level method would be
>
> (loop [s (seq s) seen? #{}]
> (if s
> (let [f (first s)]
> (when-not (seen? f)
> (recur (next s) (conj seen? f))))
> true))
>
> That one also returns true on duplicates, and returns nil if there are
> none, and short circuits as soon as it sees a duplicate.
>
And just for shits and giggles, here's a point-free version of the second
one:
(def dupes?
(reduce comp
(reverse
[frequencies
vals
(partial apply max)
(partial < 1)])))
Look, ma! No parameters!
Of course this is the least efficient version of all, more useful as an
exercise in stretching one's FP thinking abilities than as a practical way
to scan collections for the presence of duplicates. If I needed to do that
in a performance-critical loop I'd use the (loop ...) version, and
otherwise the pithy (= s (distinct s)). :)
--
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.