lua-users wiki: Curried Lua

Curried Lua wiki Currying is defined by Wikipedia [1] as follows: "In computer science, currying is the technique of transforming a function taking multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result" You can implement curried functions in all languages that support functions as first-class objects. For example, there's a little [tutorial about curried JavaScript] . Here is a small Lua example of a curried function: function sum(number) return function (anothernumber) return number + anothernumber end end local f = sum(5) print (f(3)) --> 8 -- WalterCruz Here is another, contributed by [ GavinWraith ], which takes a variable number of arguments terminated with a " () ": function addup(x)...

Linked on 2015-01-28 20:19:35 | Similar Links