You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Maybe` monad represents values that may or may not exist. It's a safe way to handle potentially null or undefined values without resorting to null checks throughout your code.
54
+
55
+
```typescript
56
+
import { maybe, none } from'typescript-monads'
57
+
58
+
// Creating Maybe instances
59
+
const someValue =maybe(42) // Maybe with a value
60
+
const noValue =maybe(null) // Maybe with no value (None)
61
+
const alsoNoValue =none<number>() // Explicitly create a None
maybeToObservable(maybe(5)) // Observable that emits 5 and completes
98
+
maybeToObservable(none()) // Observable that completes without emitting
99
+
```
53
100
54
101
## List
55
-
TODO
102
+
103
+
The `List` monad is a lazily evaluated collection with chainable operations. It provides many of the common list processing operations found in functional programming languages.
104
+
105
+
```typescript
106
+
import { List } from'typescript-monads'
107
+
108
+
// Creating Lists
109
+
const fromValues =List.of(1, 2, 3, 4, 5)
110
+
const fromIterable =List.from([1, 2, 3, 4, 5])
111
+
const numbersFromRange =List.range(1, 10) // 1 to 10
112
+
const infiniteNumbers =List.integers() // All integers (use with take!)
The `Either` monad represents values that can be one of two possible types. It's often used to represent a value that can be either a success (Right) or a failure (Left).
146
+
147
+
```typescript
148
+
import { either } from'typescript-monads'
149
+
150
+
// Creating Either instances
151
+
const rightValue =either<string, number>(undefined, 42) // Right value
152
+
const leftValue =either<string, number>('error', undefined) // Left value
153
+
154
+
// Checking which side is present
155
+
rightValue.isRight() // true
156
+
rightValue.isLeft() // false
157
+
leftValue.isRight() // false
158
+
leftValue.isLeft() // true
159
+
160
+
// Pattern matching
161
+
rightValue.match({
162
+
right: val=>`Success: ${val}`,
163
+
left: err=>`Error: ${err}`
164
+
}) // "Success: 42"
165
+
166
+
// Side effects with tap
167
+
rightValue.tap({
168
+
right: val=>console.log(`Got right: ${val}`),
169
+
left: err=>console.log(`Got left: ${err}`)
170
+
})
171
+
172
+
// Transformation (only applies to Right values)
173
+
rightValue.map(n=>n*2) // Either with Right(84)
174
+
leftValue.map(n=>n*2) // Either with Left('error') unchanged
175
+
176
+
// Chaining (flatMap only applies to Right values)
177
+
rightValue.flatMap(n=>either(undefined, n+10)) // Either with Right(52)
178
+
leftValue.flatMap(n=>either(undefined, n+10)) // Either with Left('error') unchanged
179
+
```
59
180
60
181
## Reader
61
-
TODO
182
+
183
+
The `Reader` monad represents a computation that depends on some external configuration or environment. It's useful for dependency injection.
184
+
185
+
```typescript
186
+
import { reader } from'typescript-monads'
187
+
188
+
// Define a configuration type
189
+
interfaceConfig {
190
+
apiUrl:string
191
+
apiKey:string
192
+
}
193
+
194
+
// Create readers that depend on this configuration
The `Result` monad represents operations that can either succeed with a value or fail with an error. It's similar to Either but with more specific semantics for success/failure.
0 commit comments