mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Added lume.chain(), added section to README.md
This commit is contained in:
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.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
|
## License
|
||||||
|
|
||||||
|
15
lume.lua
15
lume.lua
@@ -417,4 +417,19 @@ function lume.rgba(color)
|
|||||||
end
|
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
|
return lume
|
||||||
|
Reference in New Issue
Block a user