diff --git a/README.md b/README.md index 26aaeeb..61efa30 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,17 @@ arguments to [LÖVE](http://love2d.org)'s setColor() function. lume.rgba(0xFF304050) -- Returns 48, 64, 80, 255 ``` +### lume.chain(value) +Returns a wrapped object which allows chaining of lume functions. The function +result() should be called at the end of the chain to return the resulting +value. +```lua +lume.chain({1, 2, 3, 4}) + :filter(function(x) return x % 2 == 0 end) + :map(function(x) return -x end) + :result() -- Returns { -2, -4 } +``` + ## License diff --git a/lume.lua b/lume.lua index b2e4109..82a1262 100644 --- a/lume.lua +++ b/lume.lua @@ -417,4 +417,19 @@ function lume.rgba(color) end +local chain_mt = {} +chain_mt.__index = lume.map(lume.filter(lume, isfunction, true), + function(fn) + return function(self, ...) + self._value = fn(self._value, ...) + return self + end + end) +chain_mt.__index.result = function(x) return x._value end + +function lume.chain(value) + return setmetatable({ _value = value }, chain_mt) +end + + return lume