-
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 fb91fd5
Showing
5 changed files
with
244 additions
and
6 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
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
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,89 @@ | ||
// Copyright (c) 2023 IBM Corp. | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mostlyadequate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
A "github.com/IBM/fp-go/array" | ||
E "github.com/IBM/fp-go/either" | ||
F "github.com/IBM/fp-go/function" | ||
IOE "github.com/IBM/fp-go/ioeither" | ||
S "github.com/IBM/fp-go/string" | ||
) | ||
|
||
func findUserById(id int) IOE.IOEither[error, Chapter08User] { | ||
switch id { | ||
case 1: | ||
return IOE.Of[error](albert08) | ||
case 2: | ||
return IOE.Of[error](gary08) | ||
case 3: | ||
return IOE.Of[error](theresa08) | ||
default: | ||
return IOE.Left[Chapter08User](fmt.Errorf("user %d not found", id)) | ||
} | ||
} | ||
|
||
func Example_solution11A() { | ||
// eitherToMaybe :: Either b a -> Maybe a | ||
eitherToMaybe := E.ToOption[error, string] | ||
|
||
fmt.Println(eitherToMaybe(E.Of[error]("one eyed willy"))) | ||
fmt.Println(eitherToMaybe(E.Left[string](fmt.Errorf("some error")))) | ||
|
||
// Output: | ||
// Some[string](one eyed willy) | ||
// None[string] | ||
} | ||
|
||
func Example_solution11B() { | ||
findByNameId := F.Flow2( | ||
findUserById, | ||
IOE.Map[error](Chapter08User.getName), | ||
) | ||
|
||
fmt.Println(findByNameId(1)()) | ||
fmt.Println(findByNameId(2)()) | ||
fmt.Println(findByNameId(3)()) | ||
fmt.Println(findByNameId(4)()) | ||
|
||
// Output: | ||
// Right[<nil>, string](Albert) | ||
// Right[<nil>, string](Gary) | ||
// Right[<nil>, string](Theresa) | ||
// Left[*errors.errorString, string](user 4 not found) | ||
} | ||
|
||
func Example_solution11C() { | ||
// strToList :: String -> [Char | ||
strToList := Split(regexp.MustCompile(``)) | ||
|
||
// listToStr :: [Char] -> String | ||
listToStr := A.Intercalate(S.Monoid)("") | ||
|
||
sortLetters := F.Flow3( | ||
strToList, | ||
A.Sort(S.Ord), | ||
listToStr, | ||
) | ||
|
||
fmt.Println(sortLetters("sortme")) | ||
|
||
// Output: | ||
// emorst | ||
} |