Initial commit of modash
This was taken from github.com/lmika/gopkgs/fp
This commit is contained in:
commit
a20530ddfd
20 changed files with 425 additions and 0 deletions
30
momap/fromslice.go
Normal file
30
momap/fromslice.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package momap
|
||||
|
||||
func FromSliceGroups[T any, K comparable](ts []T, fn func(t T) K) map[K][]T {
|
||||
kvs := make(map[K][]T)
|
||||
for _, t := range ts {
|
||||
k := fn(t)
|
||||
kvs[k] = append(kvs[k], t)
|
||||
}
|
||||
return kvs
|
||||
}
|
||||
|
||||
func FromSlice[T any, K comparable, V any](ts []T, fn func(t T) (K, V)) map[K]V {
|
||||
m, _ := FromSliceWithError(ts, func(t T) (k K, v V, _ error) {
|
||||
k, v = fn(t)
|
||||
return k, v, nil
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
func FromSliceWithError[T any, K comparable, V any](ts []T, fn func(t T) (K, V, error)) (map[K]V, error) {
|
||||
kvs := make(map[K]V)
|
||||
for _, t := range ts {
|
||||
k, v, err := fn(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kvs[k] = v
|
||||
}
|
||||
return kvs, nil
|
||||
}
|
||||
9
momap/keys.go
Normal file
9
momap/keys.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package momap
|
||||
|
||||
func Keys[K comparable, V any](m map[K]V) []K {
|
||||
ks := make([]K, 0, len(m))
|
||||
for k := range m {
|
||||
ks = append(ks, k)
|
||||
}
|
||||
return ks
|
||||
}
|
||||
29
momap/toslice.go
Normal file
29
momap/toslice.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package momap
|
||||
|
||||
func ToSlice[K comparable, V, T any](m map[K]V, fn func(k K, v V) T) []T {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ts := make([]T, 0, len(m))
|
||||
for k, v := range m {
|
||||
ts = append(ts, fn(k, v))
|
||||
}
|
||||
return ts
|
||||
}
|
||||
|
||||
func ToSliceWithError[K comparable, V, T any](m map[K]V, fn func(k K, v V) (T, error)) ([]T, error) {
|
||||
if m == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ts := make([]T, 0, len(m))
|
||||
for k, v := range m {
|
||||
w, err := fn(k, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ts = append(ts, w)
|
||||
}
|
||||
return ts, nil
|
||||
}
|
||||
28
momap/toslice_test.go
Normal file
28
momap/toslice_test.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package momap_test
|
||||
|
||||
import (
|
||||
"lmika.dev/pkg/modash/momap"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestToSlice(t *testing.T) {
|
||||
type pair struct {
|
||||
left int
|
||||
right string
|
||||
}
|
||||
|
||||
ms := map[int]string{
|
||||
1: "one",
|
||||
2: "two",
|
||||
3: "three",
|
||||
}
|
||||
|
||||
pairs := momap.ToSlice(ms, func(k int, v string) pair { return pair{k, v} })
|
||||
|
||||
assert.Len(t, pairs, 3)
|
||||
assert.Contains(t, pairs, pair{1, "one"})
|
||||
assert.Contains(t, pairs, pair{2, "two"})
|
||||
assert.Contains(t, pairs, pair{3, "three"})
|
||||
}
|
||||
17
momap/values.go
Normal file
17
momap/values.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package momap
|
||||
|
||||
func Values[K comparable, V any](m map[K]V) []V {
|
||||
vs := make([]V, 0, len(m))
|
||||
for _, v := range m {
|
||||
vs = append(vs, v)
|
||||
}
|
||||
return vs
|
||||
}
|
||||
|
||||
func MapValues[K comparable, V any, W any](m map[K]V, fn func(v V, k K) W) map[K]W {
|
||||
ws := make(map[K]W)
|
||||
for k, v := range m {
|
||||
ws[k] = fn(v, k)
|
||||
}
|
||||
return ws
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue