i wanted to pass a function an iterator over a map's keys and now I have this.
there is a non-zero chance that:
- someone else has already done this, or
- this is very non-idiomatic Go.
I'm ok with both.
this whole exercise is somewhat poisoned by how verbose and without-inference go's function literal syntax is ╮ (. ❛ ᴗ ❛.) ╭
package main
import (
"fmt"
i "github.com/bkgood/giter"
)
func main() {
// sum the even numbers in [0, 100)
x := i.Sum(i.Filter(func(x int) bool { return x%2 == 0 }, i.Range[int](0, 100)))
// 2450
fmt.Println(x)
// build an index of some structs by an id
type foo struct {
id int
name string
}
index := i.Collect( // or just ToMap
i.MapCollector[int, foo](),
i.Map(
func(f foo) i.KVPair[int, foo] {
return i.KVPair[int, foo]{f.id, f}
},
i.Slice([]foo{foo{1, "willy"}})))
// map[1:{1 willy}]
fmt.Println(index)
}
i'm still getting used to writing godoc stuff; this would be decent practice.
if i can see a size, I can preallocate in collection.
ranges only ascend now. they should be able to descend.
probably? i wouldn't try to write a blas implementation with it. but for typical webby line-of-business type stuff, it is predictably slow, and behaves the same for large and small n.
but if someone did write a blas implementation with this i would be amused.