Documentation
¶
Overview ¶
Package optional provides a Maybe type representing an optional value.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Maybe ¶
type Maybe[T any] struct { // contains filtered or unexported fields }
Maybe represents an optional value of type T. A Maybe is either Some (holding a value) or None (holding nothing).
func FlatMap ¶
FlatMap applies fn to the value inside m and returns the resulting Maybe. If m is empty, FlatMap returns an empty Maybe of type R.
func Map ¶
Map applies fn to the value inside m and returns a new Maybe containing the result. If m is empty, Map returns an empty Maybe of type R.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/optional"
)
func main() {
length := func(s string) int { return len(s) }
fmt.Println(optional.Map(optional.Some("hello"), length).MustGet())
fmt.Println(optional.Map(optional.None[string](), length).IsEmpty())
}
Output: 5 true
func None ¶
None returns an empty Maybe of type T.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/optional"
)
func main() {
m := optional.None[string]()
fmt.Println(m.OrElse("default"))
}
Output: default
func Some ¶
Some returns a Maybe containing the given value.
Example ¶
package main
import (
"fmt"
"github.com/mikehelmick/go-functional/optional"
)
func main() {
m := optional.Some("hello")
if v, ok := m.Get(); ok {
fmt.Println(v)
}
}
Output: hello
func (Maybe[T]) Get ¶
Get returns the value and true if present, or the zero value of T and false if empty.
Click to show internal directories.
Click to hide internal directories.