writer

package
v1.0.148 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ap

func Ap[B, A, W any](s SG.Semigroup[W], fa Writer[W, A]) func(Writer[W, func(A) B]) Writer[W, B]

func ApS added in v1.0.107

func ApS[S1, S2, T, W any](
	s SG.Semigroup[W],
	setter func(T) func(S1) S2,
	fa Writer[W, T],
) func(Writer[W, S1]) Writer[W, S2]

ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently

func Applicative added in v1.0.129

func Applicative[W, A, B any](m M.Monoid[W]) applicative.Applicative[A, B, Writer[W, A], Writer[W, B], Writer[W, func(A) B]]

Applicative implements the applicative operations for Writer

func Bind added in v1.0.107

func Bind[S1, S2, T, W any](
	s SG.Semigroup[W],
	setter func(T) func(S1) S2,
	f func(S1) Writer[W, T],
) func(Writer[W, S1]) Writer[W, S2]

Bind attaches the result of a computation to a context [S1] to produce a context [S2]

func BindTo added in v1.0.107

func BindTo[W, S1, T any](
	setter func(T) S1,
) func(Writer[W, T]) Writer[W, S1]

BindTo initializes a new state [S1] from a value [T]

func Censor

func Censor[A any, FCT ~func(W) W, W any](f FCT) func(Writer[W, A]) Writer[W, A]

Censor modifies the final accumulator value by applying a function

func Chain

func Chain[A, B, W any](s SG.Semigroup[W], fa func(A) Writer[W, B]) func(Writer[W, A]) Writer[W, B]

func ChainFirst

func ChainFirst[FCT ~func(A) Writer[W, B], W, A, B any](s SG.Semigroup[W], fct FCT) func(Writer[W, A]) Writer[W, A]

func Eq

func Eq[W, A any](w EQ.Eq[W], a EQ.Eq[A]) EQ.Eq[Writer[W, A]]

Constructs an equal predicate for a Writer

func Evaluate

func Evaluate[W, A any](fa Writer[W, A]) A

Evaluate extracts the value

func Execute

func Execute[W, A any](fa Writer[W, A]) W

Execute extracts the accumulator

func Flap added in v1.0.110

func Flap[W, B, A any](a A) func(Writer[W, func(A) B]) Writer[W, B]

func FromStrictEquals

func FromStrictEquals[W, A comparable]() EQ.Eq[Writer[W, A]]

FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function

func Functor added in v1.0.129

func Functor[W, A, B any]() functor.Functor[A, B, Writer[W, A], Writer[W, B]]

Functor implements the pointed operations for Writer

func Let added in v1.0.107

func Let[W, S1, S2, T any](
	setter func(T) func(S1) S2,
	f func(S1) T,
) func(Writer[W, S1]) Writer[W, S2]

Let attaches the result of a computation to a context [S1] to produce a context [S2]

func LetTo added in v1.0.107

func LetTo[W, S1, S2, T any](
	setter func(T) func(S1) S2,
	b T,
) func(Writer[W, S1]) Writer[W, S2]

LetTo attaches the a value to a context [S1] to produce a context [S2]

func Listens

func Listens[A any, FCT ~func(W) B, W, B any](f FCT) func(Writer[W, A]) Writer[W, P.Pair[A, B]]

Listens projects a value from modifications made to the accumulator during an action

func Map

func Map[W any, FCT ~func(A) B, A, B any](f FCT) func(Writer[W, A]) Writer[W, B]

func Monad added in v1.0.129

func Monad[W, A, B any](m M.Monoid[W]) monad.Monad[A, B, Writer[W, A], Writer[W, B], Writer[W, func(A) B]]

Monad implements the monadic operations for Writer

func Pointed added in v1.0.129

func Pointed[W, A any](m M.Monoid[W]) pointed.Pointed[A, Writer[W, A]]

Pointed implements the pointed operations for Writer

Types

type Writer

type Writer[W, A any] IO.IO[P.Pair[A, W]]
Example (Logging)
package main

import (
	"fmt"

	A "github.com/IBM/fp-go/array"
	F "github.com/IBM/fp-go/function"
	P "github.com/IBM/fp-go/pair"
)

func doubleAndLog(data int) Writer[[]string, int] {
	return func() P.Pair[int, []string] {
		result := data * 2
		return P.MakePair(result, A.Of(fmt.Sprintf("Doubled %d -> %d", data, result)))
	}
}

func main() {

	res := F.Pipe3(
		Of[int](monoid, 10),
		Chain(sg, doubleAndLog),
		Chain(sg, doubleAndLog),
		Execute[[]string, int],
	)

	fmt.Println(res)

}
Output:

[Doubled 10 -> 20 Doubled 20 -> 40]

func Do added in v1.0.107

func Do[S, W any](m M.Monoid[W], s S) Writer[W, S]

Bind creates an empty context of type [S] to be used with the Bind operation

func Flatten

func Flatten[W, A any](s SG.Semigroup[W], mma Writer[W, Writer[W, A]]) Writer[W, A]

func Listen added in v1.0.107

func Listen[W, A any](fa Writer[W, A]) Writer[W, P.Pair[A, W]]

Listen modifies the result to include the changes to the accumulator

func MonadAp

func MonadAp[B, A, W any](s SG.Semigroup[W], fab Writer[W, func(A) B], fa Writer[W, A]) Writer[W, B]

func MonadCensor

func MonadCensor[A any, FCT ~func(W) W, W any](fa Writer[W, A], f FCT) Writer[W, A]

MonadCensor modifies the final accumulator value by applying a function

func MonadChain

func MonadChain[FCT ~func(A) Writer[W, B], W, A, B any](s SG.Semigroup[W], fa Writer[W, A], fct FCT) Writer[W, B]

func MonadChainFirst

func MonadChainFirst[FCT ~func(A) Writer[W, B], W, A, B any](s SG.Semigroup[W], fa Writer[W, A], fct FCT) Writer[W, A]

func MonadFlap added in v1.0.110

func MonadFlap[W, B, A any](fab Writer[W, func(A) B], a A) Writer[W, B]

func MonadListens

func MonadListens[A any, FCT ~func(W) B, W, B any](fa Writer[W, A], f FCT) Writer[W, P.Pair[A, B]]

MonadListens projects a value from modifications made to the accumulator during an action

func MonadMap

func MonadMap[FCT ~func(A) B, W, A, B any](fa Writer[W, A], f FCT) Writer[W, B]

func Of

func Of[A, W any](m M.Monoid[W], a A) Writer[W, A]

func Pass added in v1.0.107

func Pass[W, A any](fa Writer[W, P.Pair[A, EM.Endomorphism[W]]]) Writer[W, A]

Pass applies the returned function to the accumulator

func Tell added in v1.0.110

func Tell[W any](w W) Writer[W, any]

Tell appends a value to the accumulator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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