(Immutable) Map/Filter/Reduce functions (+ friends).
Disclaimer: if you need as much performance as possible, this library is not for you.
$ go get github.com/bnert/mfr
Note: must have go1.18 installed.
package main
import (
"fmt"
"github.com/bnert/mfr"
)
type Data struct {
Value int `json:"value"`
}
func main() {
d1 := []Data{
Data{Value: 0},
Data{Value: 1},
Data{Value: 2},
Data{Value: 3},
Data{Value: 4},
}
// Map to a different type
d2 := mfr.Map[Data, int](d1, func(ctx mfr.Ctx[Data]) int) []int {
return ctx.Item.Value * 3
})
d3 := mfr.Filter[int](d2, func(ctx mfr.Ctx[int]) bool) []int {
return ctx.Item % 2 == 0
})
sum := mfr.Reduce[int, int](d3, 0, func(ctx mfr.Ctx[int], acc int) int {
return acc + ctx.Item
})
// Prints:
// Reduced even sum: 18
fmt.Println("Reduced evens sum:", sum)
}
Please see examples/
and tests/
directory for other examples of how this
library can be used to write (somewhat) functional go.