Compare commits

...

11 Commits
v0.0.1 ... main

Author SHA1 Message Date
abeecfd797 added default unmapper functions for OfMap 2024-04-02 19:53:21 +02:00
5735640ab6 added FindFirstIndex and FindLastIndex 2024-02-20 16:06:41 +01:00
26ea846e93 - added FindFirst and FindLast
- removed Search
2024-02-20 16:03:29 +01:00
0592add8ca MapError implemented 2023-03-25 08:52:08 +01:00
ab6e6684f8 added Reduce method 2023-03-09 14:34:44 +01:00
Timon Ringwald
55928776b4 filter added 2022-09-17 00:04:53 +02:00
Timon Ringwald
84cc7a9e05 Count and CountCmp added 2022-09-07 22:12:33 +02:00
Timon Ringwald
cdb1cf3e03 more helper functions 2022-09-06 01:58:19 +02:00
Timon Ringwald
4f3cf3f537 removed channel dependency 2022-08-28 15:03:40 +02:00
Timon Ringwald
2124bff9de added Search function 2022-08-25 14:03:50 +02:00
Timon Ringwald
2fb5ea542d fixed reverse implementation 2022-08-21 14:08:25 +02:00
14 changed files with 216 additions and 56 deletions

22
contains.go Normal file
View File

@ -0,0 +1,22 @@
package slices
func IndexOf[T comparable](slice []T, value T) int {
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
}
func IndexOfCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
for i, v := range slice {
if cmp(v, value) {
return i
}
}
return -1
}
func Contains[T comparable](slice []T, value T) bool {
return ContainsCmp(slice, value, DefaultEqualityComparator[T])
}
func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool {
return IndexOfCmp(slice, value, cmp) != -1
}

15
count.go Normal file
View File

@ -0,0 +1,15 @@
package slices
func Count[T comparable](slice []T, value T) int {
return CountCmp(slice, value, DefaultEqualityComparator[T])
}
func CountCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
c := 0
for _, v := range slice {
if cmp(v, value) {
c++
}
}
return c
}

11
each.go Normal file
View File

@ -0,0 +1,11 @@
package slices
func Each[T any](slice []T, f func(T)) {
EachIndex(slice, func(_ int, v T) { f(v) })
}
func EachIndex[T any](slice []T, f func(int, T)) {
for i, v := range slice {
f(i, v)
}
}

38
filter.go Normal file
View File

@ -0,0 +1,38 @@
package slices
func Filter[T any](slice []T, f func(T) bool) []T {
ret := make([]T, 0, len(slice))
for _, v := range slice {
if f(v) {
ret = append(ret, v)
}
}
return ret
}
func FindFirst[T any](slice []T, f func(T) bool) (T, bool) {
for _, v := range slice {
if f(v) {
return v, true
}
}
return *new(T), false
}
func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) {
for i, v := range slice {
if f(v) {
return i, true
}
}
return -1, false
}
func FindLastIndex[T any](slice []T, f func(T) bool) (int, bool) {
for i := len(slice); i >= 0; i-- {
if f(slice[i]) {
return i, true
}
}
return -1, false
}

9
flat.go Normal file
View File

@ -0,0 +1,9 @@
package slices
func Flat[T any](s ...[]T) []T {
out := make([]T, 0)
for _, v := range s {
out = append(out, v...)
}
return out
}

2
go.mod
View File

@ -2,4 +2,4 @@ module git.milar.in/milarin/slices
go 1.19
require git.milar.in/milarin/channel v0.0.7
require git.milar.in/milarin/gmath v0.0.3

4
go.sum
View File

@ -1,2 +1,2 @@
git.milar.in/milarin/channel v0.0.7 h1:cVKtwgH/EE7U+XTHcoFCClJ4LR349KanzjX9xKwRcNg=
git.milar.in/milarin/channel v0.0.7/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=

21
map.go Normal file
View File

@ -0,0 +1,21 @@
package slices
func Map[I, O any](slice []I, mapper func(I) O) []O {
ret := make([]O, 0, len(slice))
for _, v := range slice {
ret = append(ret, mapper(v))
}
return ret
}
func MapError[I, O any](slice []I, mapper func(I) (O, error)) ([]O, error) {
ret := make([]O, 0, len(slice))
for _, old := range slice {
new, err := mapper(old)
if err != nil {
return nil, err
}
ret = append(ret, new)
}
return ret, nil
}

28
of.go Normal file
View File

@ -0,0 +1,28 @@
package slices
func Of[T any](values ...T) []T {
return values
}
// OfMap returns a slice containing the return values of the unmapper function
// applied to any key-value pair in m
// The order is random
func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T {
out := make([]T, 0, len(m))
for k, v := range m {
out = append(out, unmapper(k, v))
}
return out
}
// UnmapKey is an unmapper function which returns the map key only
// and discards its value. It is supposed to be used with OfMap
func UnmapKey[K comparable, V any](key K, _ V) K {
return key
}
// UnmapValue is an unmapper function which returns the map value only
// and discards its key. It is supposed to be used with OfMap
func UnmapValue[K comparable, V any](_ K, value V) V {
return value
}

15
reduce.go Normal file
View File

@ -0,0 +1,15 @@
package slices
import "git.milar.in/milarin/gmath"
func Reduce[T, R any](slice []T, reducer func(current R, v T) R) R {
res := new(R)
Each(slice, func(v T) {
*res = reducer(*res, v)
})
return *res
}
func SumReducer[N gmath.Number](a, b N) N {
return a + b
}

11
reduce_test.go Normal file
View File

@ -0,0 +1,11 @@
package slices
import (
"fmt"
"testing"
)
func TestReduce(t *testing.T) {
s := Of(1, 2, 3)
fmt.Println(Reduce(s, SumReducer[int]))
}

View File

@ -4,7 +4,7 @@ func Reverse[T any](slice []T) []T {
s := make([]T, len(slice))
for i := 0; i < len(slice); i++ {
ri := len(slice) - 1 - i
s[ri] = s[i]
s[ri] = slice[i]
}
return s
}

View File

@ -1,52 +0,0 @@
package slices
import (
"git.milar.in/milarin/channel"
)
func IndexOf[T comparable](slice []T, value T) int {
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
}
func IndexOfCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) int {
for i, v := range slice {
if cmp(v, value) {
return i
}
}
return -1
}
func Contains[T comparable](slice []T, value T) bool {
return ContainsCmp(slice, value, DefaultEqualityComparator[T])
}
func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool {
return IndexOfCmp(slice, value, cmp) != -1
}
func Map[I, O any](slice []I, mapper func(I) O) []O {
ret := make([]O, 0, len(slice))
for _, v := range slice {
ret = append(ret, mapper(v))
}
return ret
}
func MapParallel[I, O any](slice []I, mapper func(I) O) []O {
return channel.ToSlice(channel.Map(channel.Of(slice...), mapper))
}
func MapParallelWithRunner[I, O any](slice []I, runner channel.Runner, mapper func(I) O) []O {
return channel.ToSlice(channel.MapWithRunner(channel.Of(slice...), runner, mapper))
}
func Each[T any](slice []T, f func(T)) {
EachIndex(slice, func(_ int, v T) { f(v) })
}
func EachIndex[T any](slice []T, f func(int, T)) {
for i, v := range slice {
f(i, v)
}
}

42
to.go Normal file
View File

@ -0,0 +1,42 @@
package slices
import (
"container/list"
)
// Deref returns a slice containing all dereferenced values of s.
// The returned slice will be a dereferenced and continuous block of memory.
// Nil pointers are ignored.
func Deref[T any](s []*T) []T {
out := make([]T, 0, len(s))
Each(s, func(v *T) {
if v != nil {
out = append(out, *v)
}
})
return out
}
// ToList returns a list.List containing all values of s
func ToList[T any](s []T) *list.List {
l := list.New()
Each(s, func(value T) { l.PushBack(value) })
return l
}
// ToMap returns a map containing all values of s.
// The map key-value pairs are determined by mapper
func ToMap[T any, K comparable, V any](s []T, mapper func(T) (K, V)) map[K]V {
m := map[K]V{}
Each(s, func(value T) {
k, v := mapper(value)
m[k] = v
})
return m
}
// ToStructMap returns a struct{} map containing all values of s as keys.
// It is a shorthand for ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} })
func ToStructMap[T comparable](s []T) map[T]struct{} {
return ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} })
}