-
-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathTaskEither.ts
1881 lines (1715 loc) · 49.8 KB
/
TaskEither.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ```ts
* interface TaskEither<E, A> extends Task<Either<E, A>> {}
* ```
*
* `TaskEither<E, A>` represents an asynchronous computation that either yields a value of type `A` or fails yielding an
* error of type `E`. If you want to represent an asynchronous computation that never fails, please see `Task`.
*
* @since 2.0.0
*/
import { Alt2, Alt2C } from './Alt'
import { Applicative2, Applicative2C, getApplicativeMonoid } from './Applicative'
import {
ap as ap_,
apFirst as apFirst_,
Apply1,
Apply2,
apS as apS_,
apSecond as apSecond_,
getApplySemigroup as getApplySemigroup_
} from './Apply'
import { Bifunctor2 } from './Bifunctor'
import * as chainable from './Chain'
import { compact as compact_, Compactable2C, separate as separate_ } from './Compactable'
import * as E from './Either'
import * as ET from './EitherT'
import {
filter as filter_,
Filterable2C,
filterMap as filterMap_,
partition as partition_,
partitionMap as partitionMap_
} from './Filterable'
import {
chainOptionK as chainOptionK_,
filterOrElse as filterOrElse_,
FromEither2,
fromEitherK as fromEitherK_,
fromOption as fromOption_,
fromOptionK as fromOptionK_,
fromPredicate as fromPredicate_,
tapEither as tapEither_
} from './FromEither'
import { FromIO2, fromIOK as fromIOK_, tapIO as tapIO_ } from './FromIO'
import { FromTask2, fromTaskK as fromTaskK_, tapTask as tapTask_ } from './FromTask'
import { dual, flow, identity, LazyArg, pipe, SK } from './function'
import { as as as_, asUnit as asUnit_, bindTo as bindTo_, flap as flap_, Functor2, let as let__ } from './Functor'
import * as _ from './internal'
import { IO } from './IO'
import { IOEither } from './IOEither'
import { Monad2, Monad2C } from './Monad'
import { MonadIO2 } from './MonadIO'
import { MonadTask2, MonadTask2C } from './MonadTask'
import { MonadThrow2, MonadThrow2C } from './MonadThrow'
import { Monoid } from './Monoid'
import { NonEmptyArray } from './NonEmptyArray'
import { Option } from './Option'
import { Pointed2 } from './Pointed'
import { Predicate } from './Predicate'
import { ReadonlyNonEmptyArray } from './ReadonlyNonEmptyArray'
import { Refinement } from './Refinement'
import { Semigroup } from './Semigroup'
import * as T from './Task'
import { TaskOption } from './TaskOption'
// -------------------------------------------------------------------------------------
// model
// -------------------------------------------------------------------------------------
import Either = E.Either
import Task = T.Task
/**
* @category model
* @since 2.0.0
*/
export interface TaskEither<E, A> extends Task<Either<E, A>> {}
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.0.0
*/
export const left: <E = never, A = never>(e: E) => TaskEither<E, A> = /*#__PURE__*/ ET.left(T.Pointed)
/**
* @category constructors
* @since 2.0.0
*/
export const right: <E = never, A = never>(a: A) => TaskEither<E, A> = /*#__PURE__*/ ET.right(T.Pointed)
/**
* @category constructors
* @since 2.0.0
*/
export const rightTask: <E = never, A = never>(ma: Task<A>) => TaskEither<E, A> = /*#__PURE__*/ ET.rightF(T.Functor)
/**
* @category constructors
* @since 2.0.0
*/
export const leftTask: <E = never, A = never>(me: Task<E>) => TaskEither<E, A> = /*#__PURE__*/ ET.leftF(T.Functor)
/**
* @category constructors
* @since 2.0.0
*/
export const rightIO: <E = never, A = never>(ma: IO<A>) => TaskEither<E, A> = /*#__PURE__*/ flow(T.fromIO, rightTask)
/**
* @category constructors
* @since 2.0.0
*/
export const leftIO: <E = never, A = never>(me: IO<E>) => TaskEither<E, A> = /*#__PURE__*/ flow(T.fromIO, leftTask)
// -------------------------------------------------------------------------------------
// conversions
// -------------------------------------------------------------------------------------
/**
* @category conversions
* @since 2.7.0
*/
export const fromIO: <A, E = never>(fa: IO<A>) => TaskEither<E, A> = rightIO
/**
* @category conversions
* @since 2.7.0
*/
export const fromTask: <A, E = never>(fa: Task<A>) => TaskEither<E, A> = rightTask
/**
* @category conversions
* @since 2.0.0
*/
export const fromEither: <E, A>(fa: Either<E, A>) => TaskEither<E, A> = T.of
/**
* @category conversions
* @since 2.0.0
*/
export const fromIOEither: <E, A>(fa: IOEither<E, A>) => TaskEither<E, A> = T.fromIO
/**
* @category conversions
* @since 2.11.0
*/
export const fromTaskOption: <E>(onNone: LazyArg<E>) => <A>(fa: TaskOption<A>) => TaskEither<E, A> = (onNone) =>
T.map(E.fromOption(onNone))
/**
* @category pattern matching
* @since 2.10.0
*/
export const match: <E, B, A>(onLeft: (e: E) => B, onRight: (a: A) => B) => (ma: TaskEither<E, A>) => Task<B> =
/*#__PURE__*/ ET.match(T.Functor)
/**
* Less strict version of [`match`](#match).
*
* The `W` suffix (short for **W**idening) means that the handler return types will be merged.
*
* @category pattern matching
* @since 2.10.0
*/
export const matchW: <E, B, A, C>(onLeft: (e: E) => B, onRight: (a: A) => C) => (ma: TaskEither<E, A>) => Task<B | C> =
match as any
/**
* The `E` suffix (short for **E**ffect) means that the handlers return an effect (`Task`).
*
* @category pattern matching
* @since 2.10.0
*/
export const matchE: <E, A, B>(
onLeft: (e: E) => Task<B>,
onRight: (a: A) => Task<B>
) => (ma: TaskEither<E, A>) => Task<B> = /*#__PURE__*/ ET.matchE(T.Monad)
/**
* Alias of [`matchE`](#matche).
*
* @category pattern matching
* @since 2.0.0
*/
export const fold = matchE
/**
* Less strict version of [`matchE`](#matche).
*
* The `W` suffix (short for **W**idening) means that the handler return types will be merged.
*
* @category pattern matching
* @since 2.10.0
*/
export const matchEW: <E, B, A, C>(
onLeft: (e: E) => Task<B>,
onRight: (a: A) => Task<C>
) => (ma: TaskEither<E, A>) => Task<B | C> = matchE as any
/**
* Alias of [`matchEW`](#matchew).
*
* @category pattern matching
* @since 2.10.0
*/
export const foldW = matchEW
/**
* @category error handling
* @since 2.0.0
*/
export const getOrElse: <E, A>(onLeft: (e: E) => Task<A>) => (ma: TaskEither<E, A>) => Task<A> =
/*#__PURE__*/ ET.getOrElse(T.Monad)
/**
* Less strict version of [`getOrElse`](#getorelse).
*
* The `W` suffix (short for **W**idening) means that the handler return type will be merged.
*
* @category error handling
* @since 2.6.0
*/
export const getOrElseW: <E, B>(onLeft: (e: E) => Task<B>) => <A>(ma: TaskEither<E, A>) => Task<A | B> =
getOrElse as any
/**
* Transforms a `Promise` that may reject to a `Promise` that never rejects and returns an `Either` instead.
*
* See also [`tryCatchK`](#trycatchk).
*
* @example
* import { left, right } from 'fp-ts/Either'
* import { tryCatch } from 'fp-ts/TaskEither'
*
* tryCatch(() => Promise.resolve(1), String)().then(result => {
* assert.deepStrictEqual(result, right(1))
* })
* tryCatch(() => Promise.reject('error'), String)().then(result => {
* assert.deepStrictEqual(result, left('error'))
* })
*
* @category interop
* @since 2.0.0
*/
export const tryCatch =
<E, A>(f: LazyArg<Promise<A>>, onRejected: (reason: unknown) => E): TaskEither<E, A> =>
async () => {
try {
return await f().then(_.right)
} catch (reason) {
return _.left(onRejected(reason))
}
}
/**
* Converts a function returning a `Promise` to one returning a `TaskEither`.
*
* @category interop
* @since 2.5.0
*/
export const tryCatchK =
<E, A extends ReadonlyArray<unknown>, B>(
f: (...a: A) => Promise<B>,
onRejected: (reason: unknown) => E
): ((...a: A) => TaskEither<E, B>) =>
(...a) =>
tryCatch(() => f(...a), onRejected)
/**
* @category conversions
* @since 2.10.0
*/
export const toUnion: <E, A>(fa: TaskEither<E, A>) => Task<E | A> = /*#__PURE__*/ ET.toUnion(T.Functor)
/**
* @category conversions
* @since 2.12.0
*/
export const fromNullable: <E>(e: E) => <A>(a: A) => TaskEither<E, NonNullable<A>> = /*#__PURE__*/ ET.fromNullable(
T.Pointed
)
/**
* Use `liftNullable`.
*
* @category legacy
* @since 2.12.0
*/
export const fromNullableK: <E>(
e: E
) => <A extends ReadonlyArray<unknown>, B>(
f: (...a: A) => B | null | undefined
) => (...a: A) => TaskEither<E, NonNullable<B>> = /*#__PURE__*/ ET.fromNullableK(T.Pointed)
/**
* Use `flatMapNullable`.
*
* @category legacy
* @since 2.12.0
*/
export const chainNullableK: <E>(
e: E
) => <A, B>(f: (a: A) => B | null | undefined) => (ma: TaskEither<E, A>) => TaskEither<E, NonNullable<B>> =
/*#__PURE__*/ ET.chainNullableK(T.Monad)
// -------------------------------------------------------------------------------------
// combinators
// -------------------------------------------------------------------------------------
/**
* Returns `ma` if is a `Right` or the value returned by `onLeft` otherwise.
*
* See also [alt](#alt).
*
* @example
* import * as E from 'fp-ts/Either'
* import { pipe } from 'fp-ts/function'
* import * as TE from 'fp-ts/TaskEither'
*
* async function test() {
* const errorHandler = TE.orElse((error: string) => TE.right(`recovering from ${error}...`))
* assert.deepStrictEqual(await pipe(TE.right('ok'), errorHandler)(), E.right('ok'))
* assert.deepStrictEqual(await pipe(TE.left('ko'), errorHandler)(), E.right('recovering from ko...'))
* }
*
* test()
*
* @category error handling
* @since 2.0.0
*/
export const orElse: <E1, A, E2>(onLeft: (e: E1) => TaskEither<E2, A>) => (ma: TaskEither<E1, A>) => TaskEither<E2, A> =
/*#__PURE__*/ ET.orElse(T.Monad)
/**
* Less strict version of [`orElse`](#orelse).
*
* The `W` suffix (short for **W**idening) means that the return types will be merged.
*
* @category error handling
* @since 2.10.0
*/
export const orElseW: <E1, E2, B>(
onLeft: (e: E1) => TaskEither<E2, B>
) => <A>(ma: TaskEither<E1, A>) => TaskEither<E2, A | B> = orElse as any
/**
* Returns an effect that effectfully "peeks" at the failure of this effect.
*
* @category error handling
* @since 2.15.0
*/
export const tapError: {
<E1, E2, _>(onLeft: (e: E1) => TaskEither<E2, _>): <A>(self: TaskEither<E1, A>) => TaskEither<E1 | E2, A>
<E1, A, E2, _>(self: TaskEither<E1, A>, onLeft: (e: E1) => TaskEither<E2, _>): TaskEither<E1 | E2, A>
} = /*#__PURE__*/ dual(2, ET.tapError(T.Monad))
/**
* @category error handling
* @since 2.12.0
*/
export const orElseFirstIOK: <E, B>(onLeft: (e: E) => IO<B>) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A> = (
onLeft
) => tapError(fromIOK(onLeft))
/**
* @category error handling
* @since 2.12.0
*/
export const orElseFirstTaskK: <E, B>(onLeft: (e: E) => Task<B>) => <A>(ma: TaskEither<E, A>) => TaskEither<E, A> = (
onLeft
) => tapError(fromTaskK(onLeft))
/**
* @category error handling
* @since 2.11.0
*/
export const orLeft: <E1, E2>(onLeft: (e: E1) => Task<E2>) => <A>(fa: TaskEither<E1, A>) => TaskEither<E2, A> =
/*#__PURE__*/ ET.orLeft(T.Monad)
/**
* @since 2.0.0
*/
export const swap: <E, A>(ma: TaskEither<E, A>) => TaskEither<A, E> = /*#__PURE__*/ ET.swap(T.Functor)
/**
* @category lifting
* @since 2.11.0
*/
export const fromTaskOptionK = <E>(
onNone: LazyArg<E>
): (<A extends ReadonlyArray<unknown>, B>(f: (...a: A) => TaskOption<B>) => (...a: A) => TaskEither<E, B>) => {
const from = fromTaskOption(onNone)
return (f) => flow(f, from)
}
/**
* Use `flatMapTaskOption`.
*
* The `W` suffix (short for **W**idening) means that the error types will be merged.
*
* @category legacy
* @since 2.12.3
*/
export const chainTaskOptionKW =
<E2>(onNone: LazyArg<E2>) =>
<A, B>(f: (a: A) => TaskOption<B>) =>
<E1>(ma: TaskEither<E1, A>): TaskEither<E1 | E2, B> =>
flatMap(ma, fromTaskOptionK<E1 | E2>(onNone)(f))
/**
* Use `flatMapTaskOption`.
*
* @category legacy
* @since 2.11.0
*/
export const chainTaskOptionK: <E>(
onNone: LazyArg<E>
) => <A, B>(f: (a: A) => TaskOption<B>) => (ma: TaskEither<E, A>) => TaskEither<E, B> = chainTaskOptionKW
/**
* @category lifting
* @since 2.4.0
*/
export const fromIOEitherK = <E, A extends ReadonlyArray<unknown>, B>(
f: (...a: A) => IOEither<E, B>
): ((...a: A) => TaskEither<E, B>) => flow(f, fromIOEither)
const _map: Functor2<URI>['map'] = (fa, f) => pipe(fa, map(f))
const _apPar: Apply2<URI>['ap'] = (fab, fa) => pipe(fab, ap(fa))
const _apSeq: Apply2<URI>['ap'] = (fab, fa) => flatMap(fab, (f) => pipe(fa, map(f)))
/* istanbul ignore next */
const _alt: Alt2<URI>['alt'] = (fa, that) => pipe(fa, alt(that))
/**
* `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
* use the type constructor `F` to represent some computational context.
*
* @category mapping
* @since 2.0.0
*/
export const map: <A, B>(f: (a: A) => B) => <E>(fa: TaskEither<E, A>) => TaskEither<E, B> = /*#__PURE__*/ ET.map(
T.Functor
)
/**
* Returns a `TaskEither` whose failure and success channels have been mapped by the specified pair of functions, `f` and `g`.
*
* @example
* import * as TaskEither from 'fp-ts/TaskEither'
* import * as Either from 'fp-ts/Either'
*
* const f = (s: string) => new Error(s)
* const g = (n: number) => n * 2
*
* async function test() {
* assert.deepStrictEqual(await TaskEither.mapBoth(TaskEither.right(1), f, g)(), Either.right(2))
* assert.deepStrictEqual(await TaskEither.mapBoth(TaskEither.left('err'), f, g)(), Either.left(new Error('err')))
* }
*
* test()
*
* @category error handling
* @since 2.16.0
*/
export const mapBoth: {
<E, G, A, B>(f: (e: E) => G, g: (a: A) => B): (self: TaskEither<E, A>) => TaskEither<G, B>
<E, A, G, B>(self: TaskEither<E, A>, f: (e: E) => G, g: (a: A) => B): TaskEither<G, B>
} = /*#__PURE__*/ dual(3, ET.mapBoth(T.Functor))
/**
* Alias of `mapBoth`.
*
* @category legacy
* @since 2.0.0
*/
export const bimap: <E, G, A, B>(f: (e: E) => G, g: (a: A) => B) => (fa: TaskEither<E, A>) => TaskEither<G, B> = mapBoth
/**
* Returns a `TaskEither` with its error channel mapped using the specified function.
*
* @example
* import * as TaskEither from 'fp-ts/TaskEither'
* import * as Either from 'fp-ts/Either'
*
* const f = (s: string) => new Error(s)
*
* async function test() {
* assert.deepStrictEqual(await TaskEither.mapError(TaskEither.right(1), f)(), Either.right(1))
* assert.deepStrictEqual(await TaskEither.mapError(TaskEither.left('err'), f)(), Either.left(new Error('err')))
* }
*
* test()
*
* @category error handling
* @since 2.16.0
*/
export const mapError: {
<E, G>(f: (e: E) => G): <A>(self: TaskEither<E, A>) => TaskEither<G, A>
<E, A, G>(self: TaskEither<E, A>, f: (e: E) => G): TaskEither<G, A>
} = /*#__PURE__*/ dual(2, ET.mapError(T.Functor))
/**
* Alias of `mapError`.
*
* @category legacy
* @since 2.0.0
*/
export const mapLeft: <E, G>(f: (e: E) => G) => <A>(fa: TaskEither<E, A>) => TaskEither<G, A> = mapError
/**
* @since 2.0.0
*/
export const ap: <E, A>(fa: TaskEither<E, A>) => <B>(fab: TaskEither<E, (a: A) => B>) => TaskEither<E, B> =
/*#__PURE__*/ ET.ap(T.ApplyPar)
/**
* Less strict version of [`ap`](#ap).
*
* The `W` suffix (short for **W**idening) means that the error types will be merged.
*
* @since 2.8.0
*/
export const apW: <E2, A>(
fa: TaskEither<E2, A>
) => <E1, B>(fab: TaskEither<E1, (a: A) => B>) => TaskEither<E1 | E2, B> = ap as any
/**
* @category sequencing
* @since 2.14.0
*/
export const flatMap: {
<A, E2, B>(f: (a: A) => TaskEither<E2, B>): <E1>(ma: TaskEither<E1, A>) => TaskEither<E1 | E2, B>
<E1, A, E2, B>(ma: TaskEither<E1, A>, f: (a: A) => TaskEither<E2, B>): TaskEither<E1 | E2, B>
} = /*#__PURE__*/ dual(2, ET.flatMap(T.Monad))
/**
* Less strict version of [`flatten`](#flatten).
*
* The `W` suffix (short for **W**idening) means that the error types will be merged.
*
* @category sequencing
* @since 2.11.0
*/
export const flattenW: <E1, E2, A>(mma: TaskEither<E1, TaskEither<E2, A>>) => TaskEither<E1 | E2, A> =
/*#__PURE__*/ flatMap(identity)
/**
* @category sequencing
* @since 2.0.0
*/
export const flatten: <E, A>(mma: TaskEither<E, TaskEither<E, A>>) => TaskEither<E, A> = flattenW
/**
* Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
* types of kind `* -> *`.
*
* In case of `TaskEither` returns `fa` if is a `Right` or the value returned by `that` otherwise.
*
* See also [orElse](#orelse).
*
* @example
* import * as E from 'fp-ts/Either'
* import { pipe } from 'fp-ts/function'
* import * as TE from 'fp-ts/TaskEither'
*
* async function test() {
* assert.deepStrictEqual(
* await pipe(
* TE.right(1),
* TE.alt(() => TE.right(2))
* )(),
* E.right(1)
* )
* assert.deepStrictEqual(
* await pipe(
* TE.left('a'),
* TE.alt(() => TE.right(2))
* )(),
* E.right(2)
* )
* assert.deepStrictEqual(
* await pipe(
* TE.left('a'),
* TE.alt(() => TE.left('b'))
* )(),
* E.left('b')
* )
* }
*
* test()
*
* @category error handling
* @since 2.0.0
*/
export const alt: <E, A>(that: LazyArg<TaskEither<E, A>>) => (fa: TaskEither<E, A>) => TaskEither<E, A> =
/*#__PURE__*/ ET.alt(T.Monad)
/**
* Less strict version of [`alt`](#alt).
*
* The `W` suffix (short for **W**idening) means that the error and the return types will be merged.
*
* @category error handling
* @since 2.9.0
*/
export const altW: <E2, B>(
that: LazyArg<TaskEither<E2, B>>
) => <E1, A>(fa: TaskEither<E1, A>) => TaskEither<E2, A | B> = alt as any
/**
* @category constructors
* @since 2.0.0
*/
export const of: <E = never, A = never>(a: A) => TaskEither<E, A> = right
/**
* @since 2.7.0
*/
export const throwError: MonadThrow2<URI>['throwError'] = left
/**
* @category type lambdas
* @since 2.0.0
*/
export const URI = 'TaskEither'
/**
* @category type lambdas
* @since 2.0.0
*/
export type URI = typeof URI
declare module './HKT' {
interface URItoKind2<E, A> {
readonly [URI]: TaskEither<E, A>
}
}
/**
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* @example
* import * as E from 'fp-ts/Either'
* import { pipe } from 'fp-ts/function'
* import * as RA from 'fp-ts/ReadonlyArray'
* import * as S from 'fp-ts/Semigroup'
* import * as string from 'fp-ts/string'
* import * as T from 'fp-ts/Task'
* import * as TE from 'fp-ts/TaskEither'
*
* interface User {
* readonly id: string
* readonly name: string
* }
*
* const remoteDatabase: ReadonlyArray<User> = [
* { id: 'id1', name: 'John' },
* { id: 'id2', name: 'Mary' },
* { id: 'id3', name: 'Joey' }
* ]
*
* const fetchUser = (id: string): TE.TaskEither<string, User> =>
* pipe(
* remoteDatabase,
* RA.findFirst((user) => user.id === id),
* TE.fromOption(() => `${id} not found`)
* )
*
* async function test() {
* assert.deepStrictEqual(
* await pipe(['id4', 'id5'], RA.traverse(TE.ApplicativePar)(fetchUser))(),
* E.left('id4 not found') // <= first error
* )
*
* const Applicative = TE.getApplicativeTaskValidation(
* T.ApplyPar,
* pipe(string.Semigroup, S.intercalate(', '))
* )
*
* assert.deepStrictEqual(
* await pipe(['id4', 'id5'], RA.traverse(Applicative)(fetchUser))(),
* E.left('id4 not found, id5 not found') // <= all errors
* )
* }
*
* test()
*
* @category error handling
* @since 2.7.0
*/
export function getApplicativeTaskValidation<E>(A: Apply1<T.URI>, S: Semigroup<E>): Applicative2C<URI, E> {
const ap = ap_(A, E.getApplicativeValidation(S))
return {
URI,
_E: undefined as any,
map: _map,
ap: (fab, fa) => pipe(fab, ap(fa)),
of
}
}
/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
*
* @category error handling
* @since 2.7.0
*/
export function getAltTaskValidation<E>(S: Semigroup<E>): Alt2C<URI, E> {
const alt = ET.altValidation(T.Monad, S)
return {
URI,
_E: undefined as any,
map: _map,
alt: (fa, that) => pipe(fa, alt(that))
}
}
/**
* @category filtering
* @since 2.10.0
*/
export const getCompactable = <E>(M: Monoid<E>): Compactable2C<URI, E> => {
const C = E.getCompactable(M)
return {
URI,
_E: undefined as any,
compact: compact_(T.Functor, C),
separate: separate_(T.Functor, C, E.Functor)
}
}
/**
* @category filtering
* @since 2.1.0
*/
export function getFilterable<E>(M: Monoid<E>): Filterable2C<URI, E> {
const F = E.getFilterable(M)
const C = getCompactable(M)
const filter = filter_(T.Functor, F)
const filterMap = filterMap_(T.Functor, F)
const partition = partition_(T.Functor, F)
const partitionMap = partitionMap_(T.Functor, F)
return {
URI,
_E: undefined as any,
map: _map,
compact: C.compact,
separate: C.separate,
filter: <A>(fa: TaskEither<E, A>, predicate: Predicate<A>) => pipe(fa, filter(predicate)),
filterMap: (fa, f) => pipe(fa, filterMap(f)),
partition: <A>(fa: TaskEither<E, A>, predicate: Predicate<A>) => pipe(fa, partition(predicate)),
partitionMap: (fa, f) => pipe(fa, partitionMap(f))
}
}
/**
* @category instances
* @since 2.7.0
*/
export const Functor: Functor2<URI> = {
URI,
map: _map
}
/**
* Maps the `Right` value of this `TaskEither` to the specified constant value.
*
* @category mapping
* @since 2.16.0
*/
export const as: {
<A>(a: A): <E, _>(self: TaskEither<E, _>) => TaskEither<E, A>
<E, _, A>(self: TaskEither<E, _>, a: A): TaskEither<E, A>
} = dual(2, as_(Functor))
/**
* Maps the `Right` value of this `TaskEither` to the void constant value.
*
* @category mapping
* @since 2.16.0
*/
export const asUnit: <E, _>(self: TaskEither<E, _>) => TaskEither<E, void> = asUnit_(Functor)
/**
* @category mapping
* @since 2.10.0
*/
export const flap = /*#__PURE__*/ flap_(Functor)
/**
* @category instances
* @since 2.10.0
*/
export const Pointed: Pointed2<URI> = {
URI,
of
}
/**
* Runs computations in parallel.
*
* @category instances
* @since 2.10.0
*/
export const ApplyPar: Apply2<URI> = {
URI,
map: _map,
ap: _apPar
}
/**
* Combine two effectful actions, keeping only the result of the first.
*
* @since 2.0.0
*/
export const apFirst = /*#__PURE__*/ apFirst_(ApplyPar)
/**
* Less strict version of [`apFirst`](#apfirst).
*
* The `W` suffix (short for **W**idening) means that the error types will be merged.
*
* @since 2.12.0
*/
export const apFirstW: <E2, B>(
second: TaskEither<E2, B>
) => <E1, A>(first: TaskEither<E1, A>) => TaskEither<E1 | E2, A> = apFirst as any
/**
* Combine two effectful actions, keeping only the result of the second.
*
* @since 2.0.0
*/
export const apSecond = /*#__PURE__*/ apSecond_(ApplyPar)
/**
* Less strict version of [`apSecond`](#apsecond).
*
* The `W` suffix (short for **W**idening) means that the error types will be merged.
*
* @since 2.12.0
*/
export const apSecondW: <E2, B>(
second: TaskEither<E2, B>
) => <E1, A>(first: TaskEither<E1, A>) => TaskEither<E1 | E2, B> = apSecond as any
/**
* Runs computations in parallel.
*
* @category instances
* @since 2.7.0
*/
export const ApplicativePar: Applicative2<URI> = {
URI,
map: _map,
ap: _apPar,
of
}
/**
* Runs computations sequentially.
*
* @category instances
* @since 2.10.0
*/
export const ApplySeq: Apply2<URI> = {
URI,
map: _map,
ap: _apSeq
}
/**
* Runs computations sequentially.
*
* @category instances
* @since 2.7.0
*/
export const ApplicativeSeq: Applicative2<URI> = {
URI,
map: _map,
ap: _apSeq,
of
}
/**
* @category instances
* @since 2.10.0
*/
export const Chain: chainable.Chain2<URI> = {
URI,
map: _map,
ap: _apPar,
chain: flatMap
}
/**
* @category instances
* @since 2.10.0
*/
export const Monad: Monad2<URI> = {
URI,
map: _map,
ap: _apPar,
chain: flatMap,
of
}
/**
* @category instances
* @since 2.10.0
*/
export const MonadIO: MonadIO2<URI> = {
URI,
map: _map,
ap: _apPar,
chain: flatMap,
of,
fromIO
}
/**
* @category instances
* @since 2.10.0
*/
export const MonadTask: MonadTask2<URI> = {
URI,
map: _map,
ap: _apPar,
chain: flatMap,
of,
fromIO,
fromTask
}
/**
* @category instances
* @since 2.10.0
*/
export const MonadThrow: MonadThrow2<URI> = {
URI,
map: _map,
ap: _apPar,
chain: flatMap,
of,
throwError
}
/**
* @category instances
* @since 2.10.0
*/
export const FromEither: FromEither2<URI> = {
URI,
fromEither
}
/**
* @category instances
* @since 2.10.0
*/
export const FromIO: FromIO2<URI> = {
URI,
fromIO
}
/**
* @category instances
* @since 2.10.0
*/
export const FromTask: FromTask2<URI> = {
URI,
fromIO,
fromTask
}
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation and
* keeping only the result of the first.
*
* @category combinators
* @since 2.15.0
*/
export const tap: {
<E1, A, E2, _>(self: TaskEither<E1, A>, f: (a: A) => TaskEither<E2, _>): TaskEither<E1 | E2, A>
<A, E2, _>(f: (a: A) => TaskEither<E2, _>): <E1>(self: TaskEither<E1, A>) => TaskEither<E2 | E1, A>
} = /*#__PURE__*/ dual(2, chainable.tap(Chain))
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation and
* keeping only the result of the first.
*