typeclass/Writer.ts
A Haskell-inspired Writer typeclass.
Added in v2.0.0
Signature
export type Writer<W, A> = readonly [A, W]
Source: src/typeclass/Writer.ts:19
Added in v2.0.0
censor f m
is an action that executes the action m
and applies the function f
to its output, leaving the return value unchanged.
Signature
export declare const censor: ((...args: Array<any>) => any) & (<A, W>(fa: readonly [A, W], f: (w: W) => W) => [A, W])
Source: src/typeclass/Writer.ts:31
Added in v2.0.0
Signature
export declare const getCovariant: <W>() => covariant.Covariant<WriterTypeLambda<W>>
Source: src/typeclass/Writer.ts:82
Added in v2.0.0
Signature
export declare const getFlatMap: <W>(M: monoid.Monoid<W>) => flatmap.FlatMap<WriterTypeLambda<W>>
Source: src/typeclass/Writer.ts:70
Added in v2.0.0
Derive a Writer
typeclass from a Monoid
.
Signature
export declare const getMonad: <W>(M: monoid.Monoid<W>) => monad.Monad<WriterTypeLambda<W>>
Example
import * as import Writer
Writer from "@unionlabs/sdk/typeclass/Writer"import * as import StringInstances
StringInstances from "@effect/typeclass/data/String"import * as import FlatMap
FlatMap from "@effect/typeclass/FlatMap"import { function pipe<A>(a: A): A (+19 overloads)
Pipes the value of an expression into a pipeline of functions.
Details
The pipe
function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)
In this syntax, input
is the initial value, and func1
, func2
, ...,
funcN
are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
Here's an illustration of how pipe
works:
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘
It's important to note that functions passed to pipe
must have a single
argument because they are only called with a single argument.
When to Use
This is useful in combination with data-last functions as a simulation of
methods:
as.map(f).filter(g)
becomes:
import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))
Example (Chaining Arithmetic Operations)
import { pipe } from "effect"
// Define simple arithmetic operationsconst increment = (x: number) => x + 1const double = (x: number) => x * 2const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`const result = pipe(5, increment, double, subtractTen)
console.log(result)// Output: 2
pipe } from "effect/Function"
const const composeK: { <B, R2, O2, E2, C>(bfc: (b: B) => Writer.Writer<string, C>): <A, R1, O1, E1>(afb: (a: A) => Writer.Writer<string, B>) => (a: A) => Writer.Writer<string, C>; <A, R1_1, O1_1, E1_1, B, R2_1, O2_1, E2_1, C_1>(afb: (a: A) => Writer.Writer<...>, bfc: (b: B) => Writer.Writer<...>): (a: A) => Writer.Writer<...>;}
composeK = pipe<Monoid<string>, Monad<WriterTypeLambda<string>>, { <B, R2, O2, E2, C>(bfc: (b: B) => Writer.Writer<string, C>): <A, R1, O1, E1>(afb: (a: A) => Writer.Writer<...>) => (a: A) => Writer.Writer<...>; <A, R1_1, O1_1, E1_1, B, R2_1, O2_1, E2_1, C_1>(afb: (a: A) => Writer.Writer<...>, bfc: (b: B) => Writer.Writer<...>): (a: A) => Writer.Writer<...>;}>(a: Monoid<...>, ab: (a: Monoid<...>) => Monad<...>, bc: (b: Monad<...>) => { <B, R2, O2, E2, C>(bfc: (b: B) => Writer.Writer<string, C>): <A, R1, O1, E1>(afb: (a: A) => Writer.Writer<...>) => (a: A) => Writer.Writer<...>; <A, R1_1, O1_1, E1_1, B, R2_1, O2_1, E2_1, C_1>(afb: (a: A) => Writer.Writer<...>, bfc: (b: B) => Writer.Writer<...>): (a: A) => Writer.Writer<...>;}): { <B, R2, O2, E2, C>(bfc: (b: B) => Writer.Writer<string, C>): <A, R1, O1, E1>(afb: (a: A) => Writer.Writer<...>) => (a: A) => Writer.Writer<...>; <A, R1_1, O1_1, E1_1, B, R2_1, O2_1, E2_1, C_1>(afb: (a: A) => Writer.Writer<...>, bfc: (b: B) => Writer.Writer<...>): (a: A) => Writer.Writer<...>;} (+19 overloads)
Pipes the value of an expression into a pipeline of functions.
Details
The pipe
function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)
In this syntax, input
is the initial value, and func1
, func2
, ...,
funcN
are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
Here's an illustration of how pipe
works:
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘
It's important to note that functions passed to pipe
must have a single
argument because they are only called with a single argument.
When to Use
This is useful in combination with data-last functions as a simulation of
methods:
as.map(f).filter(g)
becomes:
import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))
Example (Chaining Arithmetic Operations)
import { pipe } from "effect"
// Define simple arithmetic operationsconst increment = (x: number) => x + 1const double = (x: number) => x * 2const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`const result = pipe(5, increment, double, subtractTen)
console.log(result)// Output: 2
pipe(import StringInstances
StringInstances.const Monoid: Monoid<string>
string
monoid under concatenation.
The empty
value is ''
.
Monoid, import Writer
Writer.const getMonad: <W>(M: Monoid<W>) => Monad<WriterTypeLambda<W>>
Derive a Writer
typeclass from a Monoid
.
getMonad, import FlatMap
FlatMap.const composeK: <F extends TypeLambda>(F: FlatMap.FlatMap<F>) => { <B, R2, O2, E2, C>(bfc: (b: B) => Kind<F, R2, O2, E2, C>): <A, R1, O1, E1>(afb: (a: A) => Kind<F, R1, O1, E1, B>) => (a: A) => Kind<F, R1 & R2, O2 | O1, E2 | E1, C>; <A, R1_1, O1_1, E1_1, B, R2_1, O2_1, E2_1, C_1>(afb: (a: A) => Kind<F, R1_1, O1_1, E1_1, B>, bfc: (b: B) => Kind<F, R2_1, O2_1, E2_1, C_1>): (a: A) => Kind<F, R1_1 & R2_1, O1_1 | O2_1, E1_1 | E2_1, C_1>;}
composeK)
const const f: (s: string) => [number, string]
f = (s: string
s: string): [number, string] => [s: string
s.String.length: number
Returns the length of a String object.
length, "[length]"]const const g: (n: number) => [boolean, string]
g = (n: number
n: number): [boolean, string] => [n: number
n > 3, `[(>) 3 ${n: number
n}]`]const const h: (a: string) => Writer.Writer<string, boolean>
h = pipe<(s: string) => [number, string], (a: string) => Writer.Writer<string, boolean>>(a: (s: string) => [number, string], ab: (a: (s: string) => [number, string]) => (a: string) => Writer.Writer<string, boolean>): (a: string) => Writer.Writer<string, boolean> (+19 overloads)
Pipes the value of an expression into a pipeline of functions.
Details
The pipe
function is a utility that allows us to compose functions in a
readable and sequential manner. It takes the output of one function and
passes it as the input to the next function in the pipeline. This enables us
to build complex transformations by chaining multiple functions together.
import { pipe } from "effect"
const result = pipe(input, func1, func2, ..., funcN)
In this syntax, input
is the initial value, and func1
, func2
, ...,
funcN
are the functions to be applied in sequence. The result of each
function becomes the input for the next function, and the final result is
returned.
Here's an illustration of how pipe
works:
┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌───────┐ ┌────────┐│ input │───►│ func1 │───►│ func2 │───►│ ... │───►│ funcN │───►│ result │└───────┘ └───────┘ └───────┘ └───────┘ └───────┘ └────────┘
It's important to note that functions passed to pipe
must have a single
argument because they are only called with a single argument.
When to Use
This is useful in combination with data-last functions as a simulation of
methods:
as.map(f).filter(g)
becomes:
import { pipe, Array } from "effect"
pipe(as, Array.map(f), Array.filter(g))
Example (Chaining Arithmetic Operations)
import { pipe } from "effect"
// Define simple arithmetic operationsconst increment = (x: number) => x + 1const double = (x: number) => x * 2const subtractTen = (x: number) => x - 10
// Sequentially apply these operations using `pipe`const result = pipe(5, increment, double, subtractTen)
console.log(result)// Output: 2
pipe(const f: (s: string) => [number, string]
f, const composeK: <number, unknown, unknown, unknown, boolean>(bfc: (b: number) => Writer.Writer<string, boolean>) => <A, R1, O1, E1>(afb: (a: A) => Writer.Writer<string, number>) => (a: A) => Writer.Writer<...> (+1 overload)
composeK(const g: (n: number) => [boolean, string]
g))
function assert(value: unknown, message?: string | Error): asserts value
An alias of
ok
.
assert.function assert.deepStrictEqual<(string | boolean)[]>(actual: unknown, expected: (string | boolean)[], message?: string | Error): asserts actual is (string | boolean)[]
Tests for deep equality between the actual
and expected
parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules.
deepStrictEqual(const h: (a: string) => Writer.Writer<string, boolean>
h(""), [false, "[length][(>) 3 0]"])function assert(value: unknown, message?: string | Error): asserts value
An alias of
ok
.
assert.function assert.deepStrictEqual<(string | boolean)[]>(actual: unknown, expected: (string | boolean)[], message?: string | Error): asserts actual is (string | boolean)[]
Tests for deep equality between the actual
and expected
parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules.
deepStrictEqual(const h: (a: string) => Writer.Writer<string, boolean>
h("abc"), [false, "[length][(>) 3 3]"])function assert(value: unknown, message?: string | Error): asserts value
An alias of
ok
.
assert.function assert.deepStrictEqual<(string | boolean)[]>(actual: unknown, expected: (string | boolean)[], message?: string | Error): asserts actual is (string | boolean)[]
Tests for deep equality between the actual
and expected
parameters.
"Deep" equality means that the enumerable "own" properties of child objects
are recursively evaluated also by the following rules.
deepStrictEqual(const h: (a: string) => Writer.Writer<string, boolean>
h("abcd"), [true, "[length][(>) 3 4]"])
Source: src/typeclass/Writer.ts:120
Added in v2.0.0
Signature
export declare const getOf: <W>(M: monoid.Monoid<W>) => of.Of<WriterTypeLambda<W>>
Source: src/typeclass/Writer.ts:62
Added in v2.0.0
Map both the return value and output of a computation using the given function.
Signature
export declare const mapWriter: { <W1, W2, A, B>(f: (a: A, w: W1) => readonly [B, W2]): (writer: readonly [A, W1]) => readonly [B, W2] <W1, W2, A, B>(writer: readonly [A, W1], f: (a: A, w: W1) => readonly [B, W2]): readonly [B, W2]}
Source: src/typeclass/Writer.ts:42
Added in v2.0.0