Documentation
¶
Overview ¶
Package maybe provides a container type and utilities for optional values.
Index ¶
- func CatMaybes[A any](vs []Type[A]) []A
- func FromJust[A any](v Type[A]) A
- func FromMaybe[A any](dflt A, v Type[A]) A
- func IsJust[A any](v Type[A]) bool
- func IsNothing[A any](v Type[A]) bool
- func MapMaybes[A, B any](f func(A) Type[B], vs []A) (rs []B)
- func Maybe[A, B any](dflt B, f func(a A) B, v Type[A]) B
- func MaybeToList[A any](v Type[A]) []A
- type Just
- type Nothing
- type Type
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CatMaybes ¶ added in v0.2.0
Example ¶
package main
import (
"fmt"
"github.com/calebcase/maybe"
)
func main() {
values := []maybe.Type[int]{
maybe.Just[int]{1},
maybe.Nothing[int]{},
maybe.Just[int]{3},
}
fmt.Println(maybe.CatMaybes(values))
}
Output: [1 3]
func MapMaybes ¶ added in v0.2.0
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/calebcase/maybe"
)
func main() {
values := []string{
"1",
"foo",
"3",
}
maybeInt := func(s string) maybe.Type[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 Maybe ¶ added in v0.2.0
Maybe returns the default value `dflt` if `v` is Nothing. Otherwise it returns the result of calling `f` on `v`.
Example ¶
package main
import (
"fmt"
"github.com/calebcase/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.Type[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.Maybe(false, odd, maybe.Type[int](maybe.Just[int]{3})))
fmt.Println(maybe.Maybe[int, bool](false, odd, maybe.Just[int]{3}))
fmt.Println(maybe.Maybe(false, odd, maybe.Type[int](maybe.Nothing[int]{})))
fmt.Println(maybe.Maybe[int, bool](false, odd, maybe.Nothing[int]{}))
// These all produce the desired compile time error (because the types
// are mismatched):
//
//fmt.Println(maybe.Maybe(false, odd, maybe.Type[float32](maybe.Nothing[int]{})))
//fmt.Println(maybe.Maybe(false, odd, maybe.Type[float32](maybe.Nothing[float32]{})))
//fmt.Println(maybe.Maybe(false, odd, maybe.Type[int](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.Maybe("unknown", str, maybe.Type[int](maybe.Just[int]{3})))
fmt.Println(maybe.Maybe("unknown", str, maybe.Type[int](maybe.Just[int]{4})))
fmt.Println(maybe.Maybe("unknown", str, maybe.Type[int](maybe.Nothing[int]{})))
}
Output: true true false false odd even unknown
func MaybeToList ¶ added in v0.2.0
Types ¶
Click to show internal directories.
Click to hide internal directories.