3 ms·
Can someone please explain where an anonymous function has an advantage over a named function? (Not trolling: I'm really trying to figure it out.)
by RyanMcGreal 17y ago
Can someone please explain where an anonymous function has an advantage over a named function? (Not trolling: I'm really trying to figure it out.)
- alrex021 17y agoIt isn't really about the advantages of anonymous functions over named functions but rather closures (first-class functions) over anonymous functions. Here is a good explanation: http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda http://stackoverflow.com/questions/220658/what-is-the-differ...
- joubert 17y agoOne example is when you want to pass a function as param to another function. JavaScript example: Forced to use named function: var greaterThanFunction = function(a, b) { return a > b; }; var filteredResults = filter(biglist, greatherThanFunction); VS: Anonymous: var filteredResults = filter(biglist, function(a, b) { return a > b; }); The JavaScript example above is nice, but in Clojure anonymous lambdas are even nicer: You can do things like: (reduce #(+ %1 %2) (range 1000)) i.e., not only do you not have to name the function, you don't even have to name the variables. ---- Excerpt from http://clojure.org/reader http://clojure.org/reader Anonymous function literal (#()) #(...) => (fn [args] (...)) where args are determined by the presence of argument literals taking the form %, %n or %&. % is a synonym for %1, %n designates the nth arg (1-based), and %& designates a rest arg. This is not a replacement for fn - idiomatic used would be for very short one-off mapping/filter fns and the like. #() forms cannot be nested.
- olavk 17y agoIn C# you can do thing like users.Where(u=>u.Score>1000).OrderBy(u=>u.Name); That would be pretty verbose if you had to declare each function seperately.
- latortuga 17y agoOne of the biggest is that anonymous functions typically are less verbose in terms of syntax required. This is really dependent upon the language you're working with but two pretty decent examples from languages I work with are Ruby and javascript. In javascript, an anonymous function is just a normal function with no name: function(){ return "i'm anonymous"; } function namer(){ return "i'm a named function"; } In Ruby, blocks are a syntactic sugar for anonymous functions: [1,2,3,4].each {|num| print i } # => 1234 [1,2,3,4].each do |num| print i; end # => 1234 In javascript a named function is very similar to an anonymous one - they can both be bound to variables, both be passed to other functions, both be returned from functions. I believe the biggest difference you'll find is that some languages enable you to write them with very little trouble (e.g. the ruby given above, simply brackets with an arg list) while some require more extensive declaration. Another good contrast of this is javascript, which doesn't even require that you give a function a formal parameter list, with (hypothetically) Java which will need both a return type and the types of each argument. Also that stackoverflow link is pretty good :)
- Hexstream 17y agoIf you had to come up with a unique name for every "one-shot" function (which you need a lot in a functional language), you'd have to expend brain-cycles every time to either decide on a representative name (which can be long and/or difficult to come up with if that function does something pretty idiosynchratic, which most one-shot functions do) or use an ugly, meaningless naming scheme like "myfunction1, myfunction2, myfunction3".