Partial function application: Difference between revisions
Content added Content deleted
(→{{header|Scala}}: Scala example) |
(Add Lua solution) |
||
Line 991: | Line 991: | ||
yes |
yes |
||
</lang> |
</lang> |
||
=={{header|Lua}}== |
|||
<lang lua>function map(f, ...) |
|||
local t = {} |
|||
for k, v in ipairs(...) do |
|||
t[#t+1] = f(v) |
|||
end |
|||
return t |
|||
end |
|||
function timestwo(n) |
|||
return n * 2 |
|||
end |
|||
function squared(n) |
|||
return n ^ 2 |
|||
end |
|||
function partial(f, ...) |
|||
local args = ... |
|||
return function(...) |
|||
return f(args, ...) |
|||
end |
|||
end |
|||
timestwo_s = partial(map, timestwo) |
|||
squared_s = partial(map, squared) |
|||
print(table.concat(timestwo_s{0, 1, 2, 3}, ', ')) |
|||
print(table.concat(squared_s{0, 1, 2, 3}, ', ')) |
|||
print(table.concat(timestwo_s{2, 4, 6, 8}, ', ')) |
|||
print(table.concat(squared_s{2, 4, 6, 8}, ', '))</lang> |
|||
'''Output:''' |
|||
0, 2, 4, 6 |
|||
0, 1, 4, 9 |
|||
4, 8, 12, 16 |
|||
4, 16, 36, 64 |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |