On Mon, Dec 29, 2008 at 10:49 AM, Emeka <emekami...@gmail.com> wrote: > Hello sir, > > I would have asked this question in the thread but , I don't want to create > noise over this issue. > I have not been able to get my head around your code or Clojure. I need some > support. > > > (defn top-words-core [s] > (reduce #(assoc %1 %2 (inc (%1 %2 0))) {} > (re-seq #"\w+" > (.toLowerCase s)))) > > My little understanding of Inc is that it returns a number greater than the > arg. That's clear to me, however, which argument is passed here (%1 %2 0)
In this call to reduce, we use a Map to "reduce into" and that map is identified by %1 in the assoc closure. The Seq that reduce over contains the words that we want to count and is produced by re-seq. The individual words are identified by %2 in the closure. Maps are _functions_ of their keys, so that means that (%1 %2 0) says "get me the value from the map by the key %2 (the word) or 0 if no such value exists". So the argument to inc is either the current count from the map that we want to increase, or the number zero. Inc will then increase this value by one and then we assoc this new value with the word in question, in the map. > Please try as much as you can to simplify your explanation because this may > assist me in making a great leap in the learning of of Clojure. Again, > #(%)[3 4] works because of closure, so when you apply reduce function of > #(.........) does it suspend #(........) closure capability. > > Emeka > > > -- Venlig hilsen / Kind regards, Christian Vest Hansen. --~--~---------~--~----~------------~-------~--~----~ 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 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 -~----------~----~----~----~------~----~------~--~---