1 | /*!
|
2 | * Copyright (c) 2017-2018 by The Funfix Project Developers.
|
3 | * Some rights reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | import { Setoid, Monad } from "funland";
|
18 | import * as std from "./std";
|
19 | import { HK, HK2 } from "./kinds";
|
20 | import { Throwable } from "./errors";
|
21 | /**
|
22 | * Represents a value of one of two possible types (a disjoint union).
|
23 | *
|
24 | * A common use of Either is as an alternative to [[Option]] for dealing
|
25 | * with possible missing values. In this usage [[Option.none]] is replaced
|
26 | * with [[Either.left]] which can contain useful information and
|
27 | * [[Option.some]] is replaced with [[Either.right]].
|
28 | *
|
29 | * Convention dictates that `left` is used for failure and `right` is used
|
30 | * for success. Note that this `Either` type is right-biased, meaning that
|
31 | * operations such as `map`, `flatMap` and `filter` work on the `right` value
|
32 | * and if you want to work on the `left` value, then you need to do a `swap`.
|
33 | *
|
34 | * For example, you could use `Either<String, Int>` to detect whether an
|
35 | * input is a string or an number:
|
36 | *
|
37 | * ```typescript
|
38 | * function tryParseInt(str: string): Either<string, number> {
|
39 | * const i = parseInt(value)
|
40 | * return isNaN(i) ? Left(str) : Right(i)
|
41 | * }
|
42 | *
|
43 | * const result = tryParseInt("not an int")
|
44 | * if (result.isRight()) {
|
45 | * console.log(`Increment: ${result.get}`)
|
46 | * } else {
|
47 | * console.log(`ERROR: could not parse ${result.swap.get}`)
|
48 | * }
|
49 | * ```
|
50 | *
|
51 | * @final
|
52 | */
|
53 | export declare class Either<L, R> implements std.IEquals<Either<L, R>>, HK2<"funfix/either", L, R> {
|
54 | readonly value: L | R;
|
55 | private readonly _isRight;
|
56 | protected constructor(value: L | R, tag: "left" | "right");
|
57 | /**
|
58 | * Returns `true` if this is a `left`, `false` otherwise.
|
59 | *
|
60 | * ```typescript
|
61 | * Left("hello").isLeft() // true
|
62 | * Right(10).isLeft() // false
|
63 | * ```
|
64 | */
|
65 | isLeft(): this is TLeft<L>;
|
66 | /**
|
67 | * Returns `true` if this is a `right`, `false` otherwise.
|
68 | *
|
69 | * ```typescript
|
70 | * Left("hello").isRight() // false
|
71 | * Right(10).isRight() // true
|
72 | * ```
|
73 | */
|
74 | isRight(): this is TRight<R>;
|
75 | /**
|
76 | * Returns true if this is a Right and its value is equal to `elem`
|
77 | * (as determined by the `equals` protocol), returns `false` otherwise.
|
78 | *
|
79 | * ```typescript
|
80 | * // True
|
81 | * Right("something").contains("something")
|
82 | *
|
83 | * // False because the values are different
|
84 | * Right("something").contains("anything") // false
|
85 | *
|
86 | * // False because the source is a `left`
|
87 | * Left("something").contains("something") // false
|
88 | * ```
|
89 | */
|
90 | contains(elem: R): this is TRight<R>;
|
91 | /**
|
92 | * Returns `false` if the source is a `left`, or returns the result
|
93 | * of the application of the given predicate to the `right` value.
|
94 | *
|
95 | * ```typescript
|
96 | * // True, because it is a right and predicate holds
|
97 | * Right(20).exists(n => n > 10)
|
98 | *
|
99 | * // False, because the predicate returns false
|
100 | * Right(10).exists(n => n % 2 != 0)
|
101 | *
|
102 | * // False, because it is a left
|
103 | * Left(10).exists(n => n == 10)
|
104 | * ```
|
105 | */
|
106 | exists(p: (r: R) => boolean): this is TRight<R>;
|
107 | /**
|
108 | * Filters `right` values with the given predicate, returning
|
109 | * the value generated by `zero` in case the source is a `right`
|
110 | * value and the predicate doesn't hold.
|
111 | *
|
112 | * Possible outcomes:
|
113 | *
|
114 | * - Returns the existing value of `right` if this is a `right` value and the
|
115 | * given predicate `p` holds for it
|
116 | * - Returns `Left(zero())` if this is a `right` value
|
117 | * and the given predicate `p` does not hold
|
118 | * - Returns the current "left" value, if the source is a `Left`
|
119 | *
|
120 | * ```typescript
|
121 | * Right(12).filterOrElse(x => x > 10, () => -1) // Right(12)
|
122 | * Right(7).filterOrElse(x => x > 10, () => -1) // Left(-1)
|
123 | * Left(7).filterOrElse(x => false, () => -1) // Left(7)
|
124 | * ```
|
125 | */
|
126 | filterOrElse<LL>(p: (r: R) => boolean, zero: () => LL): Either<L | LL, R>;
|
127 | /**
|
128 | * Binds the given function across `right` values.
|
129 | *
|
130 | * This operation is the monadic "bind" operation.
|
131 | * It can be used to *chain* multiple `Either` references.
|
132 | */
|
133 | flatMap<S>(f: (r: R) => Either<L, S>): Either<L, S>;
|
134 | /** Alias for [[flatMap]]. */
|
135 | chain<S>(f: (r: R) => Either<L, S>): Either<L, S>;
|
136 | /**
|
137 | * `Applicative` apply operator.
|
138 | *
|
139 | * Resembles {function is
map}, but the passed mapping |
140 | * lifted in the `Either` context.
|
141 | */
|
142 | ap<S>(ff: Either<L, (a: R) => S>): Either<L, S>;
|
143 | /**
|
144 | * Applies the `left` function to [[Left]] values, and the
|
145 | * `right` function to [[Right]] values and returns the result.
|
146 | *
|
147 | * ```typescript
|
148 | * const maybeNum: Either<string, number> =
|
149 | * tryParseInt("not a number")
|
150 | *
|
151 | * const result: string =
|
152 | * maybeNum.fold(
|
153 | * str => `Could not parse string: ${str}`,
|
154 | * num => `Success: ${num}`
|
155 | * )
|
156 | * ```
|
157 | */
|
158 | fold<S>(left: (l: L) => S, right: (r: R) => S): S;
|
159 | /**
|
160 | * Returns true if the source is a `left` or returns
|
161 | * the result of the application of the given predicate to the
|
162 | * `right` value.
|
163 | *
|
164 | * ```typescript
|
165 | * // True, because it is a `left`
|
166 | * Left("hello").forAll(x => x > 10)
|
167 | *
|
168 | * // True, because the predicate holds
|
169 | * Right(20).forAll(x => x > 10)
|
170 | *
|
171 | * // False, it's a right and the predicate doesn't hold
|
172 | * Right(7).forAll(x => x > 10)
|
173 | * ```
|
174 | */
|
175 | forAll(p: (r: R) => boolean): boolean;
|
176 | /**
|
177 | * Returns the `Right` value, if the source has one,
|
178 | * otherwise throws an exception.
|
179 | *
|
180 | * WARNING!
|
181 | *
|
182 | * This function is partial, the `Either` must be a `Right`, otherwise
|
183 | * a runtime exception will get thrown. Use with care.
|
184 | *
|
185 | * @throws [[NoSuchElementError]] in case the the `Either` is a `Left`
|
186 | */
|
187 | get(): R;
|
188 | /**
|
189 | * Returns the value from this `right` or the given `fallback`
|
190 | * value if this is a `left`.
|
191 | *
|
192 | * ```typescript
|
193 | * Right(10).getOrElse(27) // 10
|
194 | * Left(10).getOrElse(27) // 27
|
195 | * ```
|
196 | */
|
197 | getOrElse<RR>(fallback: RR): R | RR;
|
198 | /**
|
199 | * Returns the value from this `right` or a value generated
|
200 | * by the given `thunk` if this is a `left`.
|
201 | *
|
202 | * ```typescript
|
203 | * Right(10).getOrElseL(() => 27) // 10
|
204 | * Left(10).getOrElseL(() => 27) // 27
|
205 | * ```
|
206 | */
|
207 | getOrElseL<RR>(thunk: () => RR): R | RR;
|
208 | /**
|
209 | * Transform the source if it is a `right` with the given
|
210 | * mapping function.
|
211 | *
|
212 | * ```typescript
|
213 | * Right(10).map(x => x + 17) // right(27)
|
214 | * Left(10).map(x => x + 17) // left(10)
|
215 | * ```
|
216 | */
|
217 | map<C>(f: (r: R) => C): Either<L, C>;
|
218 | /**
|
219 | * Executes the given side-effecting function if the
|
220 | * source is a `right` value.
|
221 | *
|
222 | * ```typescript
|
223 | * Right(12).forAll(console.log) // prints 12
|
224 | * Left(10).forAll(console.log) // silent
|
225 | * ```
|
226 | */
|
227 | forEach(cb: (r: R) => void): void;
|
228 | /**
|
229 | * If this is a `left`, then return the left value as a `right`
|
230 | * or vice versa.
|
231 | *
|
232 | * ```typescript
|
233 | * Right(10).swap() // left(10)
|
234 | * Left(20).swap() // right(20)
|
235 | * ```
|
236 | */
|
237 | swap(): Either<R, L>;
|
238 | /**
|
239 | * Returns an `Option.some(right)` if the source is a `right` value,
|
240 | * or `Option.none` in case the source is a `left` value.
|
241 | */
|
242 | toOption(): Option<R>;
|
243 | /**
|
244 | * Implements {@link IEquals.equals}.
|
245 | *
|
246 | * @param that is the right hand side of the equality check
|
247 | */
|
248 | equals(that: Either<L, R>): boolean;
|
249 | /** Implements {@link IEquals.hashCode}. */
|
250 | hashCode(): number;
|
251 | /** @hidden */ readonly _URI: "funfix/either";
|
252 | /** @hidden */ readonly _A: R;
|
253 | /** @hidden */ readonly _L: L;
|
254 | /** @hidden */ static readonly _Class: Either<any, any>;
|
255 | /**
|
256 | * Builds a pure `Either` value.
|
257 | *
|
258 | * This operation is the pure `Applicative` operation for lifting
|
259 | * a value in the `Either` context.
|
260 | */
|
261 | static pure<A>(value: A): Either<never, A>;
|
262 | /**
|
263 | * Builds a left value, equivalent with {@link Left}.
|
264 | */
|
265 | static left<L, R>(value: L): Either<L, R>;
|
266 | /**
|
267 | * Builds a right value, equivalent with {@link Right}.
|
268 | */
|
269 | static right<L, R>(value: R): Either<L, R>;
|
270 | /**
|
271 | * Maps 2 `Either` values by the mapping function, returning a new
|
272 | * `Either` reference that is a `Right` only if both `Either` values are
|
273 | * `Right` values, otherwise it returns the first `Left` value noticed.
|
274 | *
|
275 | * ```typescript
|
276 | * // Yields Right(3)
|
277 | * Try.map2(Right(1), Right(2),
|
278 | * (a, b) => a + b
|
279 | * )
|
280 | *
|
281 | * // Yields Left, because the second arg is a Left
|
282 | * Try.map2(Right(1), Left("error"),
|
283 | * (a, b) => a + b
|
284 | * )
|
285 | * ```
|
286 | *
|
287 | * This operation is the `Applicative.map2`.
|
288 | */
|
289 | static map2<A1, A2, L, R>(fa1: Either<L, A1>, fa2: Either<L, A2>, f: (a1: A1, a2: A2) => R): Either<L, R>;
|
290 | /**
|
291 | * Maps 3 `Either` values by the mapping function, returning a new
|
292 | * `Either` reference that is a `Right` only if all 3 `Either` values are
|
293 | * `Right` values, otherwise it returns the first `Left` value noticed.
|
294 | *
|
295 | * ```typescript
|
296 | * // Yields Right(6)
|
297 | * Try.map3(Right(1), Right(2), Right(3),
|
298 | * (a, b, c) => a + b + c
|
299 | * )
|
300 | *
|
301 | * // Yields Left, because the second arg is a Left
|
302 | * Try.map3(Right(1), Left("error"), Right(3),
|
303 | * (a, b, c) => a + b + c
|
304 | * )
|
305 | * ```
|
306 | */
|
307 | static map3<A1, A2, A3, L, R>(fa1: Either<L, A1>, fa2: Either<L, A2>, fa3: Either<L, A3>, f: (a1: A1, a2: A2, a3: A3) => R): Either<L, R>;
|
308 | /**
|
309 | * Maps 4 `Either` values by the mapping function, returning a new
|
310 | * `Either` reference that is a `Right` only if all 4 `Either` values are
|
311 | * `Right` values, otherwise it returns the first `Left` value noticed.
|
312 | *
|
313 | * ```typescript
|
314 | * // Yields Right(10)
|
315 | * Try.map4(Right(1), Right(2), Right(3), Right(4),
|
316 | * (a, b, c, d) => a + b + c + d
|
317 | * )
|
318 | *
|
319 | * // Yields Left, because the second arg is a Left
|
320 | * Try.map4(Right(1), Left("error"), Right(3), Right(4),
|
321 | * (a, b, c, d) => a + b + c + d
|
322 | * )
|
323 | * ```
|
324 | */
|
325 | static map4<A1, A2, A3, A4, L, R>(fa1: Either<L, A1>, fa2: Either<L, A2>, fa3: Either<L, A3>, fa4: Either<L, A4>, f: (a1: A1, a2: A2, a3: A3, a4: A4) => R): Either<L, R>;
|
326 | /**
|
327 | * Maps 5 `Either` values by the mapping function, returning a new
|
328 | * `Either` reference that is a `Right` only if all 5 `Either` values are
|
329 | * `Right` values, otherwise it returns the first `Left` value noticed.
|
330 | *
|
331 | * ```typescript
|
332 | * // Yields Right(15)
|
333 | * Try.map5(Right(1), Right(2), Right(3), Right(4), Right(5),
|
334 | * (a, b, c, d, e) => a + b + c + d + e
|
335 | * )
|
336 | *
|
337 | * // Yields Left, because the second arg is a Left
|
338 | * Try.map5(Right(1), Left("error"), Right(3), Right(4), Right(5),
|
339 | * (a, b, c, d, e) => a + b + c + d + e
|
340 | * )
|
341 | * ```
|
342 | */
|
343 | static map5<A1, A2, A3, A4, A5, L, R>(fa1: Either<L, A1>, fa2: Either<L, A2>, fa3: Either<L, A3>, fa4: Either<L, A4>, fa5: Either<L, A5>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R): Either<L, R>;
|
344 | /**
|
345 | * Maps 6 `Either` values by the mapping function, returning a new
|
346 | * `Either` reference that is a `Right` only if all 6 `Either` values are
|
347 | * `Right` values, otherwise it returns the first `Left` value noticed.
|
348 | *
|
349 | * ```typescript
|
350 | * // Yields Right(21)
|
351 | * Try.map5(Right(1), Right(2), Right(3), Right(4), Right(5), Right(6),
|
352 | * (a, b, c, d, e, f) => a + b + c + d + e + f
|
353 | * )
|
354 | *
|
355 | * // Yields Left, because the second arg is a Left
|
356 | * Try.map5(Right(1), Left("error"), Right(3), Right(4), Right(5), Right(6),
|
357 | * (a, b, c, d, e, f) => a + b + c + d + e + f
|
358 | * )
|
359 | * ```
|
360 | */
|
361 | static map6<A1, A2, A3, A4, A5, A6, L, R>(fa1: Either<L, A1>, fa2: Either<L, A2>, fa3: Either<L, A3>, fa4: Either<L, A4>, fa5: Either<L, A5>, fa6: Either<L, A6>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => R): Either<L, R>;
|
362 | /**
|
363 | * Keeps calling `f` until a `Right(b)` is returned.
|
364 | *
|
365 | * Based on Phil Freeman's
|
366 | * [Stack Safety for Free]{@link http://functorial.com/stack-safety-for-free/index.pdf}.
|
367 | *
|
368 | * Described in `FlatMap.tailRecM`.
|
369 | */
|
370 | static tailRecM<L, A, B>(a: A, f: (a: A) => Either<L, Either<A, B>>): Either<L, B>;
|
371 | }
|
372 | /**
|
373 | * Result of the [[Left]] data constructor, representing
|
374 | * "left" values in the [[Either]] disjunction.
|
375 | *
|
376 | * @final
|
377 | */
|
378 | export declare class TLeft<L> extends Either<L, never> {
|
379 | readonly value: L;
|
380 | constructor(value: L);
|
381 | }
|
382 | /**
|
383 | * The `Left` data constructor represents the left side of the
|
384 | * [[Either]] disjoint union, as opposed to the [[Right]] side.
|
385 | */
|
386 | export declare function Left<L>(value: L): TLeft<L>;
|
387 | /**
|
388 | * Result of the [[Right]] data constructor, representing
|
389 | * "right" values in the [[Either]] disjunction.
|
390 | *
|
391 | * @final
|
392 | */
|
393 | export declare class TRight<R> extends Either<never, R> {
|
394 | readonly value: R;
|
395 | constructor(value: R);
|
396 | }
|
397 | /**
|
398 | * The `Right` data constructor represents the right side of the
|
399 | * [[Either]] disjoint union, as opposed to the [[Left]] side.
|
400 | */
|
401 | export declare function Right<R>(value: R): TRight<R>;
|
402 | /**
|
403 | * Type enumerating the type-classes that `Either` implements.
|
404 | */
|
405 | export declare type EitherTypes = Setoid<Either<any, any>> & Monad<"funfix/either">;
|
406 | /**
|
407 | * Type-class implementations, compatible with the `static-land`
|
408 | * and `funland` specifications.
|
409 | *
|
410 | * See [funland-js.org](https://funland-js.org).
|
411 | */
|
412 | export declare const EitherModule: EitherTypes;
|
413 | /**
|
414 | * Represents optional values, inspired by Scala's `Option` and by
|
415 | * Haskell's `Maybe` data types.
|
416 | *
|
417 | * Option is an immutable data type, represented as a sum type, being
|
418 | * either a [[Some]], in case it contains a single element, or a [[None]],
|
419 | * in case it is empty.
|
420 | *
|
421 | * The most idiomatic way to use an `Option` instance is to treat it
|
422 | * as a collection or monad and use `map`,`flatMap`, `filter`,
|
423 | * or `forEach`.
|
424 | *
|
425 | * @final
|
426 | */
|
427 | export declare class Option<A> implements std.IEquals<Option<A>>, HK<"funfix/option", A> {
|
428 | private readonly _isEmpty;
|
429 | readonly value: undefined | A;
|
430 | protected constructor(ref: A | undefined, isEmpty?: boolean);
|
431 | /**
|
432 | * Returns the option's value.
|
433 | *
|
434 | * WARNING!
|
435 | *
|
436 | * This function is partial, the option must be non-empty, otherwise
|
437 | * a runtime exception will get thrown. Use with care.
|
438 | *
|
439 | * @throws [[NoSuchElementError]] in case the option is empty
|
440 | */
|
441 | get(): A;
|
442 | /**
|
443 | * Returns the option's value if the option is nonempty, otherwise
|
444 | * return the given `fallback`.
|
445 | *
|
446 | * See [[Option.getOrElseL]] for a lazy alternative.
|
447 | */
|
448 | getOrElse<AA>(fallback: AA): A | AA;
|
449 | /**
|
450 | * Returns the option's value if the option is nonempty, otherwise
|
451 | * return `null`.
|
452 | * ```
|
453 | */
|
454 | orNull(): A | null;
|
455 | /**
|
456 | * Returns the option's value if the option is nonempty, otherwise
|
457 | * return `undefined`.
|
458 | */
|
459 | orUndefined(): A | undefined;
|
460 | /**
|
461 | * Returns the option's value if the option is nonempty, otherwise
|
462 | * return the result of evaluating `thunk`.
|
463 | *
|
464 | * See [[Option.getOrElse]] for a strict alternative.
|
465 | */
|
466 | getOrElseL<AA>(thunk: () => AA): A | AA;
|
467 | /**
|
468 | * Returns this option if it is nonempty, otherwise returns the
|
469 | * given `fallback`.
|
470 | */
|
471 | orElse<AA>(fallback: Option<AA>): Option<A | AA>;
|
472 | /**
|
473 | * Returns this option if it is nonempty, otherwise returns the
|
474 | * given result of evaluating the given `thunk`.
|
475 | *
|
476 | * @param thunk a no-params function that gets evaluated and
|
477 | * whose result is returned in case this option is empty
|
478 | */
|
479 | orElseL<AA>(thunk: () => Option<AA>): Option<A | AA>;
|
480 | /**
|
481 | * Returns `true` if the option is empty, `false` otherwise.
|
482 | */
|
483 | isEmpty(): this is TNone;
|
484 | /**
|
485 | * Returns `true` if the option is not empty, `false` otherwise.
|
486 | */
|
487 | nonEmpty(): this is TSome<A>;
|
488 | /**
|
489 | * Returns an option containing the result of applying `f` to
|
490 | * this option's value, or an empty option if the source is empty.
|
491 | *
|
492 | * NOTE: this is similar with `flatMap`, except with `map` the
|
493 | * result of `f` doesn't need to be wrapped in an `Option`.
|
494 | *
|
495 | * @param f the mapping function that will transform the value
|
496 | * of this option if nonempty.
|
497 | *
|
498 | * @return a new option instance containing the value of the
|
499 | * source mapped by the given function
|
500 | */
|
501 | map<B>(f: (a: A) => B): Option<B>;
|
502 | /**
|
503 | * Returns the result of applying `f` to this option's value if
|
504 | * the option is nonempty, otherwise returns an empty option.
|
505 | *
|
506 | * NOTE: this is similar with `map`, except that `flatMap` the
|
507 | * result returned by `f` is expected to be boxed in an `Option`
|
508 | * already.
|
509 | *
|
510 | * Example:
|
511 | *
|
512 | * ```typescript
|
513 | * const opt = Option.of(10)
|
514 | *
|
515 | * opt.flatMap(num => {
|
516 | * if (num % 2 == 0)
|
517 | * Some(num + 1)
|
518 | * else
|
519 | * None
|
520 | * })
|
521 | * ```
|
522 | *
|
523 | * @param f the mapping function that will transform the value
|
524 | * of this option if nonempty.
|
525 | *
|
526 | * @return a new option instance containing the value of the
|
527 | * source mapped by the given function
|
528 | */
|
529 | flatMap<B>(f: (a: A) => Option<B>): Option<B>;
|
530 | /** Alias for [[flatMap]]. */
|
531 | chain<B>(f: (a: A) => Option<B>): Option<B>;
|
532 | /**
|
533 | * `Applicative` apply operator.
|
534 | *
|
535 | * Resembles {@link map}, but the passed mapping function is
|
536 | * lifted in the `Either` context.
|
537 | */
|
538 | ap<B>(ff: Option<(a: A) => B>): Option<B>;
|
539 | /**
|
540 | * Returns this option if it is nonempty AND applying the
|
541 | * predicate `p` to the underlying value yields `true`,
|
542 | * otherwise return an empty option.
|
543 | *
|
544 | * @param p is the predicate function that is used to
|
545 | * apply filtering on the option's value
|
546 | *
|
547 | * @return a new option instance containing the value of the
|
548 | * source filtered with the given predicate
|
549 | */
|
550 | filter<B extends A>(p: (a: A) => a is B): Option<B>;
|
551 | filter(p: (a: A) => boolean): Option<A>;
|
552 | /**
|
553 | * Returns the result of applying `f` to this option's value,
|
554 | * or in case the option is empty, the return the result of
|
555 | * evaluating the `fallback` function.
|
556 | *
|
557 | * This function is equivalent with:
|
558 | *
|
559 | * ```typescript
|
560 | * opt.map(f).getOrElseL(fallback)
|
561 | * ```
|
562 | *
|
563 | * @param fallback is the function to be evaluated in case this
|
564 | * option is empty
|
565 | *
|
566 | * @param f is the mapping function for transforming this option's
|
567 | * value in case it is nonempty
|
568 | */
|
569 | fold<B>(fallback: () => B, f: (a: A) => B): B;
|
570 | /**
|
571 | * Returns true if this option is nonempty and the value it
|
572 | * holds is equal to the given `elem`.
|
573 | */
|
574 | contains(elem: A): boolean;
|
575 | /**
|
576 | * Returns `true` if this option is nonempty and the given
|
577 | * predicate returns `true` when applied on this option's value.
|
578 | *
|
579 | * @param p is the predicate function to test
|
580 | */
|
581 | exists(p: (a: A) => boolean): boolean;
|
582 | /**
|
583 | * Returns true if this option is empty or the given predicate
|
584 | * returns `true` when applied on this option's value.
|
585 | *
|
586 | * @param p is the predicate function to test
|
587 | */
|
588 | forAll(p: (a: A) => boolean): boolean;
|
589 | /**
|
590 | * Apply the given procedure `cb` to the option's value if
|
591 | * this option is nonempty, otherwise do nothing.
|
592 | *
|
593 | * @param cb the procedure to apply
|
594 | */
|
595 | forEach(cb: (a: A) => void): void;
|
596 | /**
|
597 | * Implements {@link IEquals.equals}.
|
598 | *
|
599 | * @param that is the right hand side of the equality check
|
600 | */
|
601 | equals(that: Option<A>): boolean;
|
602 | hashCode(): number;
|
603 | /** @hidden */ readonly _URI: "funfix/option";
|
604 | /** @hidden */ readonly _A: A;
|
605 | /** @hidden */ static readonly _Class: Option<any>;
|
606 | /**
|
607 | * Builds an [[Option]] reference that contains the given value.
|
608 | *
|
609 | * If the given value is `null` or `undefined` then the returned
|
610 | * option will be empty.
|
611 | */
|
612 | static of<A>(value: A | null | undefined): Option<A>;
|
613 | /**
|
614 | * Builds an [[Option]] reference that contains the given reference.
|
615 | *
|
616 | * Note that `value` is allowed to be `null` or `undefined`, the
|
617 | * returned option will still be non-empty. Use [[Option.of]]
|
618 | * if you want to avoid this problem. This means:
|
619 | *
|
620 | * ```typescript
|
621 | * const opt = Some<number | null>(null)
|
622 | *
|
623 | * opt.isEmpty()
|
624 | * //=> false
|
625 | *
|
626 | * opt.get()
|
627 | * //=> null
|
628 | * ```
|
629 | */
|
630 | static some<A>(value: A): Option<A>;
|
631 | /**
|
632 | * Returns an empty [[Option]].
|
633 | *
|
634 | * NOTE: Because `Option` is immutable, this function returns the
|
635 | * same cached reference is on different calls.
|
636 | */
|
637 | static none<A = never>(): Option<A>;
|
638 | /**
|
639 | * Returns an empty [[Option]].
|
640 | *
|
641 | * Similar to [[Option.none]], but this one allows specifying a
|
642 | * type parameter (in the context of TypeScript or Flow or other
|
643 | * type system).
|
644 | *
|
645 | * NOTE: Because `Option` is immutable, this function returns the
|
646 | * same cached reference is on different calls.
|
647 | */
|
648 | static empty<A>(): Option<A>;
|
649 | /**
|
650 | * Alias for [[Some]].
|
651 | */
|
652 | static pure<A>(value: A): Option<A>;
|
653 | /**
|
654 | * Maps 2 optional values by the mapping function, returning a new
|
655 | * optional reference that is `Some` only if both option values are
|
656 | * `Some`, otherwise it returns a `None`.
|
657 | *
|
658 | * ```typescript
|
659 | * // Yields Some(3)
|
660 | * Option.map2(Some(1), Some(2),
|
661 | * (a, b) => a + b
|
662 | * )
|
663 | *
|
664 | * // Yields None, because the second arg is None
|
665 | * Option.map2(Some(1), None,
|
666 | * (a, b) => a + b
|
667 | * )
|
668 | * ```
|
669 | *
|
670 | * This operation is the `Applicative.map2`.
|
671 | */
|
672 | static map2<A1, A2, R>(fa1: Option<A1>, fa2: Option<A2>, f: (a1: A1, a2: A2) => R): Option<R>;
|
673 | /**
|
674 | * Maps 3 optional values by the mapping function, returning a new
|
675 | * optional reference that is `Some` only if all 3 option values are
|
676 | * `Some`, otherwise it returns a `None`.
|
677 | *
|
678 | * ```typescript
|
679 | * // Yields Some(6)
|
680 | * Option.map3(Some(1), Some(2), Some(3),
|
681 | * (a, b, c) => a + b + c
|
682 | * )
|
683 | *
|
684 | * // Yields None, because the second arg is None
|
685 | * Option.map3(Some(1), None, Some(3),
|
686 | * (a, b, c) => a + b + c
|
687 | * )
|
688 | * ```
|
689 | */
|
690 | static map3<A1, A2, A3, R>(fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, f: (a1: A1, a2: A2, a3: A3) => R): Option<R>;
|
691 | /**
|
692 | * Maps 4 optional values by the mapping function, returning a new
|
693 | * optional reference that is `Some` only if all 4 option values are
|
694 | * `Some`, otherwise it returns a `None`.
|
695 | *
|
696 | * ```typescript
|
697 | * // Yields Some(10)
|
698 | * Option.map4(Some(1), Some(2), Some(3), Some(4),
|
699 | * (a, b, c, d) => a + b + c + d
|
700 | * )
|
701 | *
|
702 | * // Yields None, because the second arg is None
|
703 | * Option.map4(Some(1), None, Some(3), Some(4),
|
704 | * (a, b, c, d) => a + b + c + d
|
705 | * )
|
706 | * ```
|
707 | */
|
708 | static map4<A1, A2, A3, A4, R>(fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>, f: (a1: A1, a2: A2, a3: A3, a4: A4) => R): Option<R>;
|
709 | /**
|
710 | * Maps 5 optional values by the mapping function, returning a new
|
711 | * optional reference that is `Some` only if all 5 option values are
|
712 | * `Some`, otherwise it returns a `None`.
|
713 | *
|
714 | * ```typescript
|
715 | * // Yields Some(15)
|
716 | * Option.map5(Some(1), Some(2), Some(3), Some(4), Some(5),
|
717 | * (a, b, c, d, e) => a + b + c + d + e
|
718 | * )
|
719 | *
|
720 | * // Yields None, because the second arg is None
|
721 | * Option.map5(Some(1), None, Some(3), Some(4), Some(5),
|
722 | * (a, b, c, d, e) => a + b + c + d + e
|
723 | * )
|
724 | * ```
|
725 | */
|
726 | static map5<A1, A2, A3, A4, A5, R>(fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>, fa5: Option<A5>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R): Option<R>;
|
727 | /**
|
728 | * Maps 6 optional values by the mapping function, returning a new
|
729 | * optional reference that is `Some` only if all 6 option values are
|
730 | * `Some`, otherwise it returns a `None`.
|
731 | *
|
732 | * ```typescript
|
733 | * // Yields Some(21)
|
734 | * Option.map6(Some(1), Some(2), Some(3), Some(4), Some(5), Some(6),
|
735 | * (a, b, c, d, e, f) => a + b + c + d + e + f
|
736 | * )
|
737 | *
|
738 | * // Yields None, because the second arg is None
|
739 | * Option.map6(Some(1), None, Some(3), Some(4), Some(5), Some(6),
|
740 | * (a, b, c, d, e, f) => a + b + c + d + e + f
|
741 | * )
|
742 | * ```
|
743 | */
|
744 | static map6<A1, A2, A3, A4, A5, A6, R>(fa1: Option<A1>, fa2: Option<A2>, fa3: Option<A3>, fa4: Option<A4>, fa5: Option<A5>, fa6: Option<A6>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => R): Option<R>;
|
745 | /**
|
746 | * Keeps calling `f` until a `Right(b)` is returned.
|
747 | *
|
748 | * Based on Phil Freeman's
|
749 | * [Stack Safety for Free]{@link http://functorial.com/stack-safety-for-free/index.pdf}.
|
750 | *
|
751 | * Described in `FlatMap.tailRecM`.
|
752 | */
|
753 | static tailRecM<A, B>(a: A, f: (a: A) => Option<Either<A, B>>): Option<B>;
|
754 | }
|
755 | /**
|
756 | * Result of the [[Some]] data constructor, representing
|
757 | * non-empty values in the [[Option]] disjunction.
|
758 | */
|
759 | export declare class TSome<A> extends Option<A> {
|
760 | readonly value: A;
|
761 | constructor(value: A);
|
762 | }
|
763 | /**
|
764 | * The `Some<A>` data constructor for [[Option]] represents existing
|
765 | * values of type `A`.
|
766 | *
|
767 | * Using this function is equivalent with [[Option.some]].
|
768 | */
|
769 | export declare function Some<A>(value: A): TSome<A>;
|
770 | /**
|
771 | * Result of the [[Some]] data constructor, representing
|
772 | * non-empty values in the [[Option]] disjunction.
|
773 | */
|
774 | export declare class TNone extends Option<never> {
|
775 | readonly value: undefined;
|
776 | private constructor();
|
777 | }
|
778 | /**
|
779 | * The `None` data constructor for [[Option]] represents non-existing
|
780 | * values for any type.
|
781 | *
|
782 | * Using this reference directly is equivalent with [[Option.none]].
|
783 | */
|
784 | export declare const None: TNone;
|
785 | /**
|
786 | * Type enumerating the type classes implemented by `Option`.
|
787 | */
|
788 | export declare type OptionTypes = Setoid<Option<any>> & Monad<"funfix/option">;
|
789 | /**
|
790 | * Type-class implementations, compatible with the `static-land`
|
791 | * and `funland` specification.
|
792 | *
|
793 | * See [funland-js.org](https://funland-js.org).
|
794 | */
|
795 | export declare const OptionModule: OptionTypes;
|
796 | /**
|
797 | * The `Try` type represents a computation that may either result in an
|
798 | * exception, or return a successfully computed value. It's similar to,
|
799 | * but semantically different from the [[Either]] type.
|
800 | *
|
801 | * `Try` is a sum type and so instances of `Try` are either instances
|
802 | * of [[Success]] or of [[Failure]].
|
803 | *
|
804 | * For example, `Try` can be used to perform division on a user-defined
|
805 | * input, without the need to do explicit exception-handling in all of
|
806 | * the places that an exception might occur.
|
807 | *
|
808 | * Example:
|
809 | *
|
810 | * ```typescript
|
811 | * function divide(dividendS: string, divisorS: string): string {
|
812 | * const dividend = Try(() => parseInt(dividendS))
|
813 | * .filter(_ => _ === _) // filter out NaN
|
814 | * const divisor = Try(() => parseInt(divisorS))
|
815 | * .filter(_ => _ === _) // filter out NaN
|
816 | *
|
817 | * // map2 executes the given function only if both results are
|
818 | * // successful; we could also express this with flatMap / chain
|
819 | * const result = Try.map2(dividend, divisor,
|
820 | * (a, b) => a / b
|
821 | * )
|
822 | *
|
823 | * result.fold(
|
824 | * error => `failure: ${error}`
|
825 | * result => `result: ${result}`
|
826 | * )
|
827 | * }
|
828 | * ```
|
829 | *
|
830 | * An important property of `Try` is its ability to pipeline, or chain,
|
831 | * operations, catching exceptions along the way. The `flatMap` and `map`
|
832 | * combinators each essentially pass off either their successfully completed
|
833 | * value, wrapped in the [[Success]] type for it to be further operated upon
|
834 | * by the next combinator in the chain, or the exception wrapped in the
|
835 | * [[Failure]] type usually to be simply passed on down the chain.
|
836 | * Combinators such as `recover` and `recoverWith` are designed to provide
|
837 | * some type of global behavior in the case of failure.
|
838 | *
|
839 | * NOTE: all `Try` combinators will catch exceptions and return failure
|
840 | * unless otherwise specified in the documentation.
|
841 | */
|
842 | export declare class Try<A> implements std.IEquals<Try<A>>, HK<"funfix/try", A> {
|
843 | private _isSuccess;
|
844 | readonly value: Throwable | A;
|
845 | protected constructor(value: Throwable | A, tag: "failure" | "success");
|
846 | /**
|
847 | * Returns `true` if the source is a [[Success]] result,
|
848 | * or `false` in case it is a [[Failure]].
|
849 | */
|
850 | isSuccess(): this is TSuccess<A>;
|
851 | /**
|
852 | * Returns `true` if the source is a [[Failure]],
|
853 | * or `false` in case it is a [[Success]] result.
|
854 | */
|
855 | isFailure(): this is TFailure;
|
856 | /**
|
857 | * Returns a Try's successful value if it's a [[Success]] reference,
|
858 | * otherwise throws an exception if it's a [[Failure]].
|
859 | *
|
860 | * WARNING!
|
861 | *
|
862 | * This function is partial, the option must be non-empty, otherwise
|
863 | * a runtime exception will get thrown. Use with care.
|
864 | */
|
865 | get(): A;
|
866 | /**
|
867 | * Returns the value from a `Success` or the given `fallback`
|
868 | * value if this is a `Failure`.
|
869 | *
|
870 | * ```typescript
|
871 | * Success(10).getOrElse(27) // 10
|
872 | * Failure("error").getOrElse(27) // 27
|
873 | * ```
|
874 | */
|
875 | getOrElse<AA>(fallback: AA): A | AA;
|
876 | /**
|
877 | * Returns the value from a `Success` or the value generated
|
878 | * by a given `thunk` in case this is a `Failure`.
|
879 | *
|
880 | * ```typescript
|
881 | * Success(10).getOrElseL(() => 27) // 10
|
882 | * Failure("error").getOrElseL(() => 27) // 27
|
883 | * ```
|
884 | */
|
885 | getOrElseL<AA>(thunk: () => AA): A | AA;
|
886 | /**
|
887 | * Returns the current value if it's a [[Success]], or
|
888 | * if the source is a [[Failure]] then return `null`.
|
889 | *
|
890 | * ```typescript
|
891 | * Success(10).orNull() // 10
|
892 | * Failure("error").orNull() // null
|
893 | * ```
|
894 | *
|
895 | * This can be useful for use-cases such as:
|
896 | *
|
897 | * ```typescript
|
898 | * Try.of(() => dict.user.profile.name).orNull()
|
899 | * ```
|
900 | */
|
901 | orNull(): A | null;
|
902 | /**
|
903 | * Returns the current value if it's a [[Success]], or
|
904 | * if the source is a [[Failure]] then return `undefined`.
|
905 | *
|
906 | * ```typescript
|
907 | * Success(10).orUndefined() // 10
|
908 | * Failure("error").orUndefined() // undefined
|
909 | * ```
|
910 | *
|
911 | * This can be useful for use-cases such as:
|
912 | *
|
913 | * ```typescript
|
914 | * Try.of(() => dict.user.profile.name).orUndefined()
|
915 | * ```
|
916 | */
|
917 | orUndefined(): A | undefined;
|
918 | /**
|
919 | * Returns the current value if it's a [[Success]], or if
|
920 | * the source is a [[Failure]] then return the `fallback`.
|
921 | *
|
922 | * ```typescript
|
923 | * Success(10).orElse(Success(17)) // 10
|
924 | * Failure("error").orElse(Success(17)) // 17
|
925 | * ```
|
926 | */
|
927 | orElse<AA>(fallback: Try<AA>): Try<A | AA>;
|
928 | /**
|
929 | * Returns the current value if it's a [[Success]], or if the source
|
930 | * is a [[Failure]] then return the value generated by the given
|
931 | * `thunk`.
|
932 | *
|
933 | * ```typescript
|
934 | * Success(10).orElseL(() => Success(17)) // 10
|
935 | * Failure("error").orElseL(() => Success(17)) // 17
|
936 | * ```
|
937 | */
|
938 | orElseL<AA>(thunk: () => Try<AA>): Try<A | AA>;
|
939 | /**
|
940 | * Inverts this `Try`. If this is a [[Failure]], returns its exception wrapped
|
941 | * in a [[Success]]. If this is a `Success`, returns a `Failure` containing a
|
942 | * [[NoSuchElementError]].
|
943 | */
|
944 | failed(): Try<Throwable>;
|
945 | /**
|
946 | * Applies the `failure` function to [[Failure]] values, and the
|
947 | * `success` function to [[Success]] values and returns the result.
|
948 | *
|
949 | * ```typescript
|
950 | * const maybeNum: Try<number> =
|
951 | * tryParseInt("not a number")
|
952 | *
|
953 | * const result: string =
|
954 | * maybeNum.fold(
|
955 | * error => `Could not parse string: ${error}`,
|
956 | * num => `Success: ${num}`
|
957 | * )
|
958 | * ```
|
959 | */
|
960 | fold<R>(failure: (error: Throwable) => R, success: (a: A) => R): R;
|
961 | /**
|
962 | * Returns a [[Failure]] if the source is a [[Success]], but the
|
963 | * given `p` predicate is not satisfied.
|
964 | *
|
965 | * @throws NoSuchElementError in case the predicate doesn't hold
|
966 | */
|
967 | filter<B extends A>(p: (a: A) => a is B): Try<B>;
|
968 | filter(p: (a: A) => boolean): Try<A>;
|
969 | /**
|
970 | * Returns the given function applied to the value if this is
|
971 | * a [[Success]] or returns `this` if this is a [[Failure]].
|
972 | *
|
973 | * This operation is the monadic "bind" operation.
|
974 | * It can be used to *chain* multiple `Try` references.
|
975 | *
|
976 | * ```typescript
|
977 | * Try.of(() => parse(s1)).flatMap(num1 =>
|
978 | * Try.of(() => parse(s2)).map(num2 =>
|
979 | * num1 / num2
|
980 | * ))
|
981 | * ```
|
982 | */
|
983 | flatMap<B>(f: (a: A) => Try<B>): Try<B>;
|
984 | /** Alias for [[flatMap]]. */
|
985 | chain<B>(f: (a: A) => Try<B>): Try<B>;
|
986 | /**
|
987 | * `Applicative` apply operator.
|
988 | *
|
989 | * Resembles {@link map}, but the passed mapping function is
|
990 | * lifted in the `Either` context.
|
991 | */
|
992 | ap<B>(ff: Try<(a: A) => B>): Try<B>;
|
993 | /**
|
994 | * Returns a `Try` containing the result of applying `f` to
|
995 | * this option's value, but only if it's a `Success`, or
|
996 | * returns the current `Failure` without any modifications.
|
997 | *
|
998 | * NOTE: this is similar with `flatMap`, except with `map` the
|
999 | * result of `f` doesn't need to be wrapped in a `Try`.
|
1000 | *
|
1001 | * @param f the mapping function that will transform the value
|
1002 | * of this `Try` if successful.
|
1003 | *
|
1004 | * @return a new `Try` instance containing the value of the
|
1005 | * source mapped by the given function
|
1006 | */
|
1007 | map<B>(f: (a: A) => B): Try<B>;
|
1008 | /**
|
1009 | * Applies the given function `cb` if this is a [[Success]], otherwise
|
1010 | * returns `void` if this is a [[Failure]].
|
1011 | */
|
1012 | forEach(cb: (a: A) => void): void;
|
1013 | /**
|
1014 | * Applies the given function `f` if this is a `Failure`, otherwise
|
1015 | * returns `this` if this is a `Success`.
|
1016 | *
|
1017 | * This is like `map` for the exception.
|
1018 | *
|
1019 | * In the following example, if the `user.profile.email` exists,
|
1020 | * then it is returned as a successful value, otherwise
|
1021 | *
|
1022 | * ```typescript
|
1023 | * Try.of(() => user.profile.email).recover(e => {
|
1024 | * // Access error? Default to empty.
|
1025 | * if (e instanceof TypeError) return ""
|
1026 | * throw e // We don't know what it is, rethrow
|
1027 | * })
|
1028 | *
|
1029 | * Note that on rethrow, the error is being caught in `recover` and
|
1030 | * it still returns it as a `Failure(e)`.
|
1031 | * ```
|
1032 | */
|
1033 | recover<AA>(f: (error: Throwable) => AA): Try<A | AA>;
|
1034 | /**
|
1035 | * Applies the given function `f` if this is a `Failure`, otherwise
|
1036 | * returns `this` if this is a `Success`.
|
1037 | *
|
1038 | * This is like `map` for the exception.
|
1039 | *
|
1040 | * In the following example, if the `user.profile.email` exists,
|
1041 | * then it is returned as a successful value, otherwise
|
1042 | *
|
1043 | * ```typescript
|
1044 | * Try.of(() => user.profile.email).recover(e => {
|
1045 | * // Access error? Default to empty.
|
1046 | * if (e instanceof TypeError) return ""
|
1047 | * throw e // We don't know what it is, rethrow
|
1048 | * })
|
1049 | *
|
1050 | * Note that on rethrow, the error is being caught in `recover` and
|
1051 | * it still returns it as a `Failure(e)`.
|
1052 | * ```
|
1053 | */
|
1054 | recoverWith<AA>(f: (error: Throwable) => Try<AA>): Try<A | AA>;
|
1055 | /**
|
1056 | * Transforms the source into an [[Option]].
|
1057 | *
|
1058 | * In case the source is a `Success(v)`, then it gets translated
|
1059 | * into a `Some(v)`. If the source is a `Failure(e)`, then a `None`
|
1060 | * value is returned.
|
1061 | *
|
1062 | * ```typescript
|
1063 | * Success("value").toOption() // Some("value")
|
1064 | * Failure("error").toOption() // None
|
1065 | * ```
|
1066 | */
|
1067 | toOption(): Option<A>;
|
1068 | /**
|
1069 | * Transforms the source into an [[Either]].
|
1070 | *
|
1071 | * In case the source is a `Success(v)`, then it gets translated
|
1072 | * into a `Right(v)`. If the source is a `Failure(e)`, then a `Left(e)`
|
1073 | * value is returned.
|
1074 | *
|
1075 | * ```typescript
|
1076 | * Success("value").toEither() // Right("value")
|
1077 | * Failure("error").toEither() // Left("error")
|
1078 | * ```
|
1079 | */
|
1080 | toEither(): Either<Throwable, A>;
|
1081 | /**
|
1082 | * Implements {@link IEquals.equals} with overridable equality for `A`.
|
1083 | */
|
1084 | equals(that: Try<A>): boolean;
|
1085 | hashCode(): number;
|
1086 | /** @hidden */ readonly _URI: "funfix/try";
|
1087 | /** @hidden */ readonly _A: A;
|
1088 | /** @hidden */ static readonly _Class: Try<any>;
|
1089 | /**
|
1090 | * Evaluates the given `thunk` and returns either a [[Success]],
|
1091 | * in case the evaluation succeeded, or a [[Failure]], in case
|
1092 | * an exception was thrown.
|
1093 | *
|
1094 | * Example:
|
1095 | *
|
1096 | * ```typescript
|
1097 | * let effect = 0
|
1098 | *
|
1099 | * const e = Try.of(() => { effect += 1; return effect })
|
1100 | * e.get() // 1
|
1101 | * ```
|
1102 | */
|
1103 | static of<A>(thunk: () => A): Try<A>;
|
1104 | /** Alias of [[Try.success]]. */
|
1105 | static pure<A>(value: A): Try<A>;
|
1106 | /**
|
1107 | * Shorthand for `now(undefined as void)`, always returning
|
1108 | * the same reference as optimization.
|
1109 | */
|
1110 | static unit(): Try<void>;
|
1111 | /**
|
1112 | * Returns a [[Try]] reference that represents a successful result
|
1113 | * (i.e. wrapped in [[Success]]).
|
1114 | */
|
1115 | static success<A>(value: A): Try<A>;
|
1116 | /**
|
1117 | * Returns a [[Try]] reference that represents a failure
|
1118 | * (i.e. an exception wrapped in [[Failure]]).
|
1119 | */
|
1120 | static failure<A = never>(e: Throwable): Try<A>;
|
1121 | /**
|
1122 | * Alias for {@link Try.failure} and {@link Failure},
|
1123 | * wrapping any throwable into a `Try` value.
|
1124 | */
|
1125 | static raise<A = never>(e: Throwable): Try<A>;
|
1126 | /**
|
1127 | * Maps 2 `Try` values by the mapping function, returning a new
|
1128 | * `Try` reference that is a `Success` only if both `Try` values are
|
1129 | * a `Success`, otherwise it returns the first `Failure` noticed.
|
1130 | *
|
1131 | * ```typescript
|
1132 | * // Yields Success(3)
|
1133 | * Try.map2(Success(1), Success(2),
|
1134 | * (a, b) => a + b
|
1135 | * )
|
1136 | *
|
1137 | * // Yields Failure, because the second arg is a Failure
|
1138 | * Try.map2(Success(1), Failure("error"),
|
1139 | * (a, b) => a + b
|
1140 | * )
|
1141 | * ```
|
1142 | *
|
1143 | * This operation is the `Applicative.map2`.
|
1144 | */
|
1145 | static map2<A1, A2, R>(fa1: Try<A1>, fa2: Try<A2>, f: (a1: A1, a2: A2) => R): Try<R>;
|
1146 | /**
|
1147 | * Maps 3 `Try` values by the mapping function, returning a new
|
1148 | * `Try` reference that is a `Success` only if all 3 `Try` values are
|
1149 | * a `Success`, otherwise it returns the first `Failure` noticed.
|
1150 | *
|
1151 | * ```typescript
|
1152 | * // Yields Success(6)
|
1153 | * Try.map3(Success(1), Success(2), Success(3),
|
1154 | * (a, b, c) => {
|
1155 | * return a + b + c
|
1156 | * }
|
1157 | * )
|
1158 | *
|
1159 | * // Yields Failure, because the second arg is a Failure
|
1160 | * Try.map3(
|
1161 | * Success(1),
|
1162 | * Failure("error"),
|
1163 | * Success(3),
|
1164 | *
|
1165 | * (a, b, c) => {
|
1166 | * return a + b + c
|
1167 | * }
|
1168 | * )
|
1169 | * ```
|
1170 | */
|
1171 | static map3<A1, A2, A3, R>(fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, f: (a1: A1, a2: A2, a3: A3) => R): Try<R>;
|
1172 | /**
|
1173 | * Maps 4 `Try` values by the mapping function, returning a new
|
1174 | * `Try` reference that is a `Success` only if all 4 `Try` values are
|
1175 | * a `Success`, otherwise it returns the first `Failure` noticed.
|
1176 | *
|
1177 | * ```typescript
|
1178 | * // Yields Success(10)
|
1179 | * Try.map4(Success(1), Success(2), Success(3), Success(4),
|
1180 | * (a, b, c, d) => {
|
1181 | * return a + b + c + d
|
1182 | * }
|
1183 | * )
|
1184 | *
|
1185 | * // Yields Failure, because the second arg is a Failure
|
1186 | * Try.map3(
|
1187 | * Success(1),
|
1188 | * Failure("error"),
|
1189 | * Success(3),
|
1190 | * Success(4),
|
1191 | *
|
1192 | * (a, b, c, d) => {
|
1193 | * return a + b + c + d
|
1194 | * }
|
1195 | * )
|
1196 | * ```
|
1197 | */
|
1198 | static map4<A1, A2, A3, A4, R>(fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>, f: (a1: A1, a2: A2, a3: A3, a4: A4) => R): Try<R>;
|
1199 | /**
|
1200 | * Maps 5 `Try` values by the mapping function, returning a new
|
1201 | * `Try` reference that is a `Success` only if all 5 `Try` values are
|
1202 | * a `Success`, otherwise it returns the first `Failure` noticed.
|
1203 | *
|
1204 | * ```typescript
|
1205 | * // Yields Success(15)
|
1206 | * Try.map5(
|
1207 | * Success(1),
|
1208 | * Success(2),
|
1209 | * Success(3),
|
1210 | * Success(4),
|
1211 | * Success(5),
|
1212 | *
|
1213 | * (a, b, c, d, e) => {
|
1214 | * return a + b + c + d + e
|
1215 | * }
|
1216 | * )
|
1217 | *
|
1218 | * // Yields Failure, because the second arg is a Failure
|
1219 | * Try.map5(
|
1220 | * Success(1),
|
1221 | * Failure("error"),
|
1222 | * Success(3),
|
1223 | * Success(4),
|
1224 | * Success(5),
|
1225 | *
|
1226 | * (a, b, c, d, e) => {
|
1227 | * return a + b + c + d + e
|
1228 | * }
|
1229 | * )
|
1230 | * ```
|
1231 | */
|
1232 | static map5<A1, A2, A3, A4, A5, R>(fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>, fa5: Try<A5>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => R): Try<R>;
|
1233 | /**
|
1234 | * Maps 6 `Try` values by the mapping function, returning a new
|
1235 | * `Try` reference that is a `Success` only if all 6 `Try` values are
|
1236 | * a `Success`, otherwise it returns the first `Failure` noticed.
|
1237 | *
|
1238 | * ```typescript
|
1239 | * // Yields Success(21)
|
1240 | * Try.map6(
|
1241 | * Success(1),
|
1242 | * Success(2),
|
1243 | * Success(3),
|
1244 | * Success(4),
|
1245 | * Success(5),
|
1246 | * Success(6),
|
1247 | *
|
1248 | * (a, b, c, d, e, f) => {
|
1249 | * return a + b + c + d + e + f
|
1250 | * }
|
1251 | * )
|
1252 | *
|
1253 | * // Yields Failure, because the second arg is a Failure
|
1254 | * Try.map6(
|
1255 | * Success(1),
|
1256 | * Failure("error"),
|
1257 | * Success(3),
|
1258 | * Success(4),
|
1259 | * Success(5),
|
1260 | * Success(6),
|
1261 | *
|
1262 | * (a, b, c, d, e, f) => {
|
1263 | * return a + b + c + d + e + f
|
1264 | * }
|
1265 | * )
|
1266 | * ```
|
1267 | */
|
1268 | static map6<A1, A2, A3, A4, A5, A6, R>(fa1: Try<A1>, fa2: Try<A2>, fa3: Try<A3>, fa4: Try<A4>, fa5: Try<A5>, fa6: Try<A6>, f: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => R): Try<R>;
|
1269 | /**
|
1270 | * Keeps calling `f` until a `Right(b)` is returned.
|
1271 | *
|
1272 | * Based on Phil Freeman's
|
1273 | * [Stack Safety for Free]{@link http://functorial.com/stack-safety-for-free/index.pdf}.
|
1274 | *
|
1275 | * Described in `FlatMap.tailRecM`.
|
1276 | */
|
1277 | static tailRecM<A, B>(a: A, f: (a: A) => Try<Either<A, B>>): Try<B>;
|
1278 | }
|
1279 | /**
|
1280 | * Result of the [[Success]] data constructor, representing
|
1281 | * successful values in the [[Try]] disjunction.
|
1282 | *
|
1283 | * @final
|
1284 | */
|
1285 | export declare class TSuccess<A> extends Try<A> {
|
1286 | readonly value: A;
|
1287 | constructor(value: A);
|
1288 | }
|
1289 | /**
|
1290 | * The `Success` data constructor is for building [[Try]] values that
|
1291 | * are successful results of computations, as opposed to [[Failure]].
|
1292 | */
|
1293 | export declare function Success<A>(value: A): Try<A>;
|
1294 | /**
|
1295 | * The `Success` data constructor is for building [[Try]] values that
|
1296 | * are successful results of computations, as opposed to [[Failure]].
|
1297 | *
|
1298 | * @final
|
1299 | */
|
1300 | export declare class TFailure extends Try<never> {
|
1301 | readonly value: Throwable;
|
1302 | constructor(value: Throwable);
|
1303 | }
|
1304 | /**
|
1305 | * The `Failure` data constructor is for building [[Try]] values that
|
1306 | * represent failures, as opposed to [[Success]].
|
1307 | */
|
1308 | export declare function Failure(e: Throwable): Try<never>;
|
1309 | /**
|
1310 | * Type enumerating the type classes implemented by `Try`.
|
1311 | */
|
1312 | export declare type TryTypes = Setoid<Try<any>> & Monad<"funfix/try">;
|
1313 | /**
|
1314 | * Type-class implementations, compatible with the `static-land`
|
1315 | * and `funland` specifications.
|
1316 | *
|
1317 | * See [funland-js.org](https://funland-js.org).
|
1318 | */
|
1319 | export declare const TryModule: TryTypes;
|
1320 |
|
\ | No newline at end of file |