2022-03-09 14:47:49 +01:00
|
|
|
package channel
|
|
|
|
|
|
|
|
import "container/list"
|
|
|
|
|
|
|
|
// ToSlice returns a slice containing all values read from ch
|
|
|
|
func ToSlice[T any](ch <-chan T) []T {
|
|
|
|
s := make([]T, 0, cap(ch))
|
2023-03-04 11:24:42 +01:00
|
|
|
EachSuccessive(ch, func(value T) { s = append(s, value) })
|
2022-03-09 14:47:49 +01:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSliceDeref returns a slice containing all values read from ch.
|
|
|
|
// The returned slice will be a dereferenced and continuous block of memory.
|
|
|
|
// Nil pointers are ignored.
|
|
|
|
func ToSliceDeref[T any](ch <-chan *T) []T {
|
|
|
|
s := make([]T, 0, cap(ch))
|
2023-03-04 11:24:42 +01:00
|
|
|
EachSuccessive(ch, func(value *T) {
|
2022-03-09 14:47:49 +01:00
|
|
|
if value != nil {
|
|
|
|
s = append(s, *value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToList returns a list.List containing all values read from ch
|
|
|
|
func ToList[T any](ch <-chan T) *list.List {
|
|
|
|
l := list.New()
|
2023-03-04 11:24:42 +01:00
|
|
|
EachSuccessive(ch, func(value T) { l.PushBack(value) })
|
2022-03-09 14:47:49 +01:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap returns a map containing all values read from ch.
|
2022-12-17 19:50:41 +01:00
|
|
|
// The map key-value pairs are determined by f which will be called as concurrently as possible
|
|
|
|
// to build the resulting map
|
2022-03-09 14:55:11 +01:00
|
|
|
func ToMap[T any, K comparable, V any](ch <-chan T, f func(T) (K, V)) map[K]V {
|
2022-12-17 19:50:41 +01:00
|
|
|
return ToMapWithRunner(ch, getDefaultRunner(), f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap returns a map containing all values read from ch.
|
|
|
|
// The map key-value pairs are determined by f which will be called as concurrently as possible
|
|
|
|
// to build the resulting map
|
|
|
|
func ToMapWithRunner[T any, K comparable, V any](ch <-chan T, runner Runner, f func(T) (K, V)) map[K]V {
|
|
|
|
map2entry := func(t T) mapEntry[K, V] {
|
|
|
|
k, v := f(t)
|
|
|
|
return mapEntry[K, V]{Key: k, Value: v}
|
|
|
|
}
|
|
|
|
|
|
|
|
map2kv := func(e mapEntry[K, V]) (K, V) { return e.Key, e.Value }
|
|
|
|
|
|
|
|
return ToMapSuccessive(MapWithRunner(ch, runner, map2entry), map2kv)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMapSuccessive returns a map containing all values read from ch.
|
|
|
|
// The map key-value pairs are determined by f
|
|
|
|
func ToMapSuccessive[T any, K comparable, V any](ch <-chan T, f func(T) (K, V)) map[K]V {
|
2022-03-09 14:47:49 +01:00
|
|
|
m := map[K]V{}
|
2023-03-04 11:24:42 +01:00
|
|
|
EachSuccessive(ch, func(value T) {
|
2022-03-09 14:55:11 +01:00
|
|
|
k, v := f(value)
|
|
|
|
m[k] = v
|
|
|
|
})
|
2022-03-09 14:47:49 +01:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStructMap returns a struct{} map containing all values read from ch as keys.
|
2022-03-09 14:55:11 +01:00
|
|
|
// It is a shorthand for ToMap(ch, func(value T) (T, struct{}) { return value, struct{}{} })
|
2022-03-09 14:47:49 +01:00
|
|
|
func ToStructMap[T comparable](ch <-chan T) map[T]struct{} {
|
2022-03-09 14:55:11 +01:00
|
|
|
return ToMap(ch, func(value T) (T, struct{}) { return value, struct{}{} })
|
2022-03-09 14:47:49 +01:00
|
|
|
}
|