value

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: BSD-3-Clause Imports: 1 Imported by: 7

Documentation

Overview

Package value defines adapters for value types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func At added in v0.6.1

func At[T any](p *T) T

At returns the value pointed to by p, or zero if p == nil.

func AtDefault added in v0.6.1

func AtDefault[T any](p *T, dflt T) T

AtDefault returns the value pointed to by p, or dflt if p == nil.

func Cond added in v0.14.4

func Cond[T any](b bool, x, y T) T

Cond returns x if b is true, otherwise it returns y.

func Ptr added in v0.6.0

func Ptr[T any](v T) *T

Ptr returns a pointer to its argument type containing v.

Types

type Maybe added in v0.18.0

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

Maybe is a container that can hold a value of type T. Just(v) returns a Maybe holding the value v. Absent() returns a Maybe that holds no value. A zero Maybe is ready for use and is equivalent to Absent().

It is safe to copy and assign a Maybe value, but note that if a value is present, only a shallow copy of the underlying value is made. Maybe values are comparable if and only if T is comparable.

Example
package main

import (
	"fmt"

	"github.com/creachadair/mds/value"
)

var randomValues = []int{1, 6, 16, 19, 4}

func main() {
	even := make([]value.Maybe[int], 5)
	for i, r := range randomValues {
		if r%2 == 0 {
			even[i] = value.Just(r)
		}
	}

	var count int
	for _, v := range even {
		if v.Present() {
			count++
		}
	}

	fmt.Println("input:", randomValues)
	fmt.Println("result:", even)
	fmt.Println("count:", count)
}
Output:

input: [1 6 16 19 4]
result: [Absent[int] 6 16 Absent[int] 4]
count: 3

func Absent added in v0.18.0

func Absent[T any]() Maybe[T]

Absent returns a Maybe holding no value. A zero Maybe is equivalent to Absent().

func AtMaybe added in v0.18.1

func AtMaybe[T any](p *T) Maybe[T]

AtMaybe returns Just(*p) if p != nil, or otherwise Absent().

func Check added in v0.18.0

func Check[T any](v T, err error) Maybe[T]

Check returns Just(v) if err == nil; otherwise it returns Absent().

func Just added in v0.18.0

func Just[T any](v T) Maybe[T]

Just returns a Maybe holding the value v.

func (Maybe[T]) Get added in v0.18.0

func (m Maybe[T]) Get() T

Get returns value held in m, if present; otherwise it returns the zero of T.

func (Maybe[T]) GetOK added in v0.18.0

func (m Maybe[T]) GetOK() (T, bool)

GetOK reports whether m holds a value, and if so returns that value. If m is empty, GetOK returns the zero of T.

func (Maybe[T]) Or added in v0.18.0

func (m Maybe[T]) Or(o T) Maybe[T]

Or returns m if m holds a value; otherwise it returns Just(o).

func (Maybe[T]) Present added in v0.18.0

func (m Maybe[T]) Present() bool

Present reports whether m holds a value.

func (Maybe[T]) Ptr added in v0.18.1

func (m Maybe[T]) Ptr() *T

Ptr converts m to a pointer. It returns nil if m is empty, otherwise it returns a pointer to a location containing the value held in m.

func (Maybe[T]) String added in v0.18.0

func (m Maybe[T]) String() string

String returns the string representation of m. If m holds a value v, the string representation of m is that of v.

Jump to

Keyboard shortcuts

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