Documentation
¶
Overview ¶
Package maybe provides a container type and utilities for optional values.
Index ¶
- func Apply[A, B any](dflt B, f func(a A) B, v Maybe[A]) B
- func CatMaybes[A any](vs []Maybe[A]) []A
- func Conform[A any, CA Class[A, A, A]](c CA) func(t *testing.T, x A)
- func FromJust[A any](v Maybe[A]) A
- func FromMaybe[A any](dflt A, v Maybe[A]) A
- func IsJust[A any](v Maybe[A]) bool
- func IsNothing[A any](v Maybe[A]) bool
- func MapMaybes[A, B any](f func(A) Maybe[B], vs []A) (rs []B)
- func MaybeToList[A any](v Maybe[A]) []A
- type Class
- type Just
- type Maybe
- type Nothing
- type Type
- func (t Type[A, B, C]) Apply(f Maybe[func(A) B], m Maybe[A]) Maybe[B]
- func (t Type[A, B, C]) ApplyL(x Maybe[A], y Maybe[B]) Maybe[A]
- func (t Type[A, B, C]) ApplyR(x Maybe[A], y Maybe[B]) Maybe[B]
- func (t Type[A, B, C]) Bind(x Maybe[A], k func(A) Maybe[B]) Maybe[B]
- func (t Type[A, B, C]) FMap(f func(A) B, v Maybe[A]) Maybe[B]
- func (t Type[A, B, C]) FReplace(a A, v Maybe[B]) Maybe[A]
- func (t Type[A, B, C]) LiftA2(f func(A, B) C, x Maybe[A], y Maybe[B]) Maybe[C]
- func (t Type[A, B, C]) NewJust(x A) Just[A]
- func (t Type[A, B, C]) NewNothing() Nothing[A]
- func (t Type[A, B, C]) Pure(x A) Maybe[A]
- func (t Type[A, B, C]) Return(x A) Maybe[A]
- func (t Type[A, B, C]) Then(x Maybe[A], y Maybe[B]) Maybe[B]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply returns the default value `dflt` if `v` is Nothing. Otherwise it returns the result of calling `f` on `v`.
func CatMaybes ¶
Example ¶
package main
import (
"fmt"
"github.com/calebcase/base/data/maybe"
)
func main() {
values := []maybe.Maybe[int]{
maybe.Just[int]{1},
maybe.Nothing[int]{},
maybe.Just[int]{3},
}
fmt.Println(maybe.CatMaybes(values))
}
Output: [1 3]
func Conform ¶ added in v0.0.5
Conform returns a function testing if the implementation abides by its laws.
func MapMaybes ¶
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/calebcase/base/data/maybe"
)
func main() {
values := []string{
"1",
"foo",
"3",
}
maybeInt := func(s string) maybe.Maybe[int] {
i, err := strconv.Atoi(s)
if err != nil {
return maybe.Nothing[int]{}
}
return maybe.Just[int]{i}
}
fmt.Println(maybe.MapMaybes(maybeInt, values))
}
Output: [1 3]
func MaybeToList ¶
Types ¶
type Maybe ¶
Maybe is the sum type for maybe.
Example ¶
package main
import (
"fmt"
"github.com/calebcase/base/data/maybe"
)
func main() {
// Odd returns true if the integer is odd.
odd := func(v int) bool {
return v%2 != 0
}
// NOTE: The additional type hinting `maybe.Apply[int](...)` is
// currently necessary because of a limitation in Go's type
// inferencing. The hinting may eventually be unnecessary when/if the
// type inferencing improves for generics. Alternately the types can be
// explicitly set on the Maybe function instead.
fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Just[int]{3})))
fmt.Println(maybe.Apply[int](false, odd, maybe.Just[int]{3}))
fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Nothing[int]{})))
fmt.Println(maybe.Apply[int](false, odd, maybe.Nothing[int]{}))
// These all produce the desired compile time error (because the types
// are mismatched):
//
//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[float32](maybe.Nothing[int]{})))
//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[float32](maybe.Nothing[float32]{})))
//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Just[float32]{3})))
//fmt.Println(maybe.Apply[int, bool](false, odd, maybe.Just[float32]{3}))
// str returns the string even or odd for the value.
str := func(v int) string {
if v%2 == 0 {
return "even"
}
return "odd"
}
fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Just[int]{3})))
fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Just[int]{4})))
fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Nothing[int]{})))
}
Output: true true false false odd even unknown
func ListToMaybe ¶
type Type ¶
type Type[A, B, C any] struct{}
func (Type[A, B, C]) FMap ¶
Example ¶
package main
import (
"fmt"
"github.com/calebcase/base/data/maybe"
)
func main() {
flip := func(a bool) bool {
return !a
}
flop := maybe.NewType[bool, bool, bool]().FMap(flip, maybe.Just[bool]{true})
fmt.Println(flop)
stringify := func(a bool) string {
return fmt.Sprint(a)
}
str := maybe.NewType[bool, string, bool]().FMap(stringify, maybe.Just[bool]{true})
fmt.Println(str)
fmt.Println(maybe.NewType[bool, string, bool]().FMap(stringify, maybe.Nothing[bool]{}))
}
Output: {false} {true} {}
func (Type[A, B, C]) FReplace ¶
Example ¶
package main
import (
"fmt"
"github.com/calebcase/base/data/maybe"
)
func main() {
fmt.Println(maybe.NewType[bool, bool, bool]().FReplace(false, maybe.Just[bool]{true}))
fmt.Println(maybe.NewType[string, bool, bool]().FReplace("done", maybe.Just[bool]{true}))
fmt.Println(maybe.NewType[string, bool, bool]().FReplace("done", maybe.Nothing[bool]{}))
}
Output: {false} {done} {}
func (Type[A, B, C]) NewNothing ¶ added in v0.0.4
Click to show internal directories.
Click to hide internal directories.