Skip to content

Instantly share code, notes, and snippets.

@MattMcFarland
Created April 5, 2017 11:17
Show Gist options
  • Save MattMcFarland/9cea2a7de01b9bfdaf738a3386d39689 to your computer and use it in GitHub Desktop.
Save MattMcFarland/9cea2a7de01b9bfdaf738a3386d39689 to your computer and use it in GitHub Desktop.
functional pico8
function _init()
my_grid = make_grid(10,10)
.map(set_wall)
my_grid.each(log)
room = my_grid
.select(4, 4, 7, 7)
.map(set_blank)
room.each(log)
state = compose(my_grid, room)
state.each(log)
end
function log(v, x, y)
print(x..','..y..':'..v)
end
function render(v, x, y)
pset(x, y, v)
end
function make_grid(width, height)
local whole={}
return grid(1,1,width,height,whole)
end
function grid(x1, y1, x2, y2, _grid)
_grid = either(_grid, {})
_grid[0] = {}
x1 = either(x1, 1)
y1 = either(y1, 1)
x2 = either(x2, #_grid)
y2 = either(y2, #_grid[0])
local with=function(fn)
for x=x1,x2 do
_grid[x] = either(_grid[x], {})
for y=y1,y2 do
fn(x, y)
end
end
end
return {
body=function()
return _grid
end,
select=function(x1, y1, x2, y2)
return grid(x1, y1, x2, y2, _grid)
end,
map=function(fn)
local tbl={}
with(function(x,y)
tbl[x] = either(tbl[x], {})
tbl[x][y] = fn(_grid[x][y], x ,y)
end)
return grid(x1, y1, x2, y2, tbl)
end,
each=function(fn)
with(function(x,y)
fn(_grid[x][y], x, y)
end)
end
}
end
function compose(...)
local arg = {...}
local t={}
for a=1,#arg do
local body = arg[a].body()
for k,v in pairs(body) do
print(v)
add(t, v) end
end
return grid(1, 1, 120, 100, t)
end
function set_blank() return 0 end
function set_wall() return 1 end
function either(a, b)
if (nil == a) return b
return a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment