You're viewing version 1.2.0 of flatten. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

flatten

clojure.core

  • (flatten x)
Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat sequence.
(flatten nil) returns nil.

1 Example top

  • user=> (flatten [1 [2 3]])
    (1 2 3)
    
    user=> (flatten '(1 2 3))
    (1 2 3)
    
    user=> (flatten '(1 2 [3 (4 5)])) 
    (1 2 3 4 5)
    
    user=> (flatten nil)
    ()
    
    ; Attention with stuff which is not a sequence
    
    user=> (flatten 5)
    ()
    
    user=> (flatten {:name "Hubert" :age 23})
    ()
    
    ; Workaround for maps
    
    user=> (flatten (seq {:name "Hubert" :age 23}))
    (:name "Hubert" :age 23)
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5532 top

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

Comments top

No comments for flatten. Log in to add a comment.