diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-01-03 22:25:34 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2016-01-03 22:28:06 +0100 |
commit | 65a97a2163e40019e911053fb1e50b22337af118 (patch) | |
tree | def01cfa90cec5ed1b362a6819f87815ea5a1b93 | |
parent | cdbb6c0ac72817cd5f1eff16c1be944386a3773c (diff) | |
download | applause2-65a97a2163e40019e911053fb1e50b22337af118.tar.gz |
fixed optimizations for multiple consecutive arithmetic operators
* we have to make sure that exactly the same function values are used
-rw-r--r-- | applause.lua | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/applause.lua b/applause.lua index d77f1cd..a568a4a 100644 --- a/applause.lua +++ b/applause.lua @@ -449,40 +449,38 @@ function Stream:__tostring() return "{"..table.concat(t, ", ").."}" end +-- NOTE: Named addOp() and similar functions below +-- are necessary instead of lambdas so consecutive +-- operations can be collapsed by ZipStream (which +-- tests for function equivalence) +local function addOp(x1, x2) return x1+x2 end function Stream.__add(op1, op2) - return ZipStream:new(function(x1, x2) - return x1+x2 - end, op1, op2) + return ZipStream:new(addOp, op1, op2) end +local function subOp(x1, x2) return x1-x2 end function Stream.__sub(op1, op2) - return ZipStream:new(function(x1, x2) - return x1-x2 - end, op1, op2) + return ZipStream:new(subOp, op1, op2) end +local function mulOp(x1, x2) return x1*x2 end function Stream.__mul(op1, op2) - return ZipStream:new(function(x1, x2) - return x1*x2 - end, op1, op2) + return ZipStream:new(mulOp, op1, op2) end +local function divOp(x1, x2) return x1/x2 end function Stream.__div(op1, op2) - return ZipStream:new(function(x1, x2) - return x1/x2 - end, op1, op2) + return ZipStream:new(divOp, op1, op2) end +local function modOp(x1, x2) return x1%x2 end function Stream.__mod(op1, op2) - return ZipStream:new(function(x1, x2) - return x1%x2 - end, op1, op2) + return ZipStream:new(modOp, op1, op2) end +local function powOp(x1, x2) return x1^x2 end function Stream.__pow(op1, op2) - return ZipStream:new(function(x1, x2) - return x1^x2 - end, op1, op2) + return ZipStream:new(powOp, op1, op2) end function Stream:__unm() return self:mul(-1) end |