On May 28, 2009, at 12:35 AM, Mikio Hokari wrote:
>
>>> I wonder if there is a more idiomatic way to compare two lazy
>>> sequences... lazily?
>
> You can just use =.
>
> (= seq1 seq2)
>
> It works lazily.
>
> user> (= (iterate inc 0) (map #(do (println %) %) [0 1 2 -3 4 5 6]) )
> 0
> 1
> 2
> -3
> false
Seems odd to me that = works with sequences but compare doesn't. Is
this a Java thing?
In other words, it seems odd that this works:
user> (defn get-comparators [str]
(cons (nth str 0)
(lazy-seq (do
(println "Looking at second element")
(Thread/sleep 1000)
(list (nth str 1))))))
user> (sort-by get-comparators = ["ab" "cd" "ef" "gh"])
("ab" "cd" "ef" "gh")
But this doesn't:
user> (sort-by get-comparators ["ab" "cd" "ef" "gh"])
java.lang.RuntimeException: java.lang.RuntimeException:
java.lang.ClassCastException: clojure.lang.Cons (NO_SOURCE_FILE:0)
[Thrown class clojure.lang.Compiler$CompilerException]
(Of course, if you use = instead of compare, it won't really sort the
list...)
—
Daniel Lyons
http://www.storytotell.org -- Tell It!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---