writer

package
v1.0.124 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 7 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](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](
	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 Bind added in v1.0.107

func Bind[S1, S2, T, W any](
	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](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](fct FCT) func(Writer[W, A]) Writer[W, A]

func Do added in v1.0.107

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

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

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 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, T.Tuple2[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 Of

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

func Tell added in v1.0.110

func Tell[W any](s S.Semigroup[W]) func(W) Writer[W, any]

Tell appends a value to the accumulator

Types

type Writer

type Writer[W, A any] IO.IO[T.Tuple3[A, W, S.Semigroup[W]]]
Example (Logging)
package main

import (
	"fmt"

	A "github.com/IBM/fp-go/array"
	F "github.com/IBM/fp-go/function"
	S "github.com/IBM/fp-go/semigroup"
	T "github.com/IBM/fp-go/tuple"
)

func doubleAndLog(data int) Writer[[]string, int] {
	return func() T.Tuple3[int, []string, S.Semigroup[[]string]] {
		result := data * 2
		return T.MakeTuple3(result, A.Of(fmt.Sprintf("Doubled %d -> %d", data, result)), sg)
	}
}

func main() {

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

	fmt.Println(res)

}
Output:

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

func Flatten

func Flatten[W, A any](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, T.Tuple2[A, W]]

Listen modifies the result to include the changes to the accumulator

func MonadAp

func MonadAp[B, A, W any](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](fa Writer[W, A], fct FCT) Writer[W, B]

func MonadChainFirst

func MonadChainFirst[FCT ~func(A) Writer[W, B], W, A, B any](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, T.Tuple2[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 Pass added in v1.0.107

func Pass[W, A any](fa Writer[W, T.Tuple2[A, EM.Endomorphism[W]]]) Writer[W, A]

Pass applies the returned function to the accumulator

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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