3 Commits

Author SHA1 Message Date
4f3cf3f537 removed channel dependency 2022-08-28 15:03:40 +02:00
2124bff9de added Search function 2022-08-25 14:03:50 +02:00
2fb5ea542d fixed reverse implementation 2022-08-21 14:08:25 +02:00
4 changed files with 13 additions and 16 deletions

2
go.mod
View File

@ -1,5 +1,3 @@
module git.milar.in/milarin/slices
go 1.19
require git.milar.in/milarin/channel v0.0.7

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
}

11
search.go Normal file
View File

@ -0,0 +1,11 @@
package slices
func Search[T any](slice []T, f func(a, b T) T) T {
if len(slice) == 0 {
return *new(T)
}
value := slice[0]
Each(slice, func(v T) { value = f(value, v) })
return value
}

View File

@ -1,9 +1,5 @@
package slices
import (
"git.milar.in/milarin/channel"
)
func IndexOf[T comparable](slice []T, value T) int {
return IndexOfCmp(slice, value, DefaultEqualityComparator[T])
}
@ -33,14 +29,6 @@ func Map[I, O any](slice []I, mapper func(I) O) []O {
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) })
}