-
Notifications
You must be signed in to change notification settings - Fork 6
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 #3 from OlegStotsky/fix/examples
fix examples to work with go 1.18
- Loading branch information
Showing
8 changed files
with
224 additions
and
40 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OlegStotsky/go-monads/either" | ||
"io" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
func toString(b []byte) string { | ||
return string(b) | ||
} | ||
|
||
func contains(text string) bool { | ||
return strings.Contains(text, "go") | ||
} | ||
|
||
func containsGo(reader io.Reader) (bool, error) { | ||
bytes, err := ioutil.ReadAll(reader) | ||
if err != nil { | ||
return false, err | ||
} | ||
text := toString(bytes) | ||
return contains(text), nil | ||
} | ||
|
||
func ReadAllE(reader io.Reader) either.Either[error, []byte] { | ||
x := either.FromErrorable[[]byte](ioutil.ReadAll(reader)) | ||
return x | ||
} | ||
|
||
func ContainsGoE(reader io.Reader) either.Either[error, bool] { | ||
return either.Map(either.Map(ReadAllE(reader), toString), contains) | ||
} | ||
|
||
func main() { | ||
fmt.Println("first example") | ||
x := either.AsRight(5) | ||
y := either.AsLeft("xyz") | ||
fmt.Println(either.ToMaybe(x).Get()) // prints 5, nil | ||
fmt.Println(either.ToMaybe(y).Get()) // prints <nil> Trying to get from Nothing | ||
|
||
fmt.Println("second example") | ||
r := strings.NewReader("hello world go") | ||
fmt.Println(containsGo(r)) | ||
r.Reset("hello world go") | ||
fmt.Println(either.ToMaybe(ContainsGoE(r)).Get()) | ||
} |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OlegStotsky/go-monads/io" | ||
"github.com/OlegStotsky/go-monads/util" | ||
"log" | ||
"os" | ||
) | ||
|
||
//countNumberOfBytesInFile is an effectfull computation that returns number of bytes in file | ||
func countNumberOfBytesInFile(f *os.File) int64 { | ||
fi, _ := f.Stat() | ||
return fi.Size() | ||
} | ||
|
||
func printVal[T any](val T) io.IO[util.Unit] { | ||
return io.Return[util.Unit](func() util.Unit { | ||
fmt.Println(val) | ||
return util.UnitVar | ||
}) | ||
} | ||
|
||
func main() { | ||
f, err := os.CreateTemp("./", "ab*") | ||
if err != nil { | ||
log.Fatalln(err.Error()) | ||
} | ||
defer os.Remove(f.Name()) | ||
f.WriteString("hello world") | ||
|
||
x := io.Return[int64](func() int64 { return countNumberOfBytesInFile(f) }) | ||
fmt.Println(x.UnsafePerformIO()) | ||
|
||
x2 := io.FlatMap(x, printVal[int64]) | ||
fmt.Println(x2.UnsafePerformIO()) | ||
} |
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,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OlegStotsky/go-monads/maybe" | ||
"strconv" | ||
) | ||
|
||
func main() { | ||
m1 := maybe.Return(5) | ||
fmt.Println(m1.Get()) | ||
|
||
val := new(int) | ||
m2 := maybe.OfNullable[int](val) | ||
fmt.Println(m2.Get()) | ||
|
||
m3 := maybe.Empty[any]() | ||
fmt.Println(m3.Get()) | ||
|
||
m4 := maybe.Return[int](5) | ||
fmt.Println(maybe.Map[int, string](m4, strconv.Itoa).Get()) | ||
|
||
four := maybe.Map(divideBy(6, 3), func(x int) int { return x * 2 }) //four equals Just{obj: 4} | ||
nothing := maybe.Map(divideBy(6, 0), func(x int) int { return x * 2 }) //nothing equals Nothing{} | ||
fmt.Println(four.Get()) | ||
fmt.Println(nothing.Get()) | ||
|
||
x := 0 | ||
y := 7 | ||
m5 := maybe.FlatMap[int, int](divideBy(6, x), func(x int) maybe.Maybe[int] { return divideBy(x, y) }) //x, y are some unknown integers, might be zeros | ||
fmt.Println(m5.Get()) | ||
} | ||
|
||
func divideBy(x int, y int) maybe.Maybe[int] { | ||
if y == 0 { | ||
return maybe.Empty[int]() | ||
} | ||
return maybe.Return(x / y) | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/OlegStotsky/go-monads/state" | ||
"github.com/OlegStotsky/go-monads/util" | ||
) | ||
|
||
func CountZeroes(ints []int) int { | ||
counter := state.Put[int](0) | ||
for _, x := range ints { | ||
if x == 0 { | ||
s := state.BindI[int, util.Unit, int](counter, state.Get[int]()) | ||
counter = state.FlatMap[int, int, util.Unit](s, func(c int) state.State[int, util.Unit] { return state.Put[int](c + 1) }) | ||
} | ||
} | ||
_, s := counter.RunState(0) | ||
return s | ||
} | ||
|
||
func main() { | ||
x := state.Return[any, int](5) | ||
y := state.Put(100) | ||
fmt.Println(x.RunState(nil)) // prints 5 | ||
fmt.Println(y.RunState(6)) // prints 100 | ||
|
||
fmt.Println(CountZeroes([]int{5, 4, 0, 0, 1, 0})) // prints 3 | ||
} |
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
File renamed without changes.
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