-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dima Kossovich
committed
Nov 19, 2023
1 parent
4f63e0f
commit dae8127
Showing
2 changed files
with
185 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,68 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/koss-null/funcfrog/pkg/pipe" | ||
) | ||
|
||
type DomObj struct{ i int } | ||
|
||
func GetUser(i int) (*DomObj, error) { | ||
if i%2 != 0 { | ||
return &DomObj{i}, nil | ||
} | ||
return nil, fmt.Errorf("%d user can't be found", i) | ||
} | ||
|
||
func EnrichUser(do *DomObj) (*DomObj, error) { | ||
if do.i%3 == 0 { | ||
return nil, fmt.Errorf("user can't be enriched %d", do.i) | ||
} | ||
return do, nil | ||
} | ||
|
||
func main() { | ||
p := pipe.Slice([]int{1, 2, 3, 4, -5, 6, 7, 8, 9}) | ||
y1, y2 := pipe.NewYeti(), pipe.NewYeti() | ||
res := p.Yeti(y1).Map(func(i int) int { | ||
if i < 0 { | ||
y1.Yeet(errors.New("omg the value is NEGATIVE")) | ||
getUserErr := make(chan error, 1) | ||
handleGetUserErr := func(err error) { | ||
getUserErr <- err | ||
} | ||
enrichUserErr := make(chan error, 1) | ||
handleEnrichUserErr := func(err error) { | ||
enrichUserErr <- err | ||
} | ||
go func() { | ||
for err := range getUserErr { | ||
fmt.Println("unable to get user", err) | ||
} | ||
return i - 6 | ||
}).Snag(func(err error) { | ||
fmt.Println("Snagging an error: " + err.Error()) | ||
}).Yeti(y2).Map(func(i int) int { | ||
if i > 0 { | ||
y2.Yeet(errors.New("omg the value is POSITIVE")) | ||
}() | ||
go func() { | ||
for err := range enrichUserErr { | ||
fmt.Println("unable to enrich user", err) | ||
} | ||
return 2 * i | ||
}).Snag(func(err error) { | ||
fmt.Println("another snag for the same error: " + err.Error()) | ||
}).Filter(func(i *int) bool { return *i > 0 }).Do() | ||
fmt.Println(res) | ||
}() | ||
|
||
y1, y2 := pipe.NewYeti(), pipe.NewYeti() | ||
users := pipe.Func(func(i int) (*DomObj, bool) { | ||
domObj, err := GetUser(i) | ||
if err != nil { | ||
y1.Yeet(err) | ||
return nil, false | ||
} | ||
return domObj, true | ||
}).Yeti(y1). | ||
Snag(handleGetUserErr). // suppose we have some pre-defined handler | ||
MapFilter(func(do *DomObj) (*DomObj, bool) { | ||
enriched, err := EnrichUser(do) | ||
if err != nil { | ||
y2.Yeet(err) | ||
return nil, false | ||
} | ||
return enriched, true | ||
}).Yeti(y2).Snag(handleEnrichUserErr). | ||
Gen(20). | ||
Parallel(16). | ||
Do() | ||
|
||
fmt.Println(users) | ||
} |