On Monday 15 December 2008 07:22, Stuart Sierra wrote:
> On Dec 15, 9:08 am, Randall R Schulz <rsch...@sonic.net> 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".

If my current experience is any indicator (I'll leave it to you to 
evaluate the "if" part of that), there will be a proliferation of these 
tests at the point of calling of flatten if it's not done that one time 
within it.


> 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"?

My intuition is "no," but that's a pretty shallow intuition at this 
point.


> 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


Randall Schulz

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to