Documentation ¶
Index ¶
- Variables
- func FlatMapOr[T, U any](option Option[T], fallbackValue U, mapper func(v T) Option[U]) U
- func FlatMapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (Option[U], error)) (U, error)
- func MapOr[T, U any](option Option[T], fallbackValue U, mapper func(v T) U) U
- func MapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (U, error)) (U, error)
- func Unzip[T, U any](zipped Option[Pair[T, U]]) (Option[T], Option[U])
- func UnzipWith[T, U, V any](zipped Option[V], unzipper func(zipped V) (T, U)) (Option[T], Option[U])
- type Option
- func FlatMap[T, U any](option Option[T], mapper func(v T) Option[U]) Option[U]
- func FlatMapWithError[T, U any](option Option[T], mapper func(v T) (Option[U], error)) (Option[U], error)
- func FromNillable[T any](v *T) Option[T]
- func Map[T, U any](option Option[T], mapper func(v T) U) Option[U]
- func MapWithError[T, U any](option Option[T], mapper func(v T) (U, error)) (Option[U], error)
- func None[T any]() Option[T]
- func PtrFromNillable[T any](v *T) Option[*T]
- func Some[T any](v T) Option[T]
- func Zip[T, U any](opt1 Option[T], opt2 Option[U]) Option[Pair[T, U]]
- func ZipWith[T, U, V any](opt1 Option[T], opt2 Option[U], zipper func(opt1 T, opt2 U) V) Option[V]
- func (o Option[T]) Filter(predicate func(v T) bool) Option[T]
- func (o Option[T]) IfNone(f func())
- func (o Option[T]) IfNoneWithError(f func() error) error
- func (o Option[T]) IfSome(f func(v T))
- func (o Option[T]) IfSomeWithError(f func(v T) error) error
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o Option[T]) Or(fallbackOptionValue Option[T]) Option[T]
- func (o Option[T]) OrElse(fallbackOptionFunc func() Option[T]) Option[T]
- func (o Option[T]) String() string
- func (o Option[T]) Take() (T, error)
- func (o Option[T]) TakeOr(fallbackValue T) T
- func (o Option[T]) TakeOrElse(fallbackFunc func() T) T
- func (o *Option[T]) UnmarshalJSON(data []byte) error
- func (o Option[T]) Unwrap() T
- type Pair
Examples ¶
- FlatMap
- FlatMapOr
- FlatMapOrWithError
- FlatMapWithError
- Map
- MapOr
- MapOrWithError
- MapWithError
- Option.Filter
- Option.IfNone
- Option.IfNoneWithError
- Option.IfSome
- Option.IfSomeWithError
- Option.IsNone
- Option.IsSome
- Option.Or
- Option.OrElse
- Option.Take
- Option.TakeOr
- Option.TakeOrElse
- Option.Unwrap
- Unzip
- UnzipWith
- Zip
- ZipWith
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoneValueTaken represents the error that is raised when None value is taken. ErrNoneValueTaken = errors.New("none value taken") )
Functions ¶
func FlatMapOr ¶ added in v0.4.0
FlatMapOr converts given Option value to another *actual* value according to the mapper function. The difference from the MapOr is the mapper function returns an Option value instead of the bare value. If given Option value is None or mapper function returns None, this returns fallbackValue.
Example ¶
mapper := func(v int) Option[string] { return Some[string](fmt.Sprintf("%d", v)) } some := Some[int](1) mapped := FlatMapOr(some, "N/A", mapper) fmt.Printf("%s\n", mapped) none := None[int]() mapped = FlatMapOr(none, "N/A", mapper) fmt.Printf("%s\n", mapped)
Output: 1 N/A
func FlatMapOrWithError ¶ added in v0.4.0
func FlatMapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (Option[U], error)) (U, error)
FlatMapOrWithError converts given Option value to another *actual* value according to the mapper function that has the ability to return the value with an error. The difference from the MapOrWithError is the mapper function returns an Option value instead of the bare value. If given Option value is None, this returns (fallbackValue, nil). Else if the mapper returns an error then returns ($zero_value_of_type, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (U, nil).
Example ¶
mapperWithNoError := func(v int) (Option[string], error) { return Some[string](fmt.Sprintf("%d", v)), nil } some := Some[int](1) mapped, err := FlatMapOrWithError(some, "N/A", mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped) none := None[int]() mapped, err = FlatMapOrWithError(none, "N/A", mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped) mapperWithError := func(v int) (Option[string], error) { return Some[string]("<ignore-me>"), errors.New("something wrong") } mapped, err = FlatMapOrWithError(some, "N/A", mapperWithError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped)
Output: err is nil: true 1 err is nil: true N/A err is nil: false
func MapOr ¶
MapOr converts given Option value to another *actual* value according to the mapper function. If given Option value is None, this returns fallbackValue.
Example ¶
mapper := func(v int) string { return fmt.Sprintf("%d", v) } some := Some[int](1) mapped := MapOr(some, "N/A", mapper) fmt.Printf("%s\n", mapped) none := None[int]() mapped = MapOr(none, "N/A", mapper) fmt.Printf("%s\n", mapped)
Output: 1 N/A
func MapOrWithError ¶ added in v0.1.0
func MapOrWithError[T, U any](option Option[T], fallbackValue U, mapper func(v T) (U, error)) (U, error)
MapOrWithError converts given Option value to another *actual* value according to the mapper function that has the ability to return the value with an error. If given Option value is None, this returns (fallbackValue, nil). Else if the mapper returns an error then returns (_, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (U, nil).
Example ¶
mapperWithNoError := func(v int) (string, error) { return fmt.Sprintf("%d", v), nil } some := Some[int](1) mapped, err := MapOrWithError(some, "N/A", mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped) none := None[int]() mapped, err = MapOrWithError(none, "N/A", mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped) mapperWithError := func(v int) (string, error) { return "<ignore-me>", errors.New("something wrong") } mapped, err = MapOrWithError(some, "N/A", mapperWithError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", mapped)
Output: err is nil: true 1 err is nil: true N/A err is nil: false <ignore-me>
func Unzip ¶
Unzip extracts the values from a Pair and pack them into each Option value. If the given zipped value is None, this returns None for all return values.
Example ¶
// see also ExampleZip() pair := Pair[int, string]{ Value1: 123, Value2: "foo", } o1, o2 := Unzip(Some[Pair[int, string]](pair)) fmt.Printf("%d\n", o1.TakeOr(0)) fmt.Printf("%s\n", o2.TakeOr("")) o1, o2 = Unzip(None[Pair[int, string]]()) fmt.Printf("is none => %v\n", o1.IsNone()) fmt.Printf("is none => %v\n", o2.IsNone())
Output: 123 foo is none => true is none => true
func UnzipWith ¶
func UnzipWith[T, U, V any](zipped Option[V], unzipper func(zipped V) (T, U)) (Option[T], Option[U])
UnzipWith extracts the values from the given value according to the unzipper function and pack the into each Option value. If the given zipped value is None, this returns None for all return values.
Example ¶
// see also ExampleZipWith() type Data struct { A int B string } unzipper := func(d Data) (int, string) { return d.A, d.B } o1, o2 := UnzipWith(Some[Data](Data{ A: 123, B: "foo", }), unzipper) fmt.Printf("%d\n", o1.TakeOr(0)) fmt.Printf("%s\n", o2.TakeOr("")) o1, o2 = UnzipWith(None[Data](), unzipper) fmt.Printf("is none => %v\n", o1.IsNone()) fmt.Printf("is none => %v\n", o2.IsNone())
Output: 123 foo is none => true is none => true
Types ¶
type Option ¶
type Option[T any] []T
Option is a data type that must be Some (i.e. having a value) or None (i.e. doesn't have a value).
func FlatMap ¶ added in v0.4.0
FlatMap converts give Option value to another Option value according to the mapper function. The difference from the Map is the mapper function returns an Option value instead of the bare value. If given Option value is None, this also returns None.
Example ¶
mapper := func(v int) Option[string] { return Some[string](fmt.Sprintf("%d", v)) } some := Some[int](1) opt := FlatMap(some, mapper) fmt.Printf("%s\n", opt.TakeOr("N/A")) none := None[int]() opt = FlatMap(none, mapper) fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output: 1 N/A
func FlatMapWithError ¶ added in v0.4.0
func FlatMapWithError[T, U any](option Option[T], mapper func(v T) (Option[U], error)) (Option[U], error)
FlatMapWithError converts given Option value to another Option value according to the mapper function that has the ability to return the value with an error. The difference from the MapWithError is the mapper function returns an Option value instead of the bare value. If given Option value is None, this returns (None, nil). Else if the mapper returns an error then this returns (None, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (Some[U], nil).
Example ¶
mapperWithNoError := func(v int) (Option[string], error) { return Some[string](fmt.Sprintf("%d", v)), nil } some := Some[int](1) opt, err := FlatMapWithError(some, mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A")) none := None[int]() opt, err = FlatMapWithError(none, mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A")) mapperWithError := func(v int) (Option[string], error) { return Some[string](""), errors.New("something wrong") } opt, err = FlatMapWithError(some, mapperWithError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output: err is nil: true 1 err is nil: true N/A err is nil: false N/A
func FromNillable ¶ added in v0.7.0
FromNillable is a function to make an Option type value with the nillable value with value de-referencing. If the given value is not nil, this returns Some[T] value. On the other hand, if the value is nil, this returns None[T]. This function does "dereference" for the value on packing that into Option value. If this value is not preferable, please consider using PtrFromNillable() instead.
func Map ¶
Map converts given Option value to another Option value according to the mapper function. If given Option value is None, this also returns None.
Example ¶
mapper := func(v int) string { return fmt.Sprintf("%d", v) } some := Some[int](1) opt := Map(some, mapper) fmt.Printf("%s\n", opt.TakeOr("N/A")) none := None[int]() opt = Map(none, mapper) fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output: 1 N/A
func MapWithError ¶ added in v0.1.0
MapWithError converts given Option value to another Option value according to the mapper function that has the ability to return the value with an error. If given Option value is None, this returns (None, nil). Else if the mapper returns an error then this returns (None, error). Unless of them, i.e. given Option value is Some and the mapper doesn't return the error, this returns (Some[U], nil).
Example ¶
mapperWithNoError := func(v int) (string, error) { return fmt.Sprintf("%d", v), nil } some := Some[int](1) opt, err := MapWithError(some, mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A")) none := None[int]() opt, err = MapWithError(none, mapperWithNoError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A")) mapperWithError := func(v int) (string, error) { return "", errors.New("something wrong") } opt, err = MapWithError(some, mapperWithError) fmt.Printf("err is nil: %v\n", err == nil) fmt.Printf("%s\n", opt.TakeOr("N/A"))
Output: err is nil: true 1 err is nil: true N/A err is nil: false N/A
func PtrFromNillable ¶ added in v0.7.0
PtrFromNillable is a function to make an Option type value with the nillable value without value de-referencing. If the given value is not nil, this returns Some[*T] value. On the other hand, if the value is nil, this returns None[*T]. This function doesn't "dereference" the value on packing that into the Option value; in other words, this puts the as-is pointer value into the Option envelope. This behavior contrasts with the FromNillable() function's one.
func Zip ¶
Zip zips two Options into a Pair that has each Option's value. If either one of the Options is None, this also returns None.
Example ¶
maybePair := Zip(Some[int](1), Some[string]("foo")) pair, err := maybePair.Take() fmt.Printf("is none => %v\n", maybePair.IsNone()) fmt.Printf("err is nil => %v\n", err == nil) fmt.Printf("%d %s\n", pair.Value1, pair.Value2) maybePair = Zip(Some[int](1), None[string]()) fmt.Printf("is none => %v\n", maybePair.IsNone()) maybePair = Zip(None[int](), Some[string]("foo")) fmt.Printf("is none => %v\n", maybePair.IsNone()) maybePair = Zip(None[int](), None[string]()) fmt.Printf("is none => %v\n", maybePair.IsNone())
Output: is none => false err is nil => true 1 foo is none => true is none => true is none => true
func ZipWith ¶
ZipWith zips two Options into a typed value according to the zipper function. If either one of the Options is None, this also returns None.
Example ¶
type Data struct { A int B string } zipper := func(v1 int, v2 string) Data { return Data{ A: v1, B: v2, } } maybeData := ZipWith(Some[int](1), Some[string]("foo"), zipper) fmt.Printf("is none => %v\n", maybeData.IsNone()) d, err := maybeData.Take() fmt.Printf("err is nil => %v\n", err == nil) fmt.Printf("%d %s\n", d.A, d.B) maybeData = ZipWith(Some[int](1), None[string](), zipper) fmt.Printf("is none => %v\n", maybeData.IsNone()) maybeData = ZipWith(None[int](), Some[string]("foo"), zipper) fmt.Printf("is none => %v\n", maybeData.IsNone()) maybeData = ZipWith(None[int](), None[string](), zipper) fmt.Printf("is none => %v\n", maybeData.IsNone())
Output: is none => false err is nil => true 1 foo is none => true is none => true is none => true
func (Option[T]) Filter ¶
Filter returns self if the Option has a value and the value matches the condition of the predicate function. In other cases (i.e. it doesn't match with the predicate or the Option is None), this returns None value.
Example ¶
isEven := func(v int) bool { return v%2 == 0 } some := Some[int](2) opt := some.Filter(isEven) fmt.Printf("%d\n", opt.TakeOr(0)) some = Some[int](1) opt = some.Filter(isEven) fmt.Printf("%d\n", opt.TakeOr(0)) none := None[int]() opt = none.Filter(isEven) fmt.Printf("%d\n", opt.TakeOr(0))
Output: 2 0 0
func (Option[T]) IfNone ¶ added in v0.2.0
func (o Option[T]) IfNone(f func())
IfNone calls given function if the receiver value is None.
Example ¶
None[string]().IfNone(func() { fmt.Println("value is none") }) Some("foo").IfNone(func() { fmt.Println("do not show this message") })
Output: value is none
func (Option[T]) IfNoneWithError ¶ added in v0.2.0
IfNoneWithError calls given function if the receiver value is None. This method propagates the error of given function, and if the receiver value is Some, this returns nil error.
Example ¶
err := None[string]().IfNoneWithError(func() error { fmt.Println("value is none") return nil }) if err != nil { fmt.Println(err) // no error } err = None[string]().IfNoneWithError(func() error { fmt.Println("value is none!!") return errors.New("^^^ error occurred") }) if err != nil { fmt.Println(err) } err = Some("foo").IfNoneWithError(func() error { return errors.New("do not show this error") }) if err != nil { fmt.Println(err) // must not show this error }
Output: value is none value is none!! ^^^ error occurred
func (Option[T]) IfSome ¶ added in v0.2.0
func (o Option[T]) IfSome(f func(v T))
IfSome calls given function with the value of Option if the receiver value is Some.
Example ¶
Some("foo").IfSome(func(val string) { fmt.Println(val) }) None[string]().IfSome(func(val string) { fmt.Println("do not show this message") })
Output: foo
func (Option[T]) IfSomeWithError ¶ added in v0.2.0
IfSomeWithError calls given function with the value of Option if the receiver value is Some. This method propagates the error of given function, and if the receiver value is None, this returns nil error.
Example ¶
err := Some("foo").IfSomeWithError(func(val string) error { fmt.Println(val) return nil }) if err != nil { fmt.Println(err) // no error } err = Some("bar").IfSomeWithError(func(val string) error { fmt.Println(val) return errors.New("^^^ error occurred") }) if err != nil { fmt.Println(err) } err = None[string]().IfSomeWithError(func(val string) error { return errors.New("do not show this error") }) if err != nil { fmt.Println(err) // must not show this error }
Output: foo bar ^^^ error occurred
func (Option[T]) IsNone ¶
IsNone returns whether the Option *doesn't* have a value or not.
Example ¶
some := Some[int](1) fmt.Printf("%v\n", some.IsNone()) none := None[int]() fmt.Printf("%v\n", none.IsNone()) num := 123 some = FromNillable[int](&num) fmt.Printf("%v\n", some.IsNone()) none = FromNillable[int](nil) fmt.Printf("%v\n", none.IsNone()) ptrSome := PtrFromNillable[int](&num) fmt.Printf("%v\n", ptrSome.IsNone()) ptrNone := PtrFromNillable[int](nil) fmt.Printf("%v\n", ptrNone.IsNone()) var nilValue Option[int] = nil fmt.Printf("%v\n", nilValue.IsNone())
Output: false true false true false true true
func (Option[T]) IsSome ¶
IsSome returns whether the Option has a value or not.
Example ¶
some := Some[int](1) fmt.Printf("%v\n", some.IsSome()) none := None[int]() fmt.Printf("%v\n", none.IsSome()) num := 123 some = FromNillable[int](&num) fmt.Printf("%v\n", some.IsSome()) none = FromNillable[int](nil) fmt.Printf("%v\n", none.IsSome()) ptrSome := PtrFromNillable[int](&num) fmt.Printf("%v\n", ptrSome.IsSome()) ptrNone := PtrFromNillable[int](nil) fmt.Printf("%v\n", ptrNone.IsSome()) var nilValue Option[int] = nil fmt.Printf("%v\n", nilValue.IsSome())
Output: true false true false true false false
func (Option[T]) MarshalJSON ¶ added in v0.5.0
func (Option[T]) Or ¶ added in v0.10.0
Or returns the Option value according to the actual value existence. If the receiver's Option value is Some, this function pass-through that to return. Otherwise, this value returns the `fallbackOptionValue`.
Example ¶
fallback := Some[string]("fallback") some := Some[string]("actual") fmt.Printf("%s\n", some.Or(fallback)) none := None[string]() fmt.Printf("%s\n", none.Or(fallback))
Output: Some[actual] Some[fallback]
func (Option[T]) OrElse ¶ added in v0.9.0
OrElse returns the Option value according to the actual value existence. If the receiver's Option value is Some, this function pass-through that to return. Otherwise, this executes `fallbackOptionFunc` and returns the result value of that function.
Example ¶
fallbackFunc := func() Option[string] { return Some[string]("fallback") } some := Some[string]("actual") fmt.Printf("%s\n", some.OrElse(fallbackFunc)) none := None[string]() fmt.Printf("%s\n", none.OrElse(fallbackFunc))
Output: Some[actual] Some[fallback]
func (Option[T]) Take ¶
Take takes the contained value in Option. If Option value is Some, this returns the value that is contained in Option. On the other hand, this returns an ErrNoneValueTaken as the second return value.
Example ¶
some := Some[int](1) v, err := some.Take() fmt.Printf("%d\n", v) fmt.Printf("%v\n", err == nil) none := None[int]() _, err = none.Take() fmt.Printf("%v\n", err == nil)
Output: 1 true false
func (Option[T]) TakeOr ¶
func (o Option[T]) TakeOr(fallbackValue T) T
TakeOr returns the actual value if the Option has a value. On the other hand, this returns fallbackValue.
Example ¶
some := Some[int](1) v := some.TakeOr(666) fmt.Printf("%d\n", v) none := None[int]() v = none.TakeOr(666) fmt.Printf("%d\n", v)
Output: 1 666
func (Option[T]) TakeOrElse ¶
func (o Option[T]) TakeOrElse(fallbackFunc func() T) T
TakeOrElse returns the actual value if the Option has a value. On the other hand, this executes fallbackFunc and returns the result value of that function.
Example ¶
some := Some[int](1) v := some.TakeOrElse(func() int { return 666 }) fmt.Printf("%d\n", v) none := None[int]() v = none.TakeOrElse(func() int { return 666 }) fmt.Printf("%d\n", v)
Output: 1 666
func (*Option[T]) UnmarshalJSON ¶ added in v0.5.0
func (Option[T]) Unwrap ¶ added in v0.3.0
func (o Option[T]) Unwrap() T
Unwrap returns the value regardless of Some/None status. If the Option value is Some, this method returns the actual value. On the other hand, if the Option value is None, this method returns the *default* value according to the type.
Example ¶
fmt.Printf("%v\n", Some[int](12345).Unwrap()) fmt.Printf("%v\n", None[int]().Unwrap()) fmt.Printf("%v\n", None[*int]().Unwrap()) num := 123 fmt.Printf("%v\n", FromNillable[int](&num).Unwrap()) fmt.Printf("%v\n", FromNillable[int](nil).Unwrap()) fmt.Printf("%v\n", *PtrFromNillable[int](&num).Unwrap()) // NOTE: this dereferences tha unwrapped value fmt.Printf("%v\n", PtrFromNillable[int](nil).Unwrap())
Output: 12345 0 <nil> 123 0 123 <nil>