Compare commits
No commits in common. "main" and "v0.0.21" have entirely different histories.
@ -23,10 +23,6 @@ func WriteIntoDelayed[T any](ch chan<- T, delay time.Duration, values ...T) {
|
|||||||
func WriteIntoWriter[T any](ch <-chan T, writers ...io.Writer) {
|
func WriteIntoWriter[T any](ch <-chan T, writers ...io.Writer) {
|
||||||
w := io.MultiWriter(writers...)
|
w := io.MultiWriter(writers...)
|
||||||
EachSuccessive(ch, func(value T) {
|
EachSuccessive(ch, func(value T) {
|
||||||
if err, ok := any(value).(error); ok {
|
|
||||||
fmt.Fprintln(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintln(w, value)
|
fmt.Fprintln(w, value)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
25
filter.go
25
filter.go
@ -1,6 +1,6 @@
|
|||||||
package channel
|
package channel
|
||||||
|
|
||||||
func FilterSuccessive[T any](source <-chan T, filter func(T) bool) <-chan T {
|
func Filter[T any](source <-chan T, filter func(T) bool) <-chan T {
|
||||||
out := make(chan T, cap(source))
|
out := make(chan T, cap(source))
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -14,26 +14,3 @@ func FilterSuccessive[T any](source <-chan T, filter func(T) bool) <-chan T {
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func Filter[T any](source <-chan T, filter func(T) bool) <-chan T {
|
|
||||||
return FilterPreserveOrderWithRunner(source, getDefaultRunner(), filter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func FilterPreserveOrderWithRunner[T any](source <-chan T, runner Runner, filter func(T) bool) <-chan T {
|
|
||||||
type FilteredValue[T any] struct {
|
|
||||||
Value T
|
|
||||||
Filter bool
|
|
||||||
}
|
|
||||||
|
|
||||||
mappedValues := MapPreserveOrderWithRunner(source, runner, func(value T) FilteredValue[T] {
|
|
||||||
return FilteredValue[T]{Value: value, Filter: filter(value)}
|
|
||||||
})
|
|
||||||
|
|
||||||
filteredValues := FilterSuccessive(mappedValues, func(filteredValue FilteredValue[T]) bool {
|
|
||||||
return filteredValue.Filter
|
|
||||||
})
|
|
||||||
|
|
||||||
return MapSuccessive(filteredValues, func(filteredValue FilteredValue[T]) T {
|
|
||||||
return filteredValue.Value
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
2
find.go
2
find.go
@ -10,8 +10,8 @@ func FindFirst[T any](source <-chan T) *T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FindFirstAndCancel[T any](source <-chan T, cancel context.CancelFunc) *T {
|
func FindFirstAndCancel[T any](source <-chan T, cancel context.CancelFunc) *T {
|
||||||
defer cancel()
|
|
||||||
for v := range source {
|
for v := range source {
|
||||||
|
cancel()
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
15
flat.go
15
flat.go
@ -15,21 +15,6 @@ func FlatSlice[T any](source <-chan []T) <-chan T {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func FlatMap[K comparable, V, T any](source <-chan map[K]V, unmapper func(key K, value V) T) <-chan T {
|
|
||||||
out := make(chan T, cap(source))
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(out)
|
|
||||||
for slice := range source {
|
|
||||||
for k, v := range slice {
|
|
||||||
out <- unmapper(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func FlatChan[T any](source <-chan <-chan T) <-chan T {
|
func FlatChan[T any](source <-chan <-chan T) <-chan T {
|
||||||
out := make(chan T, cap(source))
|
out := make(chan T, cap(source))
|
||||||
|
|
||||||
|
2
go.mod
2
go.mod
@ -1,3 +1,3 @@
|
|||||||
module git.milar.in/milarin/channel
|
module git.milar.in/milarin/channel
|
||||||
|
|
||||||
go 1.23
|
go 1.18
|
||||||
|
32
of.go
32
of.go
@ -2,7 +2,6 @@ package channel
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"iter"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -59,7 +58,7 @@ func OfFunc[T any](ctx context.Context, buffer int, f func() T) <-chan T {
|
|||||||
func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) <-chan T {
|
func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) <-chan T {
|
||||||
out := make(chan T, len(m))
|
out := make(chan T, len(m))
|
||||||
|
|
||||||
go func() {
|
defer func() {
|
||||||
defer close(out)
|
defer close(out)
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
out <- unmapper(k, v)
|
out <- unmapper(k, v)
|
||||||
@ -68,32 +67,3 @@ func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) <-chan T {
|
|||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// OfSeq returns a channel containing all values provided by the iterator
|
|
||||||
func OfSeq[T any](seq iter.Seq[T], buffer int) <-chan T {
|
|
||||||
out := make(chan T, buffer)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(out)
|
|
||||||
for v := range seq {
|
|
||||||
out <- v
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
// OfSeq2 returns a channel containing the return values of the unmapper function
|
|
||||||
// when provided with the values of the iterator
|
|
||||||
func OfSeq2[K comparable, V, T any](seq iter.Seq2[K, V], buffer int, unmapper func(K, V) T) <-chan T {
|
|
||||||
out := make(chan T, buffer)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer close(out)
|
|
||||||
for key, value := range seq {
|
|
||||||
out <- unmapper(key, value)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
17
result.go
17
result.go
@ -13,12 +13,12 @@ func ResultOf[T any](value T, err error) Result[T] {
|
|||||||
return Result[T]{value: &value, err: nil}
|
return Result[T]{value: &value, err: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapResultOutputFunc[I, O any](f func(I) (O, error)) func(I) Result[O] {
|
func WrapMapFunc[I, O any](f func(I) (O, error)) func(I) Result[O] {
|
||||||
return func(i I) Result[O] { return ResultOf(f(i)) }
|
return func(i I) Result[O] { return ResultOf(f(i)) }
|
||||||
}
|
}
|
||||||
|
|
||||||
func WrapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
|
func WrapMapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
|
||||||
resFunc := WrapResultOutputFunc(f)
|
resFunc := WrapMapFunc(f)
|
||||||
nilValue := *new(O)
|
nilValue := *new(O)
|
||||||
return func(r Result[I]) Result[O] {
|
return func(r Result[I]) Result[O] {
|
||||||
v, err := r.Get()
|
v, err := r.Get()
|
||||||
@ -46,13 +46,10 @@ func (r Result[T]) GetOrDefault(defaultValue T) T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r Result[T]) Get() (T, error) {
|
func (r Result[T]) Get() (T, error) {
|
||||||
if r.err != nil {
|
|
||||||
return *new(T), r.err
|
|
||||||
}
|
|
||||||
return *r.value, r.err
|
return *r.value, r.err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Result[T]) GetUnsafe() T {
|
func (r Result[T]) GetSafe() T {
|
||||||
if r.err != nil {
|
if r.err != nil {
|
||||||
panic(r.err)
|
panic(r.err)
|
||||||
}
|
}
|
||||||
@ -64,7 +61,7 @@ func (r Result[T]) Err() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
||||||
succeeded := Filter(source, Result[T].Success)
|
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
|
||||||
|
|
||||||
return MapSuccessive(succeeded, func(r Result[T]) T {
|
return MapSuccessive(succeeded, func(r Result[T]) T {
|
||||||
v, _ := r.Get()
|
v, _ := r.Get()
|
||||||
@ -73,7 +70,7 @@ func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FilterFail[T any](source <-chan Result[T]) <-chan T {
|
func FilterFail[T any](source <-chan Result[T]) <-chan T {
|
||||||
failed := Filter(source, Result[T].Fail)
|
failed := Filter(source, func(r Result[T]) bool { return r.Fail() })
|
||||||
|
|
||||||
return MapSuccessive(failed, func(r Result[T]) T {
|
return MapSuccessive(failed, func(r Result[T]) T {
|
||||||
v, _ := r.Get()
|
v, _ := r.Get()
|
||||||
@ -94,7 +91,7 @@ func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <
|
|||||||
fail <- r.Err()
|
fail <- r.Err()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
succ <- r.GetUnsafe()
|
succ <- r.GetSafe()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
16
to.go
16
to.go
@ -9,22 +9,6 @@ func ToSlice[T any](ch <-chan T) []T {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToSliceContinuous returns a slice containing all values read from ch.
|
|
||||||
// The returned slice will be a pointer slice to a continuous block of memory.
|
|
||||||
// All values will be copied.
|
|
||||||
func ToSliceContinuous[T any](ch <-chan *T) []*T {
|
|
||||||
values := make([]T, 0, cap(ch))
|
|
||||||
pointers := make([]*T, 0, cap(ch))
|
|
||||||
EachSuccessive(ch, func(value *T) {
|
|
||||||
pointers = append(pointers, value)
|
|
||||||
|
|
||||||
if value != nil {
|
|
||||||
values = append(values, *value)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return pointers
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToSliceDeref returns a slice containing all values read from ch.
|
// ToSliceDeref returns a slice containing all values read from ch.
|
||||||
// The returned slice will be a dereferenced and continuous block of memory.
|
// The returned slice will be a dereferenced and continuous block of memory.
|
||||||
// Nil pointers are ignored.
|
// Nil pointers are ignored.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user