Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

Yes. Playing around with Metalua[1]'s anonymous functions[2] which allow you to shorten

  function(arg1, arg2, argn) return some_expr end
to the much more concise

  |arg1,arg2,argn| some_exp
makes me wish they were part of core Lua.

1- http://lua-users.org/wiki/MetaLua

2- http://metalua.luaforge.net/manual003.html#toc3



sort by: page size:

Could you elaborate? Coming from Lua I always found the nesting of anonymous functions messy and wondered why they weren't done as in the article.

There is one in Anarki: http://github.com/nex3/arc/tree/master/help/

Usage examples:

  arc> help.acons
  [fn]  (acons x)
   Determines if `x' is a `cons' cell or list.
      Unlike 'alist, this function will return nil if given an empty list
      See also [[atom]] [[alist]] [[dotted]] [[isa]] [[cons]] [[list]] 
  nil
  arc> help.alist
  [fn]  (alist x)
   Return true if argument is a possibly empty list
      Unlike 'acons, this function returns t when given an empty list
      See also [[atom]] [[acons]] [[dotted]] [[isa]] [[cons]] [[list]] 
  nil
  arc> help.afn
  [mac] (afn parms . body)
   Creates a function which calls itself with the name `self'.
      See also [[fn]] [[rfn]] [[aif]] [[awhen]] [[aand]] 
  nil

And Clojure has their way of handling unnamed variables in anonymous functions.

;; Single argument

(fn [x] (* 2 x))

#(* 2 %)

;; Multiple arguments

(fn [x y] (+ x y))

#(+ %1 %2)

;; Remaining arguments

(fn [x & args] (reduce + x args))

#(reduce + % %&)

Example copied from this nice page - https://silviapan.com/blog/anonymous-functions-clojure/


Named arguments exist, https://hackage.haskell.org/package/named-0.3.0.1/docs/Named... That's just one example.

But yes. Optional arguments are a big problem at scale. And laziness is the worst antifeature I could imagine.

But. You can disable laziness and get great performance. You just need to be careful when using libraries and pick ones that have the right strictness.

A strict Haskell would be a killer language.


The single argument case works fine for Lua but maybe the no argument case woudn't. Having to supply only the empty argument list seems counterintuitive, though.

Variable sigils would work but that would be even more annoying.


I've never used that particular feature, but Livescript lets you use default arguments on curried functions by calling with no arguments.

http://livescript.net/#functions


Yes the syntax to pipe the first argument to an anonymous function for another function and use it as second+ argument was akward and hard to remember.

Like:

"hacker news" |> (&Map.put(:site, "ycombinator", &1)).()


> (As a sidenote, 9 parameters for a function call can be a bit annoying in a language like Go, that doesn’t support default arguments and method overloading)

This trick[0][1] turns out to be very useful.

[0] talk: https://www.youtube.com/watch?v=24lFtGHWxAQ&index=15&list=PL...

[1] slides (with transcript): http://dave.cheney.net/2014/10/17/functional-options-for-fri...


Nop, but it can be a bit better by using predefined functions rather than inline ones.

It's really not a big deal. Just a bit cumbersome.

Lua has syntactic sugar where table:method(arg1, arg2, ...) is exactly equivalent to table.method(table, arg1, arg2, ...). All it really does is establish a convention and eliminate "self" from argument listings in methods, but it's still handy - its presence is an immediate sign of OO-style code.


Yeah unless I’m missing something it’s quite niche.

I was envisioning something like:

listify_args(*args, reverse=True)


Hm, I've used defs and bindings together inside macros to reduce redundant args like this function here: https://github.com/shriphani/clj-lmdb/blob/master/src/clj_lm...

I think brevity is far more important than being explicit all the time. When we speak, we use a lot of context to infer what is said - no reason code shouldn't look like that.


I also dislike not being able to coerce tuples into arguments of a function. To explore a little about this, and also learn proc-macros, I made https://github.com/swfsql/loosen - but I've never really used it.

As others have pointed out, you could just use a non-anonymous function defined in the current scope in place of a lamba...

"PySlice" with karma 25 is probably your trolling account. If not, ASK about stuff you have no clue about. It is better than looking like a little troll kid, having "fun" lowering the quality of HN by stupid language war comments.

>>where all arguments come in a flattened array

That is optional, through the power of CPAN (and kbenson's methods, of course). Examples of alternatives:

http://search.cpan.org/~barefoot/Method-Signatures-20130222/...

http://search.cpan.org/~flora/MooseX-Declare-0.35/lib/MooseX...

http://search.cpan.org/~ether/MooseX-Method-Signatures-0.44/...

(Depending on if you use Moose or not.)

One of the cool parts of Perl is that the language is extensible, that is why it e.g. has a better OO than Python, Ruby, etc. (Or simple typing of parameter arguments, see the modules above. Or why MooseX::Declare has ... ah, look yourself, troll.)


Clojure[0] and Anarki[1] (public fork of Arc) both provide a way to do this (with more than one argument as well). They both have syntax for a "literal function". Your second example would become:

Arc/Anarki: [format-names2 "John" _ "Jones"] which expands to (fn (_) (format-names2 "John" _ "Jones"))

Clojure: #(format-names2 "John" % "Jones") which expands to (fn (%) (format names2 "John" % "Jones"))

Then for a function of two arguments that curried the middle one:

Anarki: [format-names2 _1 "Paul" _2] which expands to (fn (_1 _2) (format-names2 _1 "Paul" _2))

Clojure: #(format-names2 %1 "Paul" %2) which expands to (fn [%1 %2] (format-names2 %1 "Paul" %2))

[0] http://clojure.org/reader [1] https://github.com/arclanguage/anarki/blob/master/CHANGES/ma...


One 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

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.


Sure, but if function_name is a subword of another_function_name, this would break things :/

The language is Lua BTW. It does seem to make good use of Lua's syntax for function calls on a single table, e.g.

    func {
      key1 = "value1",
      key2 = "value2"
    }
next

Legal | privacy