mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
* feat: chain filter map reduce simple Type * docs: chain documentation Co-authored-by: Ruidy <rnemausat@newstore.com>
21 lines
483 B
Go
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)
|
|
}
|