maybe

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package maybe provides a container type and utilities for optional values.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[A, B any](dflt B, f func(a A) B, v Maybe[A]) B

Apply returns the default value `dflt` if `v` is Nothing. Otherwise it returns the result of calling `f` on `v`.

func CatMaybes

func CatMaybes[A any](vs []Maybe[A]) []A
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/maybe"
)

func main() {
	values := []maybe.Maybe[int]{
		maybe.Just[int]{1},
		maybe.Nothing[int]{},
		maybe.Just[int]{3},
	}

	fmt.Println(maybe.CatMaybes(values))

}
Output:

[1 3]

func Conform added in v0.0.5

func Conform[
	A any,

	CA Class[A, A, A],
](c CA) func(t *testing.T, x A)

Conform returns a function testing if the implementation abides by its laws.

func FromJust

func FromJust[A any](v Maybe[A]) A

func FromMaybe

func FromMaybe[A any](dflt A, v Maybe[A]) A

func IsJust

func IsJust[A any](v Maybe[A]) bool

func IsNothing

func IsNothing[A any](v Maybe[A]) bool

func MapMaybes

func MapMaybes[A, B any](f func(A) Maybe[B], vs []A) (rs []B)
Example
package main

import (
	"fmt"
	"strconv"

	"github.com/calebcase/base/data/maybe"
)

func main() {
	values := []string{
		"1",
		"foo",
		"3",
	}

	maybeInt := func(s string) maybe.Maybe[int] {
		i, err := strconv.Atoi(s)
		if err != nil {
			return maybe.Nothing[int]{}
		}

		return maybe.Just[int]{i}
	}

	fmt.Println(maybe.MapMaybes(maybeInt, values))

}
Output:

[1 3]

func MaybeToList

func MaybeToList[A any](v Maybe[A]) []A

Types

type Class

type Class[A, B, C any] interface {
	monad.Class[A, B, C, Maybe[func(A) B], Maybe[A], Maybe[B], Maybe[C]]

	NewJust(A) Just[A]
	NewNothing() Nothing[A]
}

type Just

type Just[T any] struct {
	Value T
}

Just contains a value.

func (Just[T]) DEmpty added in v0.0.6

func (j Just[T]) DEmpty() bool

func (Just[T]) DRest added in v0.0.4

func (j Just[T]) DRest() data.Data[T]

func (Just[T]) DValue added in v0.0.4

func (j Just[T]) DValue() T

type Maybe

type Maybe[T any] interface {
	data.Data[T]
	// contains filtered or unexported methods
}

Maybe is the sum type for maybe.

Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/maybe"
)

func main() {
	// Odd returns true if the integer is odd.
	odd := func(v int) bool {
		return v%2 != 0
	}

	// NOTE: The additional type hinting `maybe.Apply[int](...)` is
	// currently necessary because of a limitation in Go's type
	// inferencing. The hinting may eventually be unnecessary when/if the
	// type inferencing improves for generics. Alternately the types can be
	// explicitly set on the Maybe function instead.
	fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Just[int]{3})))
	fmt.Println(maybe.Apply[int](false, odd, maybe.Just[int]{3}))

	fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Nothing[int]{})))
	fmt.Println(maybe.Apply[int](false, odd, maybe.Nothing[int]{}))

	// These all produce the desired compile time error (because the types
	// are mismatched):
	//
	//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[float32](maybe.Nothing[int]{})))
	//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[float32](maybe.Nothing[float32]{})))
	//fmt.Println(maybe.Apply(false, odd, maybe.Maybe[int](maybe.Just[float32]{3})))
	//fmt.Println(maybe.Apply[int, bool](false, odd, maybe.Just[float32]{3}))

	// str returns the string even or odd for the value.
	str := func(v int) string {
		if v%2 == 0 {
			return "even"
		}

		return "odd"
	}

	fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Just[int]{3})))
	fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Just[int]{4})))
	fmt.Println(maybe.Apply("unknown", str, maybe.Maybe[int](maybe.Nothing[int]{})))

}
Output:

true
true
false
false
odd
even
unknown

func ListToMaybe

func ListToMaybe[A any](vs []A) Maybe[A]

type Nothing

type Nothing[T any] struct{}

Nothing indicates no value is present.

func (Nothing[T]) DEmpty added in v0.0.6

func (n Nothing[T]) DEmpty() bool

func (Nothing[T]) DRest added in v0.0.4

func (n Nothing[T]) DRest() data.Data[T]

func (Nothing[T]) DValue added in v0.0.4

func (n Nothing[T]) DValue() T

type Type

type Type[A, B, C any] struct{}

func NewType

func NewType[A, B, C any]() Type[A, B, C]

func (Type[A, B, C]) Apply

func (t Type[A, B, C]) Apply(f Maybe[func(A) B], m Maybe[A]) Maybe[B]

func (Type[A, B, C]) ApplyL

func (t Type[A, B, C]) ApplyL(x Maybe[A], y Maybe[B]) Maybe[A]

func (Type[A, B, C]) ApplyR

func (t Type[A, B, C]) ApplyR(x Maybe[A], y Maybe[B]) Maybe[B]

func (Type[A, B, C]) Bind

func (t Type[A, B, C]) Bind(x Maybe[A], k func(A) Maybe[B]) Maybe[B]

func (Type[A, B, C]) FMap

func (t Type[A, B, C]) FMap(f func(A) B, v Maybe[A]) Maybe[B]
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/maybe"
)

func main() {
	flip := func(a bool) bool {
		return !a
	}

	flop := maybe.NewType[bool, bool, bool]().FMap(flip, maybe.Just[bool]{true})
	fmt.Println(flop)

	stringify := func(a bool) string {
		return fmt.Sprint(a)
	}

	str := maybe.NewType[bool, string, bool]().FMap(stringify, maybe.Just[bool]{true})
	fmt.Println(str)

	fmt.Println(maybe.NewType[bool, string, bool]().FMap(stringify, maybe.Nothing[bool]{}))

}
Output:

{false}
{true}
{}

func (Type[A, B, C]) FReplace

func (t Type[A, B, C]) FReplace(a A, v Maybe[B]) Maybe[A]
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/maybe"
)

func main() {
	fmt.Println(maybe.NewType[bool, bool, bool]().FReplace(false, maybe.Just[bool]{true}))
	fmt.Println(maybe.NewType[string, bool, bool]().FReplace("done", maybe.Just[bool]{true}))
	fmt.Println(maybe.NewType[string, bool, bool]().FReplace("done", maybe.Nothing[bool]{}))

}
Output:

{false}
{done}
{}

func (Type[A, B, C]) LiftA2

func (t Type[A, B, C]) LiftA2(f func(A, B) C, x Maybe[A], y Maybe[B]) Maybe[C]

func (Type[A, B, C]) NewJust added in v0.0.4

func (t Type[A, B, C]) NewJust(x A) Just[A]

func (Type[A, B, C]) NewNothing added in v0.0.4

func (t Type[A, B, C]) NewNothing() Nothing[A]

func (Type[A, B, C]) Pure

func (t Type[A, B, C]) Pure(x A) Maybe[A]

func (Type[A, B, C]) Return

func (t Type[A, B, C]) Return(x A) Maybe[A]

func (Type[A, B, C]) Then

func (t Type[A, B, C]) Then(x Maybe[A], y Maybe[B]) Maybe[B]

Jump to

Keyboard shortcuts

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