-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from koss-null/1.0.5
1.0.5
- Loading branch information
Showing
49 changed files
with
2,442 additions
and
1,255 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 |
---|---|---|
|
@@ -20,3 +20,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
cover.cov |
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"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 | ||
} | ||
|
||
type MyStruct struct{ Weight int } | ||
|
||
func main() { | ||
p := pipe.Slice([]int{1, 2, 3}). | ||
Erase(). | ||
Map(func(x any) any { | ||
i := *(x.(*int)) | ||
return &MyStruct{Weight: i} | ||
}).Filter(func(x *any) bool { | ||
return (*x).(*MyStruct).Weight > 10 | ||
}) | ||
ms := pipe.Collect[MyStruct](p).Parallel(10).Do() | ||
|
||
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) | ||
} | ||
}() | ||
go func() { | ||
for err := range enrichUserErr { | ||
fmt.Println("unable to enrich user", err) | ||
} | ||
}() | ||
|
||
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) | ||
} |
Oops, something went wrong.