pipe

package
v2.0.0 Latest Latest
Warning

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

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

Documentation

Overview

pipe gives a function that can be used to chain functions together.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/snowmerak/generics-for-go/v2/syntax/pipe"
)

func main() {
	funs := pipe.Link[int](func(i int) int {
		return i + 1
	}, func(i int) (int, int) {
		return i, i * i
	}, func(a, b int) string {
		return fmt.Sprintf("%d%d", a, b)
	}, func(s string) int {
		v, err := strconv.Atoi(s)
		if err != nil {
			return 0
		}
		return v
	})
	fmt.Println(funs(10))
}
Output:

11121
Example (WithError)
package main

import (
	"fmt"
	"strconv"

	"github.com/snowmerak/generics-for-go/v2/syntax/pipe"
)

func main() {
	funs := pipe.Link[struct {
		Result int
		Err    error
	}](func(i int) int {
		return i + 1
	}, func(i int) (int, int) {
		return i, i * i
	}, func(a, b int) string {
		return fmt.Sprintf("%d%d!", a, b)
	}, func(s string) (int, error) {
		v, err := strconv.Atoi(s)
		if err != nil {
			return 0, err
		}
		return v, nil
	})
	fmt.Println(funs(10))
}
Output:

{0 strconv.Atoi: parsing "11121!": invalid syntax}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Link[T any](funs ...interface{}) func(in ...interface{}) T

Link is a function that chaining given functions and return a new function. The new function returns type T. if returns error when calling functions of chain, stop the chain and returns T with error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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