-
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
Oct 15, 2023
1 parent
73083cd
commit 3fe5550
Showing
6 changed files
with
59 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/koss-null/funcfrog/pkg/pipe" | ||
) | ||
|
||
func main() { | ||
p := pipe.Slice([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}) | ||
y := pipe.NewYeti() | ||
res := p.Yeti(y).Map(func(i int) int { | ||
if i < 0 { | ||
y.Yeet(errors.New("omg the value is negative")) | ||
} | ||
return 2 * i | ||
}).Snag(func(err error) { | ||
fmt.Println("Snagging an error: " + err.Error()) | ||
}).Do() | ||
fmt.Println(res) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package internalpipe | ||
|
||
import "unsafe" | ||
|
||
// Sang ads error handler to a current Pipe step. | ||
func (p Pipe[T]) Snag(h ErrHandler) Pipe[T] { | ||
// todo: think about NPE here | ||
p.y.SnagPipe(unsafe.Pointer(p.prevP), h) | ||
return p | ||
} | ||
|
||
type YeetSnag interface { | ||
// yeet an error | ||
Yeet(err error) | ||
// snag and handle the error | ||
Snag(ErrHandler) | ||
} | ||
|
||
// Yeti adds Yeti error handler to the pipe. | ||
// If some other handlers were set before, they are handled by the Snag | ||
func (p Pipe[T]) Yeti(y YeetSnag) Pipe[T] { | ||
// todo: save previous y | ||
p.y = y.(yeti) | ||
return p | ||
} |
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
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