Compare commits

..

No commits in common. "78436e629cc4780b9ffaea20741095209fb33bd0" and "6fcd709cd378bdd3feb0c679ee2483905d7bb7f3" have entirely different histories.

2 changed files with 7 additions and 25 deletions

15
flat.go
View File

@ -15,21 +15,6 @@ func FlatSlice[T any](source <-chan []T) <-chan T {
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 {
out := make(chan T, cap(source))

View File

@ -13,12 +13,12 @@ func ResultOf[T any](value T, err error) Result[T] {
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)) }
}
func WrapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
resFunc := WrapResultOutputFunc(f)
func WrapMapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
resFunc := WrapMapFunc(f)
nilValue := *new(O)
return func(r Result[I]) Result[O] {
v, err := r.Get()
@ -46,13 +46,10 @@ func (r Result[T]) GetOrDefault(defaultValue T) T {
}
func (r Result[T]) Get() (T, error) {
if r.err != nil {
return *new(T), r.err
}
return *r.value, r.err
}
func (r Result[T]) GetUnsafe() T {
func (r Result[T]) GetSafe() T {
if r.err != nil {
panic(r.err)
}
@ -64,7 +61,7 @@ func (r Result[T]) Err() error {
}
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 {
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 {
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 {
v, _ := r.Get()
@ -94,7 +91,7 @@ func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <
fail <- r.Err()
continue
}
succ <- r.GetUnsafe()
succ <- r.GetSafe()
}
}()