option

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

option is a type that represents optional values.

Example (DefaultParameter)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	add := func(a, b *option.Option[int]) int {
		return a.UnwrapOr(0) + b.UnwrapOr(0)
	}
	r := add(option.Some(4), option.None[int]())
	fmt.Println(r)
}
Output:

4
Example (IAmEven)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(4).AndThen(func(i int) (int, bool) {
		if i%2 == 0 {
			return i, true
		}
		return 0, false
	})
	fmt.Println(a)
}
Output:

4
Example (IAmOdd)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(5).AndThen(func(i int) (int, bool) {
		if i%2 == 0 {
			return 0, false
		}
		return i, true
	})
	fmt.Println(a)
}
Output:

0

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

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

Option is a container for a value of type T.

func None

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

None returns a new Option with no value.

Example (Int)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

0
Example (String)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[string]()
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

""

func Some

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

Some returns a new Option with the given value.

Example (Int)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

1
Example (String)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some("example string")
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

example string

func (*Option[T]) AndThen

func (o *Option[T]) AndThen(fn func(T) (T, bool)) *Option[T]

MapOrElse returns new Option by applying the given function to old Option.

Example (False)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.AndThen(func(i int) (int, bool) {
		return i + 1, false
	})
	c := b.Unwrap()
	fmt.Println(c)
}
Output:

0
Example (True)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.AndThen(func(i int) (int, bool) {
		return i + 1, true
	})
	c := b.Unwrap()
	fmt.Println(c)
}
Output:

2

func (*Option[T]) IsSome

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

IsSome returns true if the Option has a value.

Example (None)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	b := a.IsSome()
	fmt.Println(b)
}
Output:

false
Example (Some)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.IsSome()
	fmt.Println(b)
}
Output:

true

func (*Option[T]) Map

func (o *Option[T]) Map(fn func(T) (T, bool))

Map transforms the Option by applying the given function to the value. if Option does not have a value, do not applying. if given function returns false, Option will be changed to None.

Example (NoneFalse)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	a.Map(func(i int) (int, bool) {
		return i + 1, false
	})
	c := a.Unwrap()
	fmt.Println(c)
}
Output:

0
Example (NoneTrue)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	a.Map(func(i int) (int, bool) {
		return i + 1, true
	})
	c := a.Unwrap()
	fmt.Println(c)
}
Output:

1
Example (SomeFalse)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	a.Map(func(i int) (int, bool) {
		return i + 1, false
	})
	c := a.Unwrap()
	fmt.Println(c)
}
Output:

0
Example (SomeTrue)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	a.Map(func(i int) (int, bool) {
		return i + 1, true
	})
	c := a.Unwrap()
	fmt.Println(c)
}
Output:

2

func (*Option[T]) MapOr

func (o *Option[T]) MapOr(fn func(T) (T, bool), defaultValue T)

MapOr transforms the Option by applying the given function to the value. if Option does not have a value or given function returns false, Option will be have defaultValue.

Example (False)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	a.MapOr(func(i int) (int, bool) {
		return i + 1, false
	}, -99)
	fmt.Println(a)
}
Output:

-99
Example (True)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	a.MapOr(func(i int) (int, bool) {
		return i + 1, true
	}, -99)
	fmt.Println(a)
}
Output:

2

func (*Option[T]) Unwrap

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

Unwrap returns the value of the Option. if Option does not have a value, returns zero value of T.

Example (False)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

0
Example (True)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.Unwrap()
	fmt.Println(b)
}
Output:

1

func (*Option[T]) UnwrapOr

func (o *Option[T]) UnwrapOr(defaultValue T) T

UnwrapOr returns the value of the Option or the given value.

Example (False)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.None[int]()
	b := a.UnwrapOr(99)
	fmt.Println(b)
}
Output:

99
Example (True)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/types/option"
)

func main() {
	a := option.Some(1)
	b := a.UnwrapOr(0)
	fmt.Println(b)
}
Output:

1

Jump to

Keyboard shortcuts

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