I am new to Clojure. I am trying to understand why the second form does not work:
First form:
user=>(def nums(range 3)) (0 1 2) user=>(map #(list %1) nums) ((0) (1) (2))
Second form:
user=> (map #(list %1) (0 1 2)) java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)
The problem is the expression (0 1 2) , which is interpreted as 0 , applied to 1 and 2 ; which is impossible because 0 not a function.
(0 1 2)
0
1
2
(map #(list %1) '(0 1 2))
works as intended.
Because (0 1 2) means the call function 0 with arguments 1 and 2, but 0 is not a function. Therefore, you need to create a list, not an application, using the quote or list function ie '(0 1 2) OR (list 0 1 2)
'(0 1 2)
(list 0 1 2)
larsmans and Ankur have this. I understand this is a trivial example, but it would probably be more idiomatic to use a vector rather than a list:
(map #(list %1) [0 1 2])
You can also use % instead of %1 when only one argument is passed to an anonymous function.
%
%1
(map #(list %) [0 1 2])
user=> (map list (range 3)) ((0) (1) (2)) user=> (map list '(0 1 2)) ((0) (1) (2)) user=> (map list [0 1 2]) ((0) (1) (2))
Source: https://habr.com/ru/post/1385153/More articles:A paper clip does not retain attachment - databaseAm I declaring a partially specialized friends class? - very confused - c ++Parsing strings containing variable names in Delphi - variablesZip code subset (factor levels) - rvalue of text field for scriptlet - javascriptRspec tests under jruby on Windows run very slowly - windowsInvalid character warning in MVC razor view - asp.net-mvcRuby Datamapper left outer joins - ruby โโ| fooobar.comflot plotpan vs plotclick: how to ignore the click event when panning is enabled? - clickIs this a mistake for liftweb TimeHelpers: 60.minutes is not equal to 1.hour - scalaAll Articles