Partial is just an easier way to define an anonymous function that captures some arguments to the function and then passes the rest of the arguments to the created function.
In this case
user> (repeatedly 10 (partial rand-int 10)) (3 1 3 6 1 2 7 1 5 3)
equivalent to:
user> (repeatedly 10
Partial is wrong here, because partial used to correct in advance all the arguments (or rather, the only one) for rand-int.
a more interesting use of a partial illustration shows that a function is better:
(partial + 4)
gives the equivalent:
(fn [& unfixed-args] (apply + (concat [4] unfixed-args)))
(this does not literally do this) The idea is to build a function that accepts non-fixed arguments, combines them with fixed ones and calls the function you passed partial with enough arguments to work correctly.
user> ((fn [& unfixed-args] (apply + (concat [4] unfixed-args))) 5 6 7 8 9) 39 user> ((partial + 4) 5 6 7 8 9) 39
I use only partial in practice when the number of arguments is variable. Otherwise, I have a personal preference for using the anonymous form of reading functions #( ... )
Arthur ulfeldt
source share