Documentation
¶
Overview ¶
Package maybe implements the Maybe monad for some basic types plus arrays and 2-D arrays of those types.
To keep type names short and manageable, abbreviations are used. Type `maybe.I` is for ints; `maybe.AoI` is short for "array of ints" and `maybe.AoAoI` is short for "array of array of ints".
This package only implements up to 2-D containers because those are common when working with line-oriented data. For example, a text file can be interpreted as an array of an array of characters.
Three constructors are provided for each type. The `Just_` and `Err_` constructors are for values and errors, respectively. The `New_` constructor can construct either type, and is intended for wrapping functions that follow the pattern of returning a value and an error.
Example ¶
Example shows how to convert a list of strings to a list of non-negative integers, accounting for the possibility of failure either in conversion or validation.
//go:build go1.8 // +build go1.8 package main import ( "fmt" "strconv" "github.com/xdg/maybe" ) type example struct { label string data []string } // Example shows how to convert a list of strings to a list of non-negative // integers, accounting for the possibility of failure either in conversion // or validation. func main() { cases := []example{ {label: "success", data: []string{"23", "42", "0"}}, {label: "bad atoi", data: []string{"23", "forty-two", "0"}}, {label: "negative", data: []string{"23", "-42", "0"}}, } // Function to convert string to maybe.I. atoi := func(s string) maybe.I { return maybe.NewI(strconv.Atoi(s)) } // Function to validate non-negative integer. validate := func(x int) maybe.I { if x < 0 { return maybe.ErrI(fmt.Errorf("%d is negative", x)) } return maybe.JustI(x) } // For each example, try converting and validating functionally and // then inspecting the result. for _, c := range cases { // Wrap the []string in a maybe type. strs := maybe.JustAoS(c.data) // Functionally convert and validate. nums := strs.ToInt(atoi).Map(validate) // Check if it worked. if nums.IsErr() { fmt.Printf("%s: %v failed to convert: %v\n", c.label, strs, nums) } else { fmt.Printf("%s: %v converted to %v\n", c.label, strs, nums) } } }
Output: success: Just [23 42 0] converted to Just [23 42 0] bad atoi: Just [23 forty-two 0] failed to convert: Err strconv.Atoi: parsing "forty-two": invalid syntax negative: Just [23 -42 0] failed to convert: Err -42 is negative
Index ¶
- type AoAoI
- func (m AoAoI) Bind(f func(s [][]int) AoAoI) AoAoI
- func (m AoAoI) Flatten() AoI
- func (m AoAoI) IsErr() bool
- func (m AoAoI) Join(f func(s []int) I) AoI
- func (m AoAoI) Map(f func(s []int) AoI) AoAoI
- func (m AoAoI) String() string
- func (m AoAoI) ToStr(f func(x int) S) AoAoS
- func (m AoAoI) Unbox() ([][]int, error)
- type AoAoS
- func (m AoAoS) Bind(f func(s [][]string) AoAoS) AoAoS
- func (m AoAoS) Flatten() AoS
- func (m AoAoS) IsErr() bool
- func (m AoAoS) Join(f func(s []string) S) AoS
- func (m AoAoS) Map(f func(xs []string) AoS) AoAoS
- func (m AoAoS) String() string
- func (m AoAoS) ToInt(f func(s string) I) AoAoI
- func (m AoAoS) Unbox() ([][]string, error)
- type AoAoX
- type AoI
- type AoS
- func (m AoS) Bind(f func(s []string) AoS) AoS
- func (m AoS) IsErr() bool
- func (m AoS) Join(f func(s []string) S) S
- func (m AoS) Map(f func(s string) S) AoS
- func (m AoS) Split(f func(s string) AoS) AoAoS
- func (m AoS) String() string
- func (m AoS) ToInt(f func(s string) I) AoI
- func (m AoS) Unbox() ([]string, error)
- type AoX
- type I
- type S
- type X
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AoAoI ¶
type AoAoI struct {
// contains filtered or unexported fields
}
AoAoI implements the Maybe monad for a 2-D slice of ints. An AoAoI is considered 'valid' or 'invalid' depending on whether it contains a 2-D slice of ints or an error value. A zero-value AoAoI is invalid and Unbox() will return an error to that effect.
func NewAoAoI ¶
NewAoAoI constructs an AoAoI from a given 2-D slice of ints or error. If e is not nil, returns ErrAoAoI(e), otherwise returns JustAoAoI(s).
func (AoAoI) Map ¶
Map applies a function to each element of a valid AoAoI (i.e. a 1-D slice) and returns a new AoAoI. If the AoAoI is invalid or if any function returns an invalid AoI, Map returns an invalid AoAoI.
type AoAoS ¶
type AoAoS struct {
// contains filtered or unexported fields
}
AoAoS implements the Maybe monad for a 2-D slice of strings. An AoAoS is considered 'valid' or 'invalid' depending on whether it contains a 2-D slice of strings or an error value. A zero-value AoAoS is invalid and Unbox() will return an error to that effect.
func NewAoAoS ¶
NewAoAoS constructs an AoAoS from a given 2-D slice of strings or error. If e is not nil, returns ErrAoAoS(e), otherwise returns JustAoAoS(s)
func (AoAoS) Map ¶
Map applies a function to each element of a valid AoAoS (i.e. a 1-D slice) and returns a new AoAoS. If the AoAoS is invalid or if any function returns an invalid AoS, Map returns an invalid AoAoS.
type AoAoX ¶
type AoAoX struct {
// contains filtered or unexported fields
}
AoAoX implements the Maybe monad for a 2-D slice of empty interfaces. An AoAoX is considered 'valid' or 'invalid' depending on whether it contains a 2-D slice of empty interfaces or an error value. A zero-value AoAoX is invalid and Unbox() will return an error to that effect.
func JustAoAoX ¶
func JustAoAoX(x [][]interface{}) AoAoX
JustAoAoX constructs a valid AoAoX from a given 2-D slice of empty interfaces.
func NewAoAoX ¶
NewAoAoX constructs an AoAoX from a given 2-D slice of empty interfaces or error. If e is not nil, returns ErrAoAoX(e), otherwise returns JustAoAoX(x).
func NewAoAoXFromSlice ¶
NewAoAoXFromSlice constructs an AoAoX from a given slice of slices of arbitrary values or error. If e is not nil, returns ErrAoAoX(e), otherwise, the inner slices of values are converted to slices of empty interface and returned as JustAoAoX(x). If the provided value is not a slice of slices, ErrAoAoX is returned.
func (AoAoX) Bind ¶
Bind applies a function that takes a 2-D slice of empty interfaces and returns an AoAoX.
func (AoAoX) Join ¶
Join applies a function that takes a 2-D slice of empty interfaces and returns an AoX.
func (AoAoX) Map ¶
Map applies a function to each element of a valid AoAoX (i.e. a 1-D slice) and returns a new AoAoX. If the AoAoX is invalid or if any function returns an invalid AoX, Map returns an invalid AoAoX.
type AoI ¶
type AoI struct {
// contains filtered or unexported fields
}
AoI implements the Maybe monad for a slice of ints. An AoI is considered 'valid' or 'invalid' depending on whether it contains a slice of ints or an error value. A zero-value AoI is invalid and Unbox() will return an error to that effect.
func NewAoI ¶
NewAoI constructs an AoI from a given slice of ints or error. If e is not nil, returns ErrAoI(e), otherwise returns JustAoI(s).
func (AoI) Map ¶
Map applies a function to each element of a valid AoI and returns a new AoI. If the AoI is invalid or if any function returns an invalid I, Map returns an invalid AoI.
func (AoI) Split ¶
Split applies a splitting function to each element of a valid AoI, resulting in a higher-dimension structure. If the AoI is invalid or if any function returns an invalid AoI, Split returns an invalid AoAoI.
type AoS ¶
type AoS struct {
// contains filtered or unexported fields
}
AoS implements the Maybe monad for a slice of strings. An AoS is considered 'valid' or 'invalid' depending on whether it contains a slice of strings or an error value. A zero-value AoS is invalid and Unbox() will return an error to that effect.
func NewAoS ¶
NewAoS constructs an AoS from a given slice of strings or error. If e is not nil, returns ErrAoS(e), otherwise returns JustAoS(s)
func (AoS) Map ¶
Map applies a function to each element of a valid AoS and returns a new AoS. If the AoS is invalid or if any function returns an invalid S, Map returns an invalid AoS.
func (AoS) Split ¶
Split applies a splitting function to each element of a valid AoS, resulting in a higher-dimension structure. If the AoS is invalid or if any function returns an invalid AoS, Split returns an invalid AoAoS.
type AoX ¶
type AoX struct {
// contains filtered or unexported fields
}
AoX implements the Maybe monad for a slice of empty interfaces. An AoX is considered 'valid' or 'invalid' depending on whether it contains a slice of empty interfaces or an error value. A zero-value AoX is invalid and Unbox() will return an error to that effect.
func JustAoX ¶
func JustAoX(x []interface{}) AoX
JustAoX constructs a valid AoX from a given slice of empty interfaces.
func NewAoX ¶
NewAoX constructs an AoX from a given slice of empty interfaces or error. If e is not nil, returns ErrAoX(e), otherwise returns JustAoX(x).
func NewAoXFromSlice ¶
NewAoXFromSlice constructs an AoX from a given slice of arbitrary values or error. If e is not nil, returns ErrAoX(e), otherwise, the slice of values is converted to a slice of empty interface and returned as JustAoX(x). If the provided value is not a slice, ErrAoX is returned.
func (AoX) Bind ¶
Bind applies a function that takes a slice of empty interfaces and returns an AoX.
func (AoX) Map ¶
Map applies a function to each element of a valid AoX and returns a new AoX. If the AoX is invalid or if any function returns an invalid I, Map returns an invalid AoX.
func (AoX) Split ¶
Split applies a splitting function to each element of a valid AoX, resulting in a higher-dimension structure. If the AoX is invalid or if any function returns an invalid AoX, Split returns an invalid AoAoX.
type I ¶
type I struct {
// contains filtered or unexported fields
}
I implements the Maybe monad for a int. An I is considered 'valid' or 'invalid' depending on whether it contains a int or an error value.
func NewI ¶
NewI constructs an I from a given int or error. If e is not nil, returns ErrI(e), otherwise returns JustI(s)
type S ¶
type S struct {
// contains filtered or unexported fields
}
S implements the Maybe monad for a string. An S is considered 'valid' or 'invalid' depending on whether it contains a string or an error value.
func NewS ¶
NewS constructs an S from a given string or error. If e is not nil, returns ErrS(e), otherwise returns JustS(s)
type X ¶
type X struct {
// contains filtered or unexported fields
}
X implements the Maybe monad for an empty interface. An X is considered 'valid' or 'invalid' depending on whether it contains a non-nil interface or an error value.
func NewX ¶
NewX constructs an X from a given empty interface or error. If e is not nil, returns ErrX(e), otherwise returns JustX(s)