Skip to content

Commit 5c0783b

Browse files
committed
docs: update docs
1 parent e1ba45b commit 5c0783b

File tree

4 files changed

+60
-25
lines changed

4 files changed

+60
-25
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ npm install holo-fn
3939
### Safe object access
4040

4141
```ts
42+
import { fromNullable, matchM } from 'holo-fn/maybe';
43+
4244
const double = (n: number) => n * 2;
4345

4446
const result = pipe(
4547
[5, 5, 6],
4648
(ns) => fromNullable(head(ns)),
4749
(x) => x.map(double),
48-
M.matchM({
50+
matchM({
4951
just: (n) => n,
5052
nothing: () => -1
5153
})
@@ -57,11 +59,18 @@ console.log(result); // 10
5759
### Optional parsing
5860

5961
```ts
62+
import { fromNullable } from 'holo-fn/maybe';
63+
6064
const parsePrice = (input: string) =>
6165
fromNullable(parseFloat(input))
6266
.map(n => (n > 0 ? n : null))
6367
.chain(fromNullable)
6468
.unwrapOr(0)
69+
70+
console.log(parsePrice('123.45')) // 123.45
71+
console.log(parsePrice('0')) // 0
72+
console.log(parsePrice('-123.45')) // 0
73+
console.log(parsePrice('abc')) // 0
6574
```
6675

6776
---

docs/either/index.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
`Either` is used for computations that may fail. It is either a `Left<L>` (error) or a `Right<R>` (success).
44

55
```ts
6-
import { Right, Left, tryCatch } from 'holo-fn/either'
6+
import { Right } from 'holo-fn/either'
77

88
const result = new Right<number, number>(10)
99
.map(n => n * 2)
10-
.unwrapOr(0) // 20
10+
.unwrapOr(0)
11+
12+
console.log(result); // 20
1113
```
1214

1315
## Methods
@@ -42,9 +44,13 @@ Wraps a potentially throwing function in an `Either`.
4244
```ts
4345
import { tryCatch } from 'holo-fn/either'
4446

47+
const input = '{"user": "John Doe"}'
48+
4549
const parsed = tryCatch(() => JSON.parse(input), e => 'Invalid JSON')
4650
.map(obj => obj.user)
4751
.unwrapOr('anonymous')
52+
53+
console.log(parsed) // John Doe
4854
```
4955

5056
- Returns `Right<R>` if `fn()` succeeds
@@ -61,6 +67,8 @@ import { fromPromise } from 'holo-fn/either'
6167

6268
const result = await fromPromise(fetch('/api'), e => 'Network error')
6369

70+
console.log(result) // _Left { value: 'Network error' }
71+
6472
```
6573

6674
- Resolves to `Right<R>` on success
@@ -77,6 +85,8 @@ import { fromAsync } from 'holo-fn/either'
7785

7886
const result = await fromAsync(async () => await fetch('/api'), e => 'Request failed')
7987

88+
console.log(result) // _Left { value: 'Request failed' }
89+
8090
```
8191

8292
- Allows deferred execution
@@ -91,7 +101,7 @@ const result = await fromAsync(async () => await fetch('/api'), e => 'Request fa
91101
Curried version of `map` for `Either`. This allows functional composition with `pipe`.
92102

93103
```ts
94-
import { mapE } from 'holo-fn/either';
104+
import { mapE, Right } from 'holo-fn/either';
95105

96106
const result = pipe(
97107
new Right(5),
@@ -109,15 +119,15 @@ console.log(result); // 10
109119
Curried version of `mapLeft` for `Either`. This allows mapping over the Left value in a functional pipeline.
110120

111121
```ts
112-
import { mapLeftE } from 'holo-fn/either';
122+
import { Left, mapLeftE } from 'holo-fn/either';
113123

114124
const result = pipe(
115125
new Left("Error"),
116126
mapLeftE((e) => `Mapped error: ${e}`),
117127
(res) => res.unwrapOr("No value")
118128
);
119129

120-
console.log(result); // "Mapped error: Error"
130+
console.log(result); // "No value"
121131
```
122132

123133
---
@@ -127,9 +137,7 @@ console.log(result); // "Mapped error: Error"
127137
Curried version of `chain` for `Either`. This allows chaining transformations on the **Right** value of `Either`, using a functional composition style.
128138

129139
```ts
130-
import { Right, Left, chainE } from 'holo-fn/either';
131-
132-
const double = (n: number) => n * 2;
140+
import { Right, chainE } from 'holo-fn/either';
133141

134142
const result = pipe(
135143
new Right(5),
@@ -147,11 +155,11 @@ console.log(result); // 10
147155
Curried version of `unwrapOr` for `Either`. This provides a cleaner way to unwrap the value in a `Either`, returning a default value if it's `Left`.
148156

149157
```ts
150-
import { unwrapOrE } from 'holo-fn/either';
158+
import { Left, unwrapOrE } from 'holo-fn/either';
151159

152160
const result = pipe(
153161
new Left("Fail"),
154-
unwrapOrE("No value")
162+
unwrapOrE<string, unknown>("No value")
155163
);
156164

157165
console.log(result); // "No value"
@@ -164,7 +172,7 @@ console.log(result); // "No value"
164172
Curried version of `match` for `Either`. This allows handling `Left` and `Right` in a functional way.
165173

166174
```ts
167-
import { matchE } from 'holo-fn/either';
175+
import { matchE, Right } from 'holo-fn/either';
168176

169177
const result = pipe(
170178
new Right(10),

docs/maybe/index.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { fromNullable } from 'holo-fn/maybe'
88
const name = fromNullable('Rich')
99
.map(n => n.toUpperCase())
1010
.unwrapOr('Anonymous')
11+
12+
console.log(name) // RICH
1113
```
1214

1315
## Methods
@@ -52,7 +54,7 @@ const maybeEmail = fromNullable(user.email)
5254
Curried version of `map` for `Maybe`. This allows functional composition with `pipe`.
5355

5456
```ts
55-
import { mapM } from 'holo-fn/maybe';
57+
import { Just, mapM } from 'holo-fn/maybe';
5658

5759
const result = pipe(
5860
new Just(10),
@@ -70,7 +72,7 @@ console.log(result); // 20
7072
Curried version of `chain` for `Maybe`. This allows chaining transformations in a functional pipeline.
7173

7274
```ts
73-
import { chainM } from 'holo-fn/maybe';
75+
import { chainM, Just } from 'holo-fn/maybe';
7476

7577
const result = pipe(
7678
new Just(2),
@@ -88,10 +90,10 @@ console.log(result); // 20
8890
Curried version of `unwrapOr` for `Maybe`. This provides a cleaner way to unwrap the value in a `Maybe`.
8991

9092
```ts
91-
import { unwrapOrM } from 'holo-fn/maybe';
93+
import { Nothing, unwrapOrM } from 'holo-fn/maybe';
9294

9395
const result = pipe(
94-
new Nothing(),
96+
new Nothing<string>(),
9597
unwrapOrM("No value")
9698
);
9799

@@ -105,7 +107,7 @@ console.log(result); // "No value"
105107
Curried version of `match` for `Maybe`. This allows handling `Just` and `Nothing` in a functional way.
106108

107109
```ts
108-
import { matchM } from 'holo-fn/maybe';
110+
import { Just, matchM } from 'holo-fn/maybe';
109111

110112
const result = pipe(
111113
new Just("hello"),

docs/result/index.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
`Result` is used to represent computations that either succeed with a value (`Ok<T>`) or fail with an error (`Err<E>`).
44

55
```ts
6-
import { Ok, Err, fromThrowable, fromPromise, fromAsync } from 'holo-fn/result'
6+
import { Ok } from 'holo-fn/result'
77

88
const result = new Ok<number, string>(10)
99
.map(n => n + 1)
10-
.unwrapOr(0) // 11
10+
.unwrapOr(0)
11+
12+
console.log(result) // 11
1113
```
1214

1315
## Methods
@@ -40,7 +42,13 @@ Matches the value to execute either the `ok` or `err` case.
4042
Wraps a synchronous function in a `Result`.
4143

4244
```ts
45+
import { fromThrowable } from 'holo-fn';
46+
47+
const input = '{"name": "John", "age": 30}'
48+
4349
const result = fromThrowable(() => JSON.parse(input), e => 'Invalid JSON')
50+
51+
console.log(result) // _Ok { value: { name: 'John', age: 30 } }
4452
```
4553

4654
- Returns `Ok<T>` if `fn()` succeeds
@@ -51,7 +59,11 @@ const result = fromThrowable(() => JSON.parse(input), e => 'Invalid JSON')
5159
Wraps a `Promise<T>` into a `Promise<Result<T, E>>`.
5260

5361
```ts
62+
import { fromPromise } from 'holo-fn/result';
63+
5464
const result = await fromPromise(fetch('/api'), e => 'Network error')
65+
66+
console.log(result) // _Err { error: 'Network error' }
5567
```
5668

5769
- Resolves to `Ok<T>` on success
@@ -62,7 +74,11 @@ const result = await fromPromise(fetch('/api'), e => 'Network error')
6274
Same as `fromPromise`, but lazy — receives a function returning a Promise.
6375

6476
```ts
77+
import { fromAsync } from 'holo-fn/result';
78+
6579
const result = await fromAsync(() => fetch('/api'), e => 'Request failed')
80+
81+
console.log(result) // _Err { error: 'Request failed' }
6682
```
6783

6884
- Allows deferred execution
@@ -77,7 +93,7 @@ const result = await fromAsync(() => fetch('/api'), e => 'Request failed')
7793
Curried version of the `map` function for `Result`. This allows you to apply a transformation to the **Ok** value in a more functional style.
7894

7995
```ts
80-
import { mapR } from 'holo-fn/result';
96+
import { mapR, Ok } from 'holo-fn/result';
8197

8298
const result = pipe(
8399
new Ok(5),
@@ -95,15 +111,15 @@ console.log(result); // 10
95111
Curried version of `mapErr` for `Result`. This allows handling errors in a more functional composition style.
96112

97113
```ts
98-
import { mapErrR } from 'holo-fn/result';
114+
import { Err, mapErrR } from 'holo-fn/result';
99115

100116
const result = pipe(
101117
new Err("Error"),
102118
mapErrR((e) => `Mapped error: ${e}`),
103119
(res) => res.unwrapOr("No value")
104120
);
105121

106-
console.log(result); // "Mapped error: Error"
122+
console.log(result); // "No value"
107123
```
108124

109125
---
@@ -113,7 +129,7 @@ console.log(result); // "Mapped error: Error"
113129
Curried version of `chain` for `Result`. This allows you to chain transformations on the Ok value in a functional pipeline.
114130

115131
```ts
116-
import { chainR } from 'holo-fn/result';
132+
import { chainR, Ok } from 'holo-fn/result';
117133

118134
const result = pipe(
119135
new Ok(10),
@@ -131,7 +147,7 @@ console.log(result); // 15
131147
Curried version of `unwrapOr` for `Result`. This provides a cleaner way to unwrap the value in a `Result`, returning a default value if it's `Err`.
132148

133149
```ts
134-
import { unwrapOrR } from 'holo-fn/result';
150+
import { Ok, unwrapOrR } from 'holo-fn/result';
135151

136152
const result = pipe(
137153
new Ok(42),
@@ -148,7 +164,7 @@ console.log(result); // 42
148164
Curried version of `match` for `Result`. This allows you to handle both `Ok` and `Err` in a functional way, providing a clean way to handle both cases.
149165

150166
```ts
151-
import { matchR } from 'holo-fn/result';
167+
import { matchR, Ok } from 'holo-fn/result';
152168

153169
const result = pipe(
154170
new Ok(10),

0 commit comments

Comments
 (0)