mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added lume.chain(), added section to README.md
This commit is contained in:
parent
acdb58a447
commit
330779fb0f
11
README.md
11
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
|
||||
|
||||
|
15
lume.lua
15
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
|
||||
|
Loading…
Reference in New Issue
Block a user