On Friday, October 24, 2014 4:28:54 PM UTC-4, Fluid Dynamics wrote:
>
> 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.
>>
>
Correction: like (= s (distinct s)) the loop is falsey on duplicates.
There is also a difference in the handling of the corner case of an empty
collection. The loop and (= s (distinct s)) return true (no duplicates),
which seems sensible. Both frequencies-using versions blow up, but you can
put a (cons (Object.)) or a (cons ::sentinel) or such at the start of the
transformation sequence to fix them.
--
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.