On Dec 15, 9:08 am, Randall R Schulz <[email protected]> wrote:
> > flatten returns a sequence - in this case a sequence containing
> > 'nil. How else would you flatten on nil?
>
> I would expect (flatten nil) => nil
I agree, it's a bug, but I'm not sure how to fix it, unless it's just
a special case that needs an "if". Here's flatten, as written by
Rich:
(defn flatten [x]
(let [s? #(instance? clojure.lang.Sequential %)]
(filter (complement s?) (tree-seq s? seq x))))
Which I think can now be simplified to:
(defn flatten2 [x] (filter (complement coll?) (tree-seq coll? seq x)))
Should maps be flattened in addition to "sequential things"?
But still (flatten2 nil) => (nil)
In general, you get weird results if the thing passed to flatten is
not a vector or a list:
(flatten "hello") => ("hello" \h \e \l \l \o)
(flatten2 "hello") => ("hello" \h \e \l \l \o)
-Stuart Sierra
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---