maybe

package module
v0.2.0 Latest Latest
Warning

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

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

README

Go Reference Go Report Card

Maybe

Maybe provides an implementation of the maybe type.


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 CatMaybes added in v0.2.0

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

import (
	"fmt"

	"github.com/calebcase/maybe"
)

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

	fmt.Println(maybe.CatMaybes(values))

}
Output:

[1 3]

func FromJust added in v0.2.0

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

func FromMaybe added in v0.2.0

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

func IsJust added in v0.2.0

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

func IsNothing added in v0.2.0

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

func MapMaybes added in v0.2.0

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

import (
	"fmt"
	"strconv"

	"github.com/calebcase/maybe"
)

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

	maybeInt := func(s string) maybe.Type[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 Maybe added in v0.2.0

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

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

Example
package main

import (
	"fmt"

	"github.com/calebcase/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.Type[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.Maybe(false, odd, maybe.Type[int](maybe.Just[int]{3})))
	fmt.Println(maybe.Maybe[int, bool](false, odd, maybe.Just[int]{3}))

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

	// These all produce the desired compile time error (because the types
	// are mismatched):
	//
	//fmt.Println(maybe.Maybe(false, odd, maybe.Type[float32](maybe.Nothing[int]{})))
	//fmt.Println(maybe.Maybe(false, odd, maybe.Type[float32](maybe.Nothing[float32]{})))
	//fmt.Println(maybe.Maybe(false, odd, maybe.Type[int](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.Maybe("unknown", str, maybe.Type[int](maybe.Just[int]{3})))
	fmt.Println(maybe.Maybe("unknown", str, maybe.Type[int](maybe.Just[int]{4})))
	fmt.Println(maybe.Maybe("unknown", str, maybe.Type[int](maybe.Nothing[int]{})))

}
Output:

true
true
false
false
odd
even
unknown

func MaybeToList added in v0.2.0

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

Types

type Just

type Just[T any] struct {
	Value T
}

Just contains a value.

type Nothing

type Nothing[T any] struct{}

Nothing indicates no value is present.

type Type added in v0.2.0

type Type[T any] interface {
	// contains filtered or unexported methods
}

Type is the sum type for maybe.

func ListToMaybe added in v0.2.0

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

Jump to

Keyboard shortcuts

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