-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add more mostly-adequate examples and solutions
Signed-off-by: Dr. Carsten Leue <[email protected]>
- Loading branch information
1 parent
3ccafb5
commit 53f4e5e
Showing
9 changed files
with
386 additions
and
37 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
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
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 |
---|---|---|
|
@@ -18,10 +18,14 @@ package mostlyadequate | |
import ( | ||
"fmt" | ||
"path" | ||
"regexp" | ||
|
||
A "github.com/IBM/fp-go/array" | ||
E "github.com/IBM/fp-go/either" | ||
"github.com/IBM/fp-go/errors" | ||
F "github.com/IBM/fp-go/function" | ||
"github.com/IBM/fp-go/io" | ||
IOE "github.com/IBM/fp-go/ioeither" | ||
O "github.com/IBM/fp-go/option" | ||
S "github.com/IBM/fp-go/string" | ||
) | ||
|
@@ -104,6 +108,21 @@ var ( | |
|
||
// pureLog :: String -> IO () | ||
pureLog = io.Logf[string]("%s") | ||
|
||
// addToMailingList :: Email -> IOEither([Email]) | ||
addToMailingList = F.Flow2( | ||
A.Of[string], | ||
IOE.Of[error, []string], | ||
) | ||
|
||
// validateEmail :: Email -> Either error Email | ||
validateEmail = E.FromPredicate(Matches(regexp.MustCompile(`\S+@\S+\.\S+`)), errors.OnSome[string]("email %s is invalid")) | ||
|
||
// emailBlast :: [Email] -> IO () | ||
emailBlast = F.Flow2( | ||
A.Intercalate(S.Monoid)(","), | ||
IOE.Of[error, string], | ||
) | ||
) | ||
|
||
func Example_street() { | ||
|
@@ -147,3 +166,21 @@ func Example_solution09B() { | |
// Output: | ||
// ch09.md | ||
} | ||
|
||
func Example_solution09C() { | ||
|
||
// // joinMailingList :: Email -> Either String (IO ()) | ||
joinMailingList := F.Flow4( | ||
validateEmail, | ||
IOE.FromEither[error, string], | ||
IOE.Chain(addToMailingList), | ||
IOE.Chain(emailBlast), | ||
) | ||
|
||
fmt.Println(joinMailingList("[email protected]")()) | ||
fmt.Println(joinMailingList("notanemail")()) | ||
|
||
// Output: | ||
// Right[<nil>, string]([email protected]) | ||
// Left[*errors.errorString, string](email notanemail is invalid) | ||
} |
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
Oops, something went wrong.