Play with Ref
Ref represents an immutable reference to a mutable value

Ref

1
// describe a reference to A
2
export interface Ref<A> {
3
// read the current value
4
readonly get: T.Effect<T.NoEnv, never, A>;
5
6
// set the value
7
set(a: A): T.Effect<T.NoEnv, never, A>;
8
9
// update the value
10
update(f: FunctionN<[A], A>): T.Effect<T.NoEnv, never, A>;
11
12
// update the value and return B
13
modify<B>(f: FunctionN<[A], readonly [B, A]>): T.Effect<T.NoEnv, never, B>;
14
}
15
16
// create a new reference
17
export const makeRef = <A>(initial: A): T.Effect<T.NoEnv, never, Ref<A>>
Copied!

Usage

1
import { effect as T, ref as R } from "@matechs/effect";
2
import { Do } from "fp-ts-contrib/lib/Do";
3
4
const program: T.Effect<unknown, never, number> = Do(T.effect)
5
.bind("ref", R.makeRef(0)) // create a new ref<number>
6
.doL(({ ref }) => ref.update(n => n + 1)) // increment
7
.bindL("value", ({ ref }) => ref.get) // read
8
.return(s => s.value); // return
Copied!
Last modified 1yr ago
Copy link
Contents
Ref
Usage