I’m very new to Clojure, and am trying to learn it by porting one of my code, however I’m currently stuck in 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’s 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 produce 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’m not writing in the correct “Lisp” way, please teach me how as well.
Your main problem is here:
You create hash-map and then call it as function (note double parenthesis before
hash-map). You can remove it and get this:This way
validate-lineupwill returnBut you need single map, not a sequence of maps. You can merge them:
Some advices:
Use
subsinstead of.substring:(fn [s] (subs s 1))Use destructuring when you iterate through map
There is a neat way to create map from vector of pairs using into function. So you can transform
lineupto vector of pairs where first item of pair is a key and second is a list of numbers:You can parse
b4|b4|b3|b3to sequence of numbers by re-seq:And finally you can replace outer
mapwith for macro: