chain

package
v1.6.2 Latest Latest
Warning

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

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

README

Chain

chain is a library for function chaning.

how to use

From
func main() {
	ch := chain.From[int, int](
		func(a int) int {
			return a + 1
		},
		func(a int) int {
			return a * 2
		},
		func(a int) int {
			return a * 3
		})

	switch ch.Ok() {
	case true:
		println(ch.Unwrap().Run(1))
	case false:
		println(ch.Err().Error())
	}
}
12
with wrong input & output matching
func main() {
	ch := chain.From[int, string](
		func(a int) int {
			return a + 1
		},
		func(a int) int {
			return a * 2
		},
		func(a int) int {
			return a * 3
		})

	switch ch.Ok() {
	case true:
		println(ch.Unwrap().Run(1))
	case false:
		println(ch.Err().Error())
	}
}
function's return type is invalid
WithResult
func main() {
	ch := chain.WithResult[int, int](
		result.ToFunctor(func(x int) (int, error) {
			return x + 1, nil
		}),
		result.ToFunctor(func(x int) (int, error) {
			return x * 2, nil
		}),
		result.ToFunctor(func(x int) (int, error) {
			return x + 1, nil
		}),
	)

	switch ch.Ok() {
	case true:
		rs := ch.Unwrap().Run(result.Ok(10))
		switch rs.Ok() {
		case true:
			println(rs.Unwrap())
		case false:
			println(rs.Err().Error())
		}
	case false:
		println(ch.Err().Error())
	}
}
23

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From[T, R any](list ...any) *result.Result[*Chain[T, R]]

func WithResult

func WithResult[T, R any](list ...any) *result.Result[*Chain[*result.Result[T], *result.Result[R]]]

Types

type Chain

type Chain[T, R any] struct {
	// contains filtered or unexported fields
}

func (*Chain[T, R]) Run

func (c *Chain[T, R]) Run(param T) R

Jump to

Keyboard shortcuts

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