optional

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2020 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrAbsent used to panic when call Optional.Get on empty Optional
	ErrAbsent = errors.New("absent value")
	// ErrNil used to panic when input a nil value to the Of method
	ErrNil = errors.New("nil value")
)

Functions

func IsNil

func IsNil(value types.T) bool

IsNil used to determine a value whether is nil or not

Types

type Optional

type Optional interface {
	Get() types.T                                  // Get the value or panic when no value
	IsPresent() bool                               // IsPresent return true is value exist
	IfPresent(types.Consumer)                      // IfPresent will invoke the Consumer if value exist
	Filter(types.Predicate) Optional               // Filter: if has value, and the value matches the given Predicate return a Present Optional, or else return Empty
	Map(types.Function) Optional                   // Map: if has value, apply the given Function to it, or else return Empty
	FlatMap(func(t types.T) Optional) Optional     // Flatmap: if has value, apply the given flatten-Function and return the result, or else return Empty
	OrElse(types.T) types.T                        // OrElse: if absent, return the given value. if value present, return it
	OrElseGet(types.Supplier) types.T              // OrElseGet: if absent, call Supplier and return it's result. if value present return it
	OrPanic(panicArg interface{}) types.T          // OrPanic:if absent, panic with `panicArg`, if value present, return it
	OrPanicGet(getPanicArg types.Supplier) types.T // OrPanicGet: if absent, panic with the given supplier's result. if value present, return it
}

Optional is a container which may or may not contain a non-nil value. If value is not nil, IsPresent will return true, and Get will return the value.

func Empty

func Empty() Optional

Empty return a absent optional

Example
package main

import (
	"errors"
	"fmt"

	"github.com/youthlin/stream/optional"
	"github.com/youthlin/stream/types"
)

func main() {
	fmt.Println(optional.Empty().IsPresent()) // false
	optional.Empty().IfPresent(func(t types.T) {
		fmt.Println("This will not print")
	})
	fmt.Printf("%#v\n", optional.Empty().Filter(func(t types.T) bool {
		fmt.Println("Filter: Not Reach")
		return true
	}).Map(func(t types.T) types.R {
		fmt.Println("Map: Not Reach")
		return t
	}).FlatMap(func(t types.T) optional.Optional {
		fmt.Println("FlatMap: Not Reach")
		return optional.Empty()
	}).OrElse(1)) // 1
	fmt.Println(optional.Empty().OrElseGet(func() types.T {
		return "else"
	}))
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
		defer func() {
			if err := recover(); err != nil {
				fmt.Println(err)
				defer func() {
					if err := recover(); err != nil {
						fmt.Printf("OrPanicGet:%+v\n", err)
					}
				}()
				optional.Empty().OrPanicGet(func() types.T {
					return errors.New("err_msg")
				})
			}
		}()
		optional.Empty().OrPanic("error_msg")
	}()
	optional.Empty().Get()
}
Output:
false
1
else
absent value
error_msg
OrPanicGet:err_msg

func Of

func Of(value types.T) Optional

Of returns a present optional, the input value must not be nil, or it will panic

Example
package main

import (
	"fmt"

	"github.com/youthlin/stream/optional"
	"github.com/youthlin/stream/types"
)

func main() {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(err)
		}
		fmt.Println(optional.Of(1).IsPresent()) // true
		fmt.Println(optional.Of(1).Filter(func(t types.T) bool {
			return t.(int) > 1
		}).IsPresent()) // false
		fmt.Println(optional.Of(1).Filter(func(t types.T) bool {
			return t.(int) == 1
		}).IsPresent()) // true
		fmt.Println(optional.Of(1).Map(func(t types.T) types.R {
			return fmt.Sprintf("<%d>", t)
		}).Get())
		optional.Of(1).FlatMap(optional.Of).IfPresent(func(t types.T) {
			fmt.Printf("FlatMap: %d\n", t)
		})
		fmt.Printf("OrElse: %d\n", optional.Of(1).OrElse(0))
		fmt.Printf("OrElseGet: %d\n", optional.Of(1).OrElseGet(func() types.T {
			return 0
		}))
		fmt.Printf("OrPanic: %d\n", optional.Of(1).OrPanic("boom"))

		defer func() {
			if err := recover(); err != nil {
				fmt.Println(err)
			}
		}()
		var s *string = nil
		optional.Of(s).IfPresent(func(t types.T) {
			fmt.Printf("%v\n", t)
		})
	}()
	of := optional.Of(nil)
	fmt.Println(of)
}
Output:
nil value
true
false
true
<1>
FlatMap: 1
OrElse: 1
OrElseGet: 1
OrPanic: 1
nil value

func OfNullable

func OfNullable(value types.T) Optional

OfNullable return a absent or present optional depends on the input value is nil or false

Example
package main

import (
	"fmt"

	"github.com/youthlin/stream/optional"
)

func main() {
	fmt.Printf("nil IsPresent: %t\n", optional.OfNullable(nil).IsPresent())
	fmt.Printf("nil(*string) IsPresent: %t\n", optional.OfNullable((*string)(nil)).IsPresent())
	fmt.Printf("IsPresent: %t\n", optional.OfNullable(1).IsPresent())
}
Output:
nil IsPresent: false
nil(*string) IsPresent: false
IsPresent: true

Jump to

Keyboard shortcuts

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