You are looking for:
(mapcat #(identity %) [[1] [2 3] [[4 5 6]]])
This shows the progression:
(ns tst.demo.core
(:use tupelo.core tupelo.test) )
(dotest
(let [data [[1]
[2 3]
[[4 5 6]]]]
(is= [1 2 3 [4 5 6]]
(mapcat (fn [x] x) data)
(mapcat (fn [x] (identity x)) data)
(mapcat #(identity %) data))))
When you use #(%)
you are essentially saying:
([2]) ; missing arg
Here is how to understand it
(dotest
(let [data [0 1 2 3 4]]
(is= 2 (get data 2))
(is= 2 (data 2))
(is= 2 ([0 1 2 3 4] 2))
(throws? ([0 1 2 3 4]))))
In clojure, seeing parantheses like (xxxxx)
means "function call". Just like in Java, seeing xxxxx()
means "function call". Parens in Clojure never mean grouping (I know, it is a hard habit to get out of!).
A demo:
(dotest
(let [the-answer-fn (fn [& args] 42)
fn-identity (fn [x] x)
fn-caller (fn [x] (x))]
(is= 42 (the-answer-fn))
(is= 3 (identity 3))
(is= 3 (fn-identity 3))
(is= 42 (fn-caller the-answer-fn))
(is (fn? #(the-answer-fn))) ; it always returns a function
(throws? (fn-caller 3))))
Note that the #(xxx)
reader macro is just a shorthand notation for
(fn [] (xxx))
So, it invokes it's body. As a memory aid, just pretend the #
isn't there. The remaining part shows what happens.
Also, remember that you are returning a new function that wraps the (xxx)
function call.