opt

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: MIT Imports: 1 Imported by: 2

README

go-opt

Package opt implements an optional-type, for the Go programming language.

In other programming languages, an optional-type might be know as: a option type, or a maybe type.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-opt

GoDoc

Examples

Here is an example optional-type that can hold a string:

var op opt.Optional[string] // the default value is nothing.

// ...

if opt.Nothing[string]() == op {
	fmt.Println("nothing")
}

// ...

op = opt.Something("Hello world! 👾")

// ...

switch op {
case op.Nothing[string]():
	//@TODO
case op.Something("apple"):
	//@TODO
case op.Something("banana"):
	//@TODO
case op.Something("cherry"):
	//@TODO
default:
	//@TODO
}

// ...

value, found := op.Get()
if {
	fmt.Println("VALUE:", value)
} else {
	fmt.Println("nothing")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Optional

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

Optional represents an optional value.

In other programming languages this is know as: an option type, or a maybe type.

For example:

var op opt.Optional[string] = opt.Something("once twice thrice fource")

// ...

value, found := op.Get()
if found {
	fmt.Println("value:", value)
} else{
	fmt.Println("nothing")
}

Also, for example:

var op opt.Optional[uint8] = opt.Something[uint8](101)

// ...

value, found := op.Get()
if found {
	fmt.Println("value:", value)
} else{
	fmt.Println("nothing")
}

func Map added in v1.2.0

func Map[T1 any, T2 any](op Optional[T1], fn func(T1) T2) Optional[T2]

Map applies the function ‘fn’ to the value inside of the optional-type ‘op’, if the optional-type ‘op’ is holding something, and returns it as a new optional-type. If the optional-type ‘op’ is holding nothing, then Map also returns nothing.

For example:

var op opt.Optional[string] = opt.Something("Hello world!")

var result opt.Optional[string] = opt.Map(op, strings.ToUpper)

// result == opt.Something[string]("HELLO WORLD!")

// ...

var op2 opt.Optional[string] = opt.Nothing[string]()

var result2 opt.Optional[string] = opt.Map(op, strings.ToUpper)

// result2 == opt.Nothing[string]()

Or also, for example:

fn := func(s string) int {
	return len(s)
}

var op opt.Optional[string] = opt.Something("Hello world!")

var result opt.Optional[int] = opt.Map(op, fn)

// result == opt.Something[int](12)

// ...

var op2 opt.Optional[string] = opt.Nothing[string]()

var result2 opt.Optional[int] = opt.Map(op, fn)

// result2 == opt.Nothing[int]()

func Nothing

func Nothing[T any]() Optional[T]

Nothing returns an optional-type with nothing in it.

For example, here is an optional-type that can hold a string with nothing in it:

var op opt.Optional[string] = opt.Nothing[string]()

Note that the default value for any optional-type is nothing. So the following code it equivalent to it:

var op opt.Optional[string]

Also, for example, here is an optional-type that can hold a uint8 with nothing in it:

var op opt.Optional[uint8] = opt.Nothing[uint8]()

Again, note that the default value for any optional-type is nothing. So the following code it equivalent to it:

var op opt.Optional[uint8]

func Something

func Something[T any](value T) Optional[T]

Something returns a optional-type with something in it.

For example, here is an optional-type with the string "once twice thrice fource" in it:

var op opt.Optional[string] = opt.Something("once twice thrice fource")

And, for example, here is an optional-type with the uint8 101 in it:

var op opt.Optional[uint8] = opt.Something(uint8(101))

func Then added in v1.3.0

func Then[T1 any, T2 any](op Optional[T1], fn func(T1) Optional[T2]) Optional[T2]

Then applies the function ‘fn’ to the value inside of the optional-type ‘op’, if the optional-type ‘op’ is holding something, and returns the resulting optional-type. If the optional-type ‘op’ is holding nothing, then Then also returns nothing.

For example:

fn := func(s string) opt.Optional[byte] {

	if len(s) < 2 {
		return opt.Nothing[byte]()
	}

	return opt.Something[byte](s[1])
}

var op opt.Optional[string] = opt.Something("Hello world!"")

var result opt.Optional[byte] = opt.Then(op, fn)

// result == opt.Something[byte]('e')

// ...

var op2 opt.Optional[string] = opt.Something[string]("X")

var result2 opt.Optional[byte] = opt.Then(op, fn)

// result2 == opt.Nothing[byte]()

// ...

var op2 opt.Optional[string] = opt.Nothing[string]()

var result2 opt.Optional[byte] = opt.Then(op, fn)

// result2 == opt.Nothing[byte]()

func (Optional[T]) Filter added in v1.3.0

func (receiver Optional[T]) Filter(fn func(T) bool) Optional[T]

Filter returns itself if it is holding something and ‘fn’ applied to its value returns true. Else it return nothing.

For example:

fn := func(value int) bool {
	return 0 == (value % 2)
}

// ...

var op1 opt.Optional[int] = opt.Something[int](10)

op1 = op1.Filter(fn)

// ...

var op2 opt.Optional[int] = opt.Something[int](11)

op2 = op2.Filter(fn)

// ...

var op3 opt.Optional[int] = opt.Nothing[int]()

op3 = op3.Filter(fn)

func (Optional[T]) Get

func (receiver Optional[T]) Get() (T, bool)

Get returns the value inside of the optional-type if it is holding something.

Example usage:

var op opt.Optional[string]

// ...

value, found := op.Get()

if found {
	fmt.Println("VALUE:", value)
} else {
	fmt.Println("nothing")
}

func (Optional[T]) GoString

func (receiver Optional[T]) GoString() string

GoString makes it that when the fmt.Fprintf(), fmt.Printf(), and fmt.Sprintf() family of functions renders this type with the %#v verb, that it will be easier to understand.

For example:

var op opt.Optional[string] = opt.Something("once twice thrice fource")

// ...

fmt.Printf("op = %#v", op)

// Output:
// op = opt.Something[string]("once twice thrice fource")

Also, for example:

var op opt.Optional[uint8] = opt.Nothing[uint8]()

// ...

fmt.Printf("op = %#v", op)

// Output:
// op = opt.Nothing[uint8]()

func (Optional[T]) WhenNothing

func (receiver Optional[T]) WhenNothing(fn func())

WhenNothing will call ‘fn’ when ‘receiver’ is holding nothing.

It will not call ‘fn’ when ‘receier’ is hold something.

For example:

var op opt.Optional = opt.Nothing[string]

// ...

op.WhenNothing(func(){
	//@TODO
})

func (Optional[T]) WhenSomething

func (receiver Optional[T]) WhenSomething(fn func(T))

WhenSomething will ‘fn’ when ‘receiver’ is holding something. The value that ‘receiver’ is holding will be passed as a parameter to the function ‘fn’.

It will not call ‘fn’ when ‘receiver’ is hold nothing.

For example:

var op opt.Optional = opt.Something("once twice thrice fource")

// ...

op.WhenSomething(func(value string){
	//@TODO
})

Jump to

Keyboard shortcuts

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