Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
84cc7a9e05 | |||
cdb1cf3e03 | |||
4f3cf3f537 |
22
contains.go
Normal file
22
contains.go
Normal 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
15
count.go
Normal 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
11
each.go
Normal 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)
|
||||
}
|
||||
}
|
9
flat.go
Normal file
9
flat.go
Normal 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
|
||||
}
|
4
go.mod
4
go.mod
@ -1,5 +1,3 @@
|
||||
module git.milar.in/milarin/slices
|
||||
|
||||
go 1.19
|
||||
|
||||
require git.milar.in/milarin/channel v0.0.7
|
||||
go 1.19
|
9
map.go
Normal file
9
map.go
Normal file
@ -0,0 +1,9 @@
|
||||
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
|
||||
}
|
16
of.go
Normal file
16
of.go
Normal file
@ -0,0 +1,16 @@
|
||||
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
|
||||
}
|
52
slices.go
52
slices.go
@ -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
42
to.go
Normal 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{}{} })
|
||||
}
|
Reference in New Issue
Block a user