clojure partial clarification

clojure, functional-programming, jvm, read-eval-print-loop

Solution

`partial` does not actually check which arities its first argument supports; an arguably more accurate docstring would say that it "takes a function f and some arguments to f". (Clearly if you supply too many arguments, the resulting partially applied function will be broken, though that will only be observable when you try to call it.) So that's why `(partial rand-int 10)` is ok even though the number of arguments to `rand-int` supplied is not "fewer than normal".

The reason why either `partial` or something like `#(rand-int 10)` is needed here is that `repeatedly` expects its final argument to be a function which it can repeatedly call, whereas `(rand-int 10)` would be a number.

Compare this to `repeat` which returns a sequence with the supplied item repeated the specified number of times (or infinitely many times in the unary case). Here `(rand-int 10)` would be an appropriate second argument, but of course it would be some particular number, so the result would look like `(8 8 8 8 8 ...)`; `repeatedly` will make a separate call to `(partial rand-int 10)` for each item of the sequence returned, so you'll get from it a sequence of (likely different, independent) random numbers.

Problem

I'm reading a book on clojure, and I came by an example that I dont fully understand.. Here is the code in repl: ``` user=> (repeatedly 10 (rand-int 10)) ClassCastException java.lang.Integer cannot be cast to clojure.lang.IFn clojure.core/repeatedly/fn--4705 (core.clj:4642) user=> (repeatedly 10 (partial rand-int 10)) (5 0 5 5 2 4 8 8 0 0) ``` My question is: why is the `partial` needed here, and how does that fit into `partial` definition, and `repeatedly` definition & syntax. Partial ... ``` 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. ``` So how does this fit in?

Original source