JanetDocsPlaygroundI'm feeling luckyGitHub sign in

flatten



    function
    boot.janet on line 1642, column 1

    (flatten xs)

    Takes a nested array (tree) `xs` and returns the depth first 
    traversal of it. Returns a new array.


3 examplesSign in to add an example
Loading...
# will recursively flatten all indexed-like values
(flatten [1 @[2 [3]] [@[4] 5]])
# => @[1 2 3 4 5]

# only applies to indexed values, other values are untouched
(flatten ["a" :b [:c :d] :e "f"])

# be careful: dictionaries are considered indexed since they define next
(flatten {:a :b})

# however, this only applies to the top value, otherwise it remains untouched
(flatten [{:a :b} {:c :d}])

# if you want only "one cycle of flatten", you want reduce
(reduce array/concat @[] (pairs {:a [:b :c] :d [:e [:f]]}))
# => @[:d (:e (:f)) :a (:b :c)]
CosmicToastPlayground
(def kvpairs [[:x 1] [:y 2]])

(table ;(flatten kvpairs)) # => @{:x 1 :y 2}
yumaikasPlayground
(flatten [1 [2 3 [4]] 5])  # => @[1 2 3 4 5]
cellularmitosisPlayground