From 1cecd293f5d3e536cd89a21c00f44118b2426b3c Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 14 Feb 2025 10:29:25 -0300 Subject: src/gobang.go: Add simplistic implementation of Map() and Filter() --- src/gobang.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/gobang.go') diff --git a/src/gobang.go b/src/gobang.go index 2985d19..1826a5f 100644 --- a/src/gobang.go +++ b/src/gobang.go @@ -65,6 +65,30 @@ var ( +func MapIndexed[A any, B any](fn func(A, int) B, coll []A) []B { + out := make([]B, len(coll)) + for i, x := range coll { + out[i] = fn(x, i) + } + return out +} + +func Map[A any, B any](fn func(A) B, coll []A) []B { + return MapIndexed(func(x A, _ int) B { + return fn(x) + }, coll) +} + +func Filter[A any](fn func(A) bool, coll []A) []A { + out := []A{} + for _, x := range coll { + if fn(x) { + out = append(out, x) + } + } + return out +} + func PanicIf(err error) { if err != nil { panic(err) -- cgit v1.2.3