Invalid number of arguments (0) passed: PersistentHashMap

I am very new to Clojure and I am trying to learn it by porting one of my codes, however, I am currently stuck with the following problem:

=> lineup {:c b4|b4|b3|b3, :sg b6|b11|b6|b6, :sf b7|b5|b5|b5, :pf b3|b1|b1|b1, :pg b10|b10|b11|b10} => (validate-lineup lineup) ArityException Wrong number of args (0) passed to: PersistentHashMap clojure.lang.AFn.throwArity (AFn.java:437) 

And here is the function:

 (defn validate-lineup [lineup] (map (fn [position] ((hash-map (position 0) (map (fn [s] (.substring s 1)) (str/split (position 1) #"\|")) )) ) lineup)) 

And I'm trying to create something like the following result:

 {:c {"4" "4" "3" "3"} :sg {"6" "11" "6" "6"} :sf {"7" "5" "5" "5"} ... } 

Thanks for the help, and if I don't write Lisp correctly, please teach me how well.

+4
source share
1 answer

Your main problem here:

 (fn [position] ((hash-map (position 0) (map (fn [s] (.substring s 1)) (str/split (position 1) #"\|"))))) 

You create a hash map and then call it as a function (note the double bracket in front of the hash-map ). You can remove it and get the following:

 (fn [position] (hash-map (position 0) (map (fn [s] (.substring s 1)) (str/split (position 1) #"\|")))) 

This validate-lineup method will return

 ({:c ("4" "4" "3" "3")} {:sg ("6" "11" "6" "6")} {:sf ("7" "5" "5" "5")} {:pf ("3" "1" "1"` "1")} {:pg ("10" "10" "11" "10")}) 

But you need a single card, not a sequence of cards. You can combine them:

 (apply merge (validate-lineup lineup)) 

Some tips:

Use subs instead of .substring : (fn [s] (subs s 1))
Use destructuring when you iterate over a map

 (fn [[key value]] (hash-map key (map (fn [s] (subs s 1)) (str/split value #"\|")))) 

There is a clear way to create a map from a vector of pairs using the into function. Thus, you can convert lineup to a vector of pairs, where the first element of the pair is the key and the second is a list of numbers:

 (defn validate-lineup [lineup] (into {} (map (fn [[key value]] [key (map #(subs s 1) (str/split value #"\|"))]) lineup))) 

You can divide b4|b4|b3|b3 into a sequence of re-seq numbers:

 user=> (re-seq #"\d+" "b1|b2|b3") ("1" "2" "3") 

And finally, you can replace the external map for macro:

 (defn validate-lineup [lineup] (into {} (for [[key value] lineup] [key (re-seq #"\d+" value)]))) 
+10
source

All Articles