Skip to content

Logged

Logged: object

Defined in: Core/Logged.ts:29

ap: <W, A>(arg) => <B>(data) => Logged<W, B>

Applies a function wrapped in a Logged to a value wrapped in a Logged, concatenating both logs.

W

A

Logged<W, A>

<B>(data) => Logged<W, B>

const fn: Logged<string, (n: number) => number> = {
  value: n => n * 2,
  log: ["fn-loaded"],
};
const arg: Logged<string, number> = { value: 5, log: ["arg-loaded"] };

const result = pipe(fn, Logged.ap(arg));
Logged.run(result); // [10, ["fn-loaded", "arg-loaded"]]

bind: <K, W, A, B>(key, f) => (data) => Logged<W, A & { [P in string]: B }>

Evaluates a new Logged using the current accumulator and attaches the output to a new key.

K extends string

W

A

B

K

(a) => Logged<W, B>

(data) => Logged<W, A & { [P in string]: B }>

pipe(
  Logged.from.value<string, { a: number }>({ a: 1 }),
  Logged.bind("b", ({ a }) => Logged.from.value<string, number>(a + 1))
); // Logged({ value: { a: 1, b: 2 } })

bindTo: <K>(key) => <W, A>(data) => Logged<W, { [P in string]: A }>

Lifts a Logged value into an accumulator object.

K extends string

K

<W, A>(data) => Logged<W, { [P in string]: A }>

pipe(Logged.from.value<string, number>(42), Logged.bindTo("value")); // Logged({ value: 42 })

chain: <W, A, B>(f) => (data) => Logged<W, B> = chainLogged

Sequences two Logged computations, concatenating their logs. The value from the first is passed to f; the resulting log entries are appended after the entries from the first.

Data-last — the first computation is the data being piped.

W

A

B

(a) => Logged<W, B>

(data) => Logged<W, B>

const result = pipe(
  Logged.from.value<string, number>(1),
  Logged.chain(n => pipe(Logged.from.entry("step"), Logged.map(() => n + 1))),
  Logged.chain(n => pipe(Logged.from.entry("done"), Logged.map(() => n * 10))),
);

Logged.run(result); // [20, ["step", "done"]]

focus: <S, A>(lens) => <W>(f) => (data) => Logged<W, S>

Focuses a Logged computation’s value transformation using a Lens.

S

A

Lens<S, A>

<W>(f) => (data) => Logged<W, S>

const nameLens = Lens.from.property<{ name: string }>()("name");
const logged = Logged.from.value<string, { name: string }>({ name: "alice" });
pipe(logged, Logged.focus(nameLens)(s => s.toUpperCase()));

from: object

entry: <W>(logEntry) => Logged<W, undefined> = makeEntry

Creates a Logged that records a single log entry and produces no meaningful value. Use this to append to the log inside a chain.

W

W

Logged<W, undefined>

Logged.from.entry("operation completed"); // { value: undefined, log: ["operation completed"] }

value: <W, A>(val) => Logged<W, A> = makeValue

Wraps a pure value into a Logged with an empty log.

W

A

A

Logged<W, A>

Logged.from.value<string, number>(42); // { value: 42, log: [] }

map: <W, A, B>(f) => (data) => Logged<W, B> = mapLogged

Transforms the value inside a Logged without affecting the log.

W

A

B

(a) => B

(data) => Logged<W, B>

pipe(
  Logged.from.value<string, number>(5),
  Logged.map(n => n * 2),
); // { value: 10, log: [] }

run: <W, A>(data) => readonly [A, readonly W[]]

Extracts the value and log as a readonly [A, ReadonlyArray<W>] tuple. Use this at the boundary where you need to consume both.

W

A

Logged<W, A>

readonly [A, readonly W[]]

const result = pipe(
  Logged.from.value<string, number>(1),
  Logged.chain(n => pipe(Logged.from.entry("incremented"), Logged.map(() => n + 1))),
);

const [value, log] = Logged.run(result);
// value = 2, log = ["incremented"]

tap: <W, A>(f) => (data) => Logged<W, A>

Runs a side effect on the value without changing the Logged. Useful for debugging or inspecting intermediate values.

W

A

(a) => void

(data) => Logged<W, A>

pipe(
  Logged.from.value<string, number>(42),
  Logged.tap(n => console.log("value:", n)),
);