pipe

package module
v0.0.0-...-8126697 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

README

pipe

Piping functions for Go

Documentation

Overview

Package pipe provides utilities to pipe functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder[T any] func(f func(T) T) Builder[T]

type Pipe

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

Pipe represents the term of the pipeline.

func Each

func Each[In, Out any](p *Pipe[iter.Seq[In]], f func(In) Out) *Pipe[iter.Seq[Out]]

func Each2

func Each2[KIn, VIn, KOut, VOut any](p *Pipe[iter.Seq2[KIn, VIn]], f func(KIn, VIn) (KOut, VOut)) *Pipe[iter.Seq2[KOut, VOut]]

func From

func From[In, Out any](p *Pipe[In], f func(In) Out) *Pipe[Out]

From is like Pipe.Chain except f returns different type.

func TryEach

func TryEach[In, Out any](p *Pipe[iter.Seq[In]], f func(In) (Out, error)) *Pipe[iter.Seq2[Out, error]]

func TryFrom

func TryFrom[In, Out any](p *Pipe[In], f func(In) (Out, error)) *Pipe[Out]

TryFrom is like Pipe.TryChain except f returns different type.

func Value

func Value[T any](v T) *Pipe[T]

Value returns a term.

Example
package main

import (
	"fmt"
	"iter"
	"strings"

	"github.com/lufia/pipe"
)

func tee[T any](v T) T {
	fmt.Println(v)
	return v
}

func require[T ~string](v T) (T, error) {
	if len(v) == 0 {
		return "", fmt.Errorf("zero length")
	}
	return v, nil
}

// After Go 1.23 is released, slices.Values will replace this.
func values[T any](a []T) iter.Seq[T] {
	return func(yield func(T) bool) {
		for _, s := range a {
			if !yield(s) {
				break
			}
		}
	}
}

// After Go 1.23 is released, slices.Collect will replace this.
func collect[T any](seq iter.Seq[T]) []T {
	var a []T
	for v := range seq {
		a = append(a, v)
	}
	return a
}

func main() {
	p1 := pipe.Value("hello world").
		TryChain(require).
		Chain(tee).
		Chain(strings.ToUpper)
	p2 := pipe.From(p1, strings.Fields)
	p3 := pipe.From(p2, values)
	p4 := pipe.Each(p3, func(s string) string {
		return s + "!"
	})
	p5 := pipe.From(p4, collect)
	a, _ := p5.Eval()
	fmt.Println(a)
}
Output:

hello world
[HELLO! WORLD!]
Example (Url)
package main

import (
	"fmt"
	"net/url"

	"github.com/lufia/pipe"
)

func WithPath(s string) func(u *url.URL) *url.URL {
	return func(u *url.URL) *url.URL {
		u.Path = s
		return u
	}
}

func WithParam(k, v string) func(u *url.URL) *url.URL {
	return func(u *url.URL) *url.URL {
		q := u.Query()
		q.Set(k, v)
		u.RawQuery = q.Encode()
		return u
	}
}

func main() {
	u, _ := pipe.TryFrom(pipe.Value("https://example.com"), url.Parse).
		Chain(WithPath("/query")).
		Chain(WithParam("key", "value")).
		Eval()
	fmt.Println(u.String())
}
Output:

https://example.com/query?key=value

func (*Pipe[T]) Chain

func (p *Pipe[T]) Chain(f func(v T) T) *Pipe[T]

func (*Pipe[T]) Defer

func (p *Pipe[T]) Defer(f func(v T)) *Pipe[T]

Defer registers f to cleanup T.

func (*Pipe[T]) Eval

func (p *Pipe[T]) Eval() (T, error)

Eval returns the result of the pipeline. If the pipeline gets an error, it stops the rest of the evaluations and returns that error along with the zero value of T.

func (*Pipe[T]) Pipe

func (p *Pipe[T]) Pipe(f func(v T) T) Builder[T]

func (*Pipe[T]) TryChain

func (p *Pipe[T]) TryChain(f func(v T) (T, error)) *Pipe[T]

Directories

Path Synopsis
Package currying provides utilities for currying.
Package currying provides utilities for currying.

Jump to

Keyboard shortcuts

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