Compare commits
4 Commits
6fcd709cd3
...
78436e629c
Author | SHA1 | Date | |
---|---|---|---|
78436e629c | |||
4612cb74b2 | |||
faf75185ae | |||
3dfcee4ee4 |
15
flat.go
15
flat.go
@ -15,6 +15,21 @@ 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))
|
||||
|
||||
|
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}
|
||||
}
|
||||
|
||||
func WrapMapFunc[I, O any](f func(I) (O, error)) func(I) Result[O] {
|
||||
func WrapResultOutputFunc[I, O any](f func(I) (O, error)) func(I) Result[O] {
|
||||
return func(i I) Result[O] { return ResultOf(f(i)) }
|
||||
}
|
||||
|
||||
func WrapMapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
|
||||
resFunc := WrapMapFunc(f)
|
||||
func WrapResultFunc[I, O any](f func(I) (O, error)) func(Result[I]) Result[O] {
|
||||
resFunc := WrapResultOutputFunc(f)
|
||||
nilValue := *new(O)
|
||||
return func(r Result[I]) Result[O] {
|
||||
v, err := r.Get()
|
||||
@ -46,10 +46,13 @@ 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]) GetSafe() T {
|
||||
func (r Result[T]) GetUnsafe() T {
|
||||
if r.err != nil {
|
||||
panic(r.err)
|
||||
}
|
||||
@ -61,7 +64,7 @@ func (r Result[T]) Err() error {
|
||||
}
|
||||
|
||||
func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
||||
succeeded := Filter(source, func(r Result[T]) bool { return r.Success() })
|
||||
succeeded := Filter(source, Result[T].Success)
|
||||
|
||||
return MapSuccessive(succeeded, func(r Result[T]) T {
|
||||
v, _ := r.Get()
|
||||
@ -70,7 +73,7 @@ func FilterSuccess[T any](source <-chan Result[T]) <-chan T {
|
||||
}
|
||||
|
||||
func FilterFail[T any](source <-chan Result[T]) <-chan T {
|
||||
failed := Filter(source, func(r Result[T]) bool { return r.Fail() })
|
||||
failed := Filter(source, Result[T].Fail)
|
||||
|
||||
return MapSuccessive(failed, func(r Result[T]) T {
|
||||
v, _ := r.Get()
|
||||
@ -91,7 +94,7 @@ func FilterResults[T any](source <-chan Result[T]) (succeeded <-chan T, failed <
|
||||
fail <- r.Err()
|
||||
continue
|
||||
}
|
||||
succ <- r.GetSafe()
|
||||
succ <- r.GetUnsafe()
|
||||
}
|
||||
}()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user