-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmap.lua
More file actions
25 lines (24 loc) · 571 Bytes
/
map.lua
File metadata and controls
25 lines (24 loc) · 571 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--[[
expr = expression to change
callback(node) = callback that returns nil if it leaves the tree untouched, returns a value if it wishes to change the tree
--]]
local Constant
local function map(expr, callback)
-- clone
if expr.clone then expr = expr:clone() end
-- process children
if expr then
for i=1,#expr do
expr[i] = map(expr[i], callback)
end
end
-- process this node
expr = callback(expr) or expr
if type(expr) == 'number' then
Constant = Constant or require 'symmath.Constant'
return Constant(expr)
end
-- done
return expr
end
return map