On Jun 17, 12:44 am, Parth <parth.malwan...@gmail.com> wrote: > On Jun 17, 9:34 am, Wrexsoul <d2387...@bsnow.net> wrote: > > I'm shocked that this is missing from clojure.core: > > > (defn accum [f init coll] > > (loop [x init c coll] > > (if (empty? c) > > x > > (recur (f x (first c)) (rest c))))) > > > user=> (accum + 0 [1 2 3]) > > 6 > > user=> (accum + 0 [1 2 3 4 5]) > > 15 > > > This is one of the most basic, useful functions in functional > > programming. :) > > > Here's any triangular number: > > > (defn tri [n] (accum + 0 (take n (iterate inc 1)))) > > Maybe "apply" can be used > > user=> (apply + [1 2 3]) > 6 > user=> (apply + [1 2 3 4 5]) > 15
Yes, in the specific case of + it can, or other functions already defined to take arbitrary arguments and accumulate them. > or "reduce"? > user=> (reduce + 0 [1 2 3]) > 6 > user=> (reduce + 10 [1 2 3]) > 16 > user=> (reduce + 0 [1 2 3 4 5]) > 15 Well that's weird. I SPECIFICALLY did a search of the entire api docs to see if there was anything like "accum" already in there. I examined every occurrence of "seq" and turned up a blank. The docs definitely have problems if this can be missed despite a very thorough search. The only more-thorough search would have been to actually read the docs in their entirety, rather than to search them! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---