Arguments are implicit with the #(...) form, if you want explicit arguments you can use (fn [& e] (println (first e))). But, in that case, there is no reason for apply.
apply turns this: (apply str ["a" "b" "c"]) into this: (str "a" "b" "c") If you want to use all elements in a collection as individual args to one function call, that is what apply is for. If you want to call a function once for every item in a collection and get back a collection containing the results of all of those calls, that is what map is for. Sent from my iPhone On Jul 22, 2012, at 12:39 PM, larry google groups <[email protected]> wrote: > Hmm, okay, so if "apply" unpacks all the arguments and feeds them all to my > anonymous function, then this should work: > > (apply #([& everything] println first everything) @visitors) > > > but instead I get: > > Unable to resolve symbol: & in this context > [Thrown class java.lang.RuntimeException] > > > Please note, I ask this only out of intellectual curiosity. In my code I'll > always use "map" in this circumstance. But I want to understand what apply > really does, and I'm clearly missing something. > > > > > > > On Tuesday, July 17, 2012 3:13:04 PM UTC-4, Mark Rathwell wrote: > > your apply will end up doing sometihng like this: > > (#(println %1) "stu" "mary" "lawrence") > > > > since apply takes @visitors as a collection and passes each item as an > > argument to the function you give it. > > In other words, apply essentially unpacks the collection and passes > the items as individual arguments to the specified function. str > takes any number of arguments, so it works well with apply in your > case. Your anonymous function is a one-argument function, but apply > sends all items in your collection as arguments, and if your > collection contains more than one element then that will be the wrong > number of arguments. > > map is mapping the specified function to each element in the > collection, one at a time, and returning a collection of the results. > apply is calling the function once, but with all elements in the > collection as the arguments and returning the result of that one > function call. > > On Tue, Jul 17, 2012 at 2:35 PM, Jack Moffitt <[email protected]> wrote: > > > > > So far, everything is working as I thought. But I also thought that apply > > > let me run a function on everything in a sequence. Other than "str", I > > > could > > > not get this to work: > > > > > > user> (apply #(println %1) @visitors) > > > > I think you are looking for direct invocation: > > > > (#(println %1) @visitors) > > > > your apply will end up doing sometihng like this: > > (#(println %1) "stu" "mary" "lawrence") > > > > since apply takes @visitors as a collection and passes each item as an > > argument to the function you give it. > > > > jack. > > > > -- > > You received this message because you are subscribed to the Google > > Groups "Clojure" group. > > To post to this group, send email to [email protected] > > Note that posts from new members are moderated - please be patient with > > your first post. > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > > http://groups.google.com/group/clojure?hl=en > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to [email protected] > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
