underscore/chain.go
Ruidy 2798562b46
Enable Chaining (#6)
* feat: chain filter map reduce simple Type

* docs: chain documentation

Co-authored-by: Ruidy <rnemausat@newstore.com>
2021-12-31 18:21:00 +01:00

21 lines
483 B
Go

package underscore
type Chain[T any] struct {
Value []T
}
func NewChain[T any](value []T) Chain[T] {
return Chain[T]{Value: value}
}
func (c Chain[T]) Filter(predicate func(n T) bool) Chain[T] {
return Chain[T]{Value: Filter(c.Value, predicate)}
}
func (c Chain[T]) Map(transform func(n T) T) Chain[T] {
return Chain[T]{Value: Map(c.Value, transform)}
}
func (c Chain[T]) Reduce(reducer func(n, acc T) T, initialValue T) T {
return Reduce(c.Value, reducer, initialValue)
}