On Jul 14, 11:47 am, slix <
notnorweg...@yahoo.se> wrote:
> What is the difference between apply and reduce? I am yet to find a
> scenario where I can't just them interchangeably.
quoting (doc apply):
"clojure/apply
([f args* argseq])
Applies fn f to the argument list formed by prepending args to
argseq."
(doc reduce):
"clojure/reduce
([f coll] [f val coll])
f should be a function of 2 arguments. If val is not supplied,
returns the result of applying f to the first 2 items in coll, then
applying f to that result and the 3rd item, etc. If coll contains no
items, f must accept no arguments as well, and reduce returns the
result of calling f with no arguments. If coll has only 1 item, it
is returned and f is not called. If val is supplied, returns the
result of applying f to val and the first item in coll, then
applying f to that result and the 2nd item, etc. If coll contains no
items, returns val and f is not called."
Apply unconditionally calls f *once*, accepting any number of
arguments. Reduce takes a function of two arguments and f is called
n-1 times, where n is the number of arguments in your list.
If you have zero or two arguments in your collection, you could use
them interchangably, though it's not a good idea for readability
concerns.
HTH
Allen