(flatten non-sequential) has a surprising result

144 views
Skip to first unread message

J. Pablo Fernández

unread,
Jul 1, 2015, 7:55:28 AM7/1/15
to clo...@googlegroups.com
Hello Clojurists,

Today I was surprised by the result of (flatten 1) which is '(). I was expecting '(1) or an error. Talking in some other people in #clojure @ clojurians.net, not everybody agrees that '(1) is a good result but that '() is somewhat surprising. Would it be better if it raised an error when the attribute is not sequential?

Mikera

unread,
Jul 1, 2015, 11:22:36 AM7/1/15
to clo...@googlegroups.com
On Wednesday, 1 July 2015 12:55:28 UTC+1, J. Pablo Fernández wrote:
Hello Clojurists,

Today I was surprised by the result of (flatten 1) which is '(). I was expecting '(1) or an error. Talking in some other people in #clojure @ clojurians.net, not everybody agrees that '(1) is a good result but that '() is somewhat surprising. Would it be better if it raised an error when the attribute is not sequential?

From an array programming / core.matrix perspective '(1) would be the most logical result.

Consider the logical sequence:
[[[1]]] = 3 dimensional array with elements '(1) 
[[1]] = 2 dimensional array with elements '(1) 
[1] =1 dimensional array with elements '(1) 
1 = 0 dimensional array (or "scalar") with elements '(1) 

I am not saying that this is necessarily the best behaviour to follow for flatten, but it is an analogy worth considering.


J. Pablo Fernández

unread,
Jul 1, 2015, 12:53:55 PM7/1/15
to clo...@googlegroups.com

I agree with you Mikera, it also maintains the homogeneity of always returning a sequence but some people disagreed with it, so an error might be better.

--
J. Pablo Fernández <pup...@pupeno.com>
http://pupeno.com

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/L6yf6iFPqe8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

icamts

unread,
Jul 2, 2015, 2:54:14 AM7/2/15
to clo...@googlegroups.com
Hi Pablo,
I think you're right. Have a look at flatten source

(defn flatten
  "Takes any nested combination of sequential things (lists, vectors,
  etc.) and returns their contents as a single, flat sequence.
  (flatten nil) returns an empty sequence."
  {:added "1.2"
   :static true}
  [x]
  (filter (complement sequential?)
          (rest (tree-seq sequential? seq x))))

it is the rest function that causes this behavior and it seems to be just an optimization to avoid filtering the first element of tree-seq that is known to be the whole sequence. A simpler definition of flatten seems to have the behavior you expected.

(defn flatten1 [x] (filter (complement sequential?) (tree-seq sequential? seq x)))

J. Pablo Fernández

unread,
Jul 2, 2015, 3:29:50 AM7/2/15
to clo...@googlegroups.com
Yes, reading the source code and trying to understand why rest was being called there is how I came up with this case.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/L6yf6iFPqe8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
J. Pablo Fernández <pup...@pupeno.com> (http://pupeno.com)
Reply all
Reply to author
Forward
0 new messages