Skip to content

Instantly share code, notes, and snippets.

@Warr1024
Last active March 29, 2019 01:49
Show Gist options
  • Save Warr1024/0615db5663888fa98da33097df9e3588 to your computer and use it in GitHub Desktop.
Save Warr1024/0615db5663888fa98da33097df9e3588 to your computer and use it in GitHub Desktop.
local function append(x, a, ...)
if not a then return x end
return a, append(x, ...)
end
local function seek(n, x, ...)
if n < 0 then error() end
if n == 0 then return x end
return seek(n - 1, ...)
end
local cat
do
local function catcore(n, b, ...)
if not seek(n, b()) then
return ...
end
return catcore(n + 1, b, append(seek(n, b()), ...))
end
cat = function(a, b)
return catcore(0, b, a())
end
end
local function reduce(f)
local function r(a, b, ...)
if not b then return a end
return r(f(a, b, ...), ...)
end
return r
end
local conscat = reduce(function(a, b)
return function() return cat(a, b) end
end)
local function catall(...)
return conscat(...)()
end
local function length(...)
return reduce(function(a) return a + 1 end)(0, ...)
end
local function map(f, a, ...)
if not a then return end
return f(a), map(f, ...)
end
local function decons(a, b, ...)
if not b then return function() return a end end
return conscat(map(decons, a, b, ...))
end
print(map(function(x) return x * 2 end,
catall(
decons(1, 2, 3),
decons(4, 5, 6),
decons(7, 8, 9))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment