Added lume.chain(), added section to README.md

This commit is contained in:
rxi 2014-04-05 16:30:10 +01:00
parent acdb58a447
commit 330779fb0f
2 changed files with 26 additions and 0 deletions

View File

@ -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

View File

@ -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