2
votes

Here is my code:

(def partial-join (partial (clojure.string/join ",")))

=>(clojure.string/join "," ["foo" "bar"])
"foo,bar"

=> (partial-join ["foo" "bar"])

And it raises this exception:

ClassCastException java.lang.String cannot be cast to clojure.lang.IFn .repl/eval12557 (form-init2162333644921704923.clj:1)

2
You are adding an extra layer of parenthesis that should not be there when using partial. - johnbakers

2 Answers

2
votes

See the doc of clojure.string/join.

clojure.string/join
([coll] [separator coll])
  Returns a string of all elements in coll, as returned by (seq coll),
   separated by an optional separator.

when only one argument is provided for clojure.string/join, this function regard its argument as collection, so:

user=> (clojure.string/join ",")
","

Next, see the doc of partial.

clojure.core/partial
([f] [f arg1] [f arg1 arg2] [f arg1 arg2 arg3] [f arg1 arg2 arg3 & more])
  Takes a function f and fewer than the normal arguments to f, and
  returns a fn that takes a variable number of additional args. When
  called, the returned function calls f with args + additional args.

When only one argument provided, partial returns its argument.

user=> (partial (clojure.string/join ",")) 
","

Try this:

user=> (def partial-join (partial clojure.string/join ","))
#'user/partial-join
user=> (partial-join ["a" "b"])
"a,b"
-1
votes

Wow. Stewart Holloway used this question as an example of how NOT to debug Clojure at Clojure Conj in Philly:

https://youtu.be/FihU5JxmnBg

I don't think it's a bad noob question (and the accepted answer was very helpful), but he really hated how it was phrased/framed.