From 65a97a2163e40019e911053fb1e50b22337af118 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 3 Jan 2016 22:25:34 +0100 Subject: fixed optimizations for multiple consecutive arithmetic operators * we have to make sure that exactly the same function values are used --- applause.lua | 34 ++++++++++++++++------------------ 1 file 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 -- cgit v1.2.3