option

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2023 License: MIT Imports: 1 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map added in v0.7.0

func Map[T any, U any](fn func(T) U) func(Option[T]) Option[U]

Map allows functions to work with option types, the returned closure will only invoke fn if Option[T] is a Some variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	double := option.Map(func(x int) int {
		return x * 2
	})

	fmt.Println(double(option.None[int]()))
	fmt.Println(double(option.Some(25)))

}
Output:

None
Some(50)

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option represents an optional value. The Some variant contains a value and the None variant represents the absence of a value.

func None

func None[T any]() Option[T]

None instantiates an Option with no value.

func Some

func Some[T any](value T) Option[T]

Some instantiates an Option with a value.

func (Option[T]) IsNone

func (o Option[T]) IsNone() bool

IsNone returns true if the Option does not contain a value.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).IsNone())
	fmt.Println(option.None[int]().IsNone())

}
Output:

false
true

func (Option[T]) IsSome

func (o Option[T]) IsSome() bool

IsSome returns true if the Option contains a value.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).IsSome())
	fmt.Println(option.None[int]().IsSome())

}
Output:

true
false

func (Option[T]) String

func (o Option[T]) String() string

func (Option[T]) Unwrap

func (o Option[T]) Unwrap() T

Unwrap returns the underlying value of a Some variant, or panics if called on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).Unwrap())
}
Output:

4

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(value T) T

UnwrapOr returns the underlying value of a Some variant, or the provided value on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOr(3))
	fmt.Println(option.None[int]().UnwrapOr(3))
}
Output:

4
3

func (Option[T]) UnwrapOrElse

func (o Option[T]) UnwrapOrElse(f func() T) T

UnwrapOrElse returns the underlying value of a Some variant, or the result of calling the provided function on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOrElse(func() int {
		return 3
	}))

	fmt.Println(option.None[int]().UnwrapOrElse(func() int {
		return 3
	}))

}
Output:

4
3

func (Option[T]) UnwrapOrZero

func (o Option[T]) UnwrapOrZero() T

UnwrapOrZero returns the underlying value of a Some variant, or the zero value on a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	fmt.Println(option.Some(4).UnwrapOrZero())
	fmt.Println(option.None[int]().UnwrapOrZero())

	// Output
	// 4
	// 0
}

func (Option[T]) Value

func (o Option[T]) Value() (T, bool)

Value returns the underlying value and true for a Some variant, or the zero value and false for a None variant.

Example
package main

import (
	"fmt"

	"github.com/BooleanCat/go-functional/option"
)

func main() {
	value, ok := option.Some(4).Value()
	fmt.Println(value)
	fmt.Println(ok)

}
Output:

4
true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL