31 lines
550 B
Go
31 lines
550 B
Go
// Package funky provides a few constructs usually present in pure
|
|
// functional programming environments, using Go 1.18 generics.
|
|
package funky
|
|
|
|
// Maybe (sometimes called `Option`)
|
|
|
|
type Maybe[V any] interface {
|
|
IsNothing() bool
|
|
Value() V
|
|
}
|
|
|
|
type maybe[V any] struct {
|
|
isNothing bool
|
|
value V
|
|
}
|
|
|
|
func Nothing[V any]() Maybe[V] {
|
|
return &maybe[V]{isNothing: true}
|
|
}
|
|
|
|
func Just[V any](v V) Maybe[V] {
|
|
return &maybe[V]{false, v}
|
|
}
|
|
|
|
func (m *maybe[V]) IsNothing() bool {
|
|
return m.isNothing
|
|
}
|
|
|
|
func (m *maybe[V]) Value() V {
|
|
return m.value
|
|
}
|