-
-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathEffect.ts
More file actions
6049 lines (5673 loc) · 190 KB
/
Effect.ts
File metadata and controls
6049 lines (5673 loc) · 190 KB
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
/**
* @since 2.0.0
*/
import type * as RA from "./Array.js"
import type * as Cause from "./Cause.js"
import type * as Chunk from "./Chunk.js"
import type * as Clock from "./Clock.js"
import type { ConfigProvider } from "./ConfigProvider.js"
import type { Console } from "./Console.js"
import type * as Context from "./Context.js"
import type * as Deferred from "./Deferred.js"
import type * as Duration from "./Duration.js"
import type * as Either from "./Either.js"
import type { Equivalence } from "./Equivalence.js"
import type { ExecutionStrategy } from "./ExecutionStrategy.js"
import type * as Exit from "./Exit.js"
import type * as Fiber from "./Fiber.js"
import type * as FiberId from "./FiberId.js"
import type * as FiberRef from "./FiberRef.js"
import type * as FiberRefs from "./FiberRefs.js"
import type * as FiberRefsPatch from "./FiberRefsPatch.js"
import type * as FiberStatus from "./FiberStatus.js"
import type { LazyArg } from "./Function.js"
import { dual } from "./Function.js"
import type * as HashMap from "./HashMap.js"
import type * as HashSet from "./HashSet.js"
import type { TypeLambda } from "./HKT.js"
import * as _console from "./internal/console.js"
import { TagProto } from "./internal/context.js"
import * as effect from "./internal/core-effect.js"
import * as core from "./internal/core.js"
import * as defaultServices from "./internal/defaultServices.js"
import * as circular from "./internal/effect/circular.js"
import * as fiberRuntime from "./internal/fiberRuntime.js"
import * as layer from "./internal/layer.js"
import * as query from "./internal/query.js"
import * as _runtime from "./internal/runtime.js"
import * as _schedule from "./internal/schedule.js"
import type * as Layer from "./Layer.js"
import type { LogLevel } from "./LogLevel.js"
import type * as Metric from "./Metric.js"
import type * as MetricLabel from "./MetricLabel.js"
import type * as Option from "./Option.js"
import type { Pipeable } from "./Pipeable.js"
import type { Predicate, Refinement } from "./Predicate.js"
import type * as Random from "./Random.js"
import type * as Ref from "./Ref.js"
import * as Request from "./Request.js"
import type { RequestBlock } from "./RequestBlock.js"
import type { RequestResolver } from "./RequestResolver.js"
import type * as Runtime from "./Runtime.js"
import type * as RuntimeFlags from "./RuntimeFlags.js"
import type * as RuntimeFlagsPatch from "./RuntimeFlagsPatch.js"
import type * as Schedule from "./Schedule.js"
import * as Scheduler from "./Scheduler.js"
import type * as Scope from "./Scope.js"
import type * as Supervisor from "./Supervisor.js"
import type * as Tracer from "./Tracer.js"
import type { Concurrency, Covariant, NoInfer, NotFunction } from "./Types.js"
import type * as Unify from "./Unify.js"
import type { YieldWrap } from "./Utils.js"
// -------------------------------------------------------------------------------------
// models
// -------------------------------------------------------------------------------------
/**
* @since 2.0.0
* @category symbols
*/
export const EffectTypeId: unique symbol = core.EffectTypeId
/**
* @since 2.0.0
* @category symbols
*/
export type EffectTypeId = typeof EffectTypeId
/**
* The `Effect` interface defines a value that lazily describes a workflow or job.
* The workflow requires some context `R`, and may fail with an error of type `E`,
* or succeed with a value of type `A`.
*
* `Effect` values model resourceful interaction with the outside world, including
* synchronous, asynchronous, concurrent, and parallel interaction. They use a
* fiber-based concurrency model, with built-in support for scheduling, fine-grained
* interruption, structured concurrency, and high scalability.
*
* To run an `Effect` value, you need a `Runtime`, which is a type that is capable
* of executing `Effect` values.
*
* @since 2.0.0
* @category models
*/
export interface Effect<out A, out E = never, out R = never> extends Effect.Variance<A, E, R>, Pipeable {
readonly [Unify.typeSymbol]?: unknown
readonly [Unify.unifySymbol]?: EffectUnify<this>
readonly [Unify.ignoreSymbol]?: EffectUnifyIgnore
[Symbol.iterator](): EffectGenerator<Effect<A, E, R>>
}
/**
* @since 3.0.0
* @category models
*/
export interface EffectGenerator<T extends Effect<any, any, any>> {
next(...args: ReadonlyArray<any>): IteratorResult<YieldWrap<T>, Effect.Success<T>>
}
/**
* @since 2.0.0
* @category models
*/
export interface EffectUnify<A extends { [Unify.typeSymbol]?: any }>
extends Either.EitherUnify<A>, Option.OptionUnify<A>, Context.TagUnify<A>
{
Effect?: () => A[Unify.typeSymbol] extends Effect<infer A0, infer E0, infer R0> | infer _ ? Effect<A0, E0, R0> : never
}
/**
* @category models
* @since 2.0.0
*/
export interface EffectUnifyIgnore {
Tag?: true
Option?: true
Either?: true
}
/**
* @category type lambdas
* @since 2.0.0
*/
export interface EffectTypeLambda extends TypeLambda {
readonly type: Effect<this["Target"], this["Out1"], this["Out2"]>
}
/**
* @since 2.0.0
* @category models
*/
export interface Blocked<out A, out E> extends Effect<A, E> {
readonly _op: "Blocked"
readonly effect_instruction_i0: RequestBlock
readonly effect_instruction_i1: Effect<A, E>
}
/**
* @since 2.0.0
* @category models
*/
declare module "./Context.js" {
interface Tag<Id, Value> extends Effect<Value, never, Id> {
[Symbol.iterator](): EffectGenerator<Tag<Id, Value>>
}
interface TagUnifyIgnore {
Effect?: true
Either?: true
Option?: true
}
}
/**
* @since 2.0.0
* @category models
*/
declare module "./Either.js" {
interface Left<L, R> extends Effect<R, L> {
readonly _tag: "Left"
[Symbol.iterator](): EffectGenerator<Left<L, R>>
}
interface Right<L, R> extends Effect<R, L> {
readonly _tag: "Right"
[Symbol.iterator](): EffectGenerator<Right<L, R>>
}
interface EitherUnifyIgnore {
Effect?: true
Tag?: true
Option?: true
}
}
/**
* @since 2.0.0
* @category models
*/
declare module "./Option.js" {
interface None<A> extends Effect<A, Cause.NoSuchElementException> {
readonly _tag: "None"
[Symbol.iterator](): EffectGenerator<None<A>>
}
interface Some<A> extends Effect<A, Cause.NoSuchElementException> {
readonly _tag: "Some"
[Symbol.iterator](): EffectGenerator<Some<A>>
}
interface OptionUnifyIgnore {
Effect?: true
Tag?: true
Either?: true
}
}
/**
* @since 2.0.0
*/
export declare namespace Effect {
/**
* @since 2.0.0
* @category models
*/
export interface Variance<out A, out E, out R> {
readonly [EffectTypeId]: VarianceStruct<A, E, R>
}
/**
* @since 2.0.0
* @category models
*/
export interface VarianceStruct<out A, out E, out R> {
readonly _V: string
readonly _A: Covariant<A>
readonly _E: Covariant<E>
readonly _R: Covariant<R>
}
/**
* @since 2.0.0
* @category type-level
*/
export type Context<T extends Effect<any, any, any>> = [T] extends [Effect<infer _A, infer _E, infer _R>] ? _R : never
/**
* @since 2.0.0
* @category type-level
*/
export type Error<T extends Effect<any, any, any>> = [T] extends [Effect<infer _A, infer _E, infer _R>] ? _E : never
/**
* @since 2.0.0
* @category type-level
*/
export type Success<T extends Effect<any, any, any>> = [T] extends [Effect<infer _A, infer _E, infer _R>] ? _A : never
}
// -------------------------------------------------------------------------------------
// refinements
// -------------------------------------------------------------------------------------
/**
* This function returns `true` if the specified value is an `Effect` value,
* `false` otherwise.
*
* This function can be useful for checking the type of a value before
* attempting to operate on it as an `Effect` value. For example, you could
* use `isEffect` to check the type of a value before using it as an
* argument to a function that expects an `Effect` value.
*
* @param u - The value to check for being an `Effect` value.
*
* @returns `true` if the specified value is an `Effect` value, `false`
* otherwise.
*
* @since 2.0.0
* @category refinements
*/
export const isEffect: (u: unknown) => u is Effect<unknown, unknown, unknown> = core.isEffect
// -------------------------------------------------------------------------------------
// caching
// -------------------------------------------------------------------------------------
/**
* Returns an effect that caches its result for a specified duration, known as
* the `timeToLive`. When the cache expires after the duration, the effect will be
* recomputed upon next evaluation.
*
* @example
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* const cached = yield* Effect.cachedWithTTL(expensiveTask, "150 millis")
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* Effect.sleep("100 millis")
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // expensive task...
* // result 1
* // result 1
* // expensive task...
* // result 2
*
* @since 2.0.0
* @category caching
*/
export const cachedWithTTL: {
(timeToLive: Duration.DurationInput): <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E>, never, R>
<A, E, R>(self: Effect<A, E, R>, timeToLive: Duration.DurationInput): Effect<Effect<A, E>, never, R>
} = circular.cached
/**
* Similar to {@link cachedWithTTL}, this function caches an effect's result for a
* specified duration. It also includes an additional effect for manually
* invalidating the cached value before it naturally expires.
*
* @example
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* const [cached, invalidate] = yield* Effect.cachedInvalidateWithTTL(
* expensiveTask,
* "1 hour"
* )
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* invalidate
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // expensive task...
* // result 1
* // result 1
* // expensive task...
* // result 2
*
* @since 2.0.0
* @category caching
*/
export const cachedInvalidateWithTTL: {
(timeToLive: Duration.DurationInput): <A, E, R>(
self: Effect<A, E, R>
) => Effect<[Effect<A, E>, Effect<void>], never, R>
<A, E, R>(
self: Effect<A, E, R>,
timeToLive: Duration.DurationInput
): Effect<[Effect<A, E>, Effect<void>], never, R>
} = circular.cachedInvalidateWithTTL
/**
* Returns an effect that computes a result lazily and caches it. Subsequent
* evaluations of this effect will return the cached result without re-executing
* the logic.
*
* @example
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* console.log("non-cached version:")
* yield* expensiveTask.pipe(Effect.andThen(Console.log))
* yield* expensiveTask.pipe(Effect.andThen(Console.log))
* console.log("cached version:")
* const cached = yield* Effect.cached(expensiveTask)
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // non-cached version:
* // expensive task...
* // result 1
* // expensive task...
* // result 2
* // cached version:
* // expensive task...
* // result 3
* // result 3
*
* @since 2.0.0
* @category caching
*/
export const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>> = effect.memoize
/**
* Returns a memoized version of a function with effects. Memoization ensures
* that results are stored and reused for the same inputs, reducing the need to
* recompute them.
*
* @example
* import { Effect, Random } from "effect"
*
* const program = Effect.gen(function* () {
* const randomNumber = (n: number) => Random.nextIntBetween(1, n)
* console.log("non-memoized version:")
* console.log(yield* randomNumber(10))
* console.log(yield* randomNumber(10))
*
* console.log("memoized version:")
* const memoized = yield* Effect.cachedFunction(randomNumber)
* console.log(yield* memoized(10))
* console.log(yield* memoized(10))
* })
*
* Effect.runFork(program)
* // Example Output:
* // non-memoized version:
* // 2
* // 8
* // memoized version:
* // 5
* // 5
*
* @since 2.0.0
* @category caching
*/
export const cachedFunction: <A, B, E, R>(
f: (a: A) => Effect<B, E, R>,
eq?: Equivalence<A>
) => Effect<(a: A) => Effect<B, E, R>> = circular.cachedFunction
/**
* Returns an effect that executes only once, regardless of how many times it's
* called.
*
* @example
* import { Effect, Console } from "effect"
*
* const program = Effect.gen(function* () {
* const task1 = Console.log("task1")
* yield* Effect.repeatN(task1, 2)
* const task2 = yield* Effect.once(Console.log("task2"))
* yield* Effect.repeatN(task2, 2)
* })
*
* Effect.runFork(program)
* // Output:
* // task1
* // task1
* // task1
* // task2
*
* @since 2.0.0
* @category caching
*/
export const once: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<void, E, R>> = effect.once
// -------------------------------------------------------------------------------------
// collecting & elements
// -------------------------------------------------------------------------------------
/**
* Runs all the provided effects in sequence respecting the structure provided in input.
*
* Supports multiple arguments, a single argument tuple / array or record / struct.
*
* @since 2.0.0
* @category collecting & elements
*/
export const all: <
const Arg extends Iterable<Effect<any, any, any>> | Record<string, Effect<any, any, any>>,
O extends {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: boolean | undefined
readonly mode?: "default" | "validate" | "either" | undefined
}
>(arg: Arg, options?: O) => All.Return<Arg, O> = fiberRuntime.all
/**
* Data-last variant of `Effect.all`.
*
* Runs all the provided effects in sequence respecting the structure provided in input.
*
* Supports multiple arguments, a single argument tuple / array or record / struct.
*
* @since 2.0.0
* @category collecting & elements
*/
export const allWith: <
O extends {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: boolean | undefined
readonly mode?: "default" | "validate" | "either" | undefined
}
>(
options?: O
) => <const Arg extends Iterable<Effect<any, any, any>> | Record<string, Effect<any, any, any>>>(
arg: Arg
) => All.Return<Arg, O> = fiberRuntime.allWith
/**
* @since 2.0.0
*/
export declare namespace All {
/**
* @since 2.0.0
*/
export type EffectAny = Effect<any, any, any>
/**
* @since 2.0.0
*/
export type ReturnIterable<T extends Iterable<EffectAny>, Discard extends boolean, Mode> = [T] extends
[Iterable<Effect.Variance<infer R0, infer L0, infer R>>] ? Effect<
Discard extends true ? void : Mode extends "either" ? Array<Either.Either<R0, L0>> : Array<R0>,
Mode extends "either" ? never
: Mode extends "validate" ? Array<Option.Option<L0>>
: L0,
R
>
: never
/**
* @since 2.0.0
*/
export type ReturnTuple<T extends ReadonlyArray<unknown>, Discard extends boolean, Mode> = Effect<
Discard extends true ? void
: T[number] extends never ? []
: Mode extends "either" ? {
-readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ?
Either.Either<_A, _E>
: never
}
: { -readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ? _A : never },
Mode extends "either" ? never
: T[number] extends never ? never
: Mode extends "validate" ? {
-readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ? Option.Option<_E>
: never
}
: [T[number]] extends [{ [EffectTypeId]: { _E: (_: never) => infer E } }] ? E
: never,
T[number] extends never ? never
: [T[number]] extends [{ [EffectTypeId]: { _R: (_: never) => infer R } }] ? R
: never
> extends infer X ? X : never
/**
* @since 2.0.0
*/
export type ReturnObject<T, Discard extends boolean, Mode> = [T] extends [{ [K: string]: EffectAny }] ? Effect<
Discard extends true ? void
: Mode extends "either" ? {
-readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ?
Either.Either<_A, _E>
: never
}
: { -readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ? _A : never },
Mode extends "either" ? never
: keyof T extends never ? never
: Mode extends "validate" ? {
-readonly [K in keyof T]: [T[K]] extends [Effect.Variance<infer _A, infer _E, infer _R>] ? Option.Option<_E>
: never
}
: [T[keyof T]] extends [{ [EffectTypeId]: { _E: (_: never) => infer E } }] ? E
: never,
keyof T extends never ? never
: [T[keyof T]] extends [{ [EffectTypeId]: { _R: (_: never) => infer R } }] ? R
: never
>
: never
/**
* @since 2.0.0
*/
export type IsDiscard<A> = [Extract<A, { readonly discard: true }>] extends [never] ? false : true
/**
* @since 2.0.0
*/
export type ExtractMode<A> = [A] extends [{ mode: infer M }] ? M : "default"
/**
* @since 2.0.0
*/
export type Return<
Arg extends Iterable<EffectAny> | Record<string, EffectAny>,
O extends {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: boolean | undefined
readonly mode?: "default" | "validate" | "either" | undefined
}
> = [Arg] extends [ReadonlyArray<EffectAny>] ? ReturnTuple<Arg, IsDiscard<O>, ExtractMode<O>>
: [Arg] extends [Iterable<EffectAny>] ? ReturnIterable<Arg, IsDiscard<O>, ExtractMode<O>>
: [Arg] extends [Record<string, EffectAny>] ? ReturnObject<Arg, IsDiscard<O>, ExtractMode<O>>
: never
}
/**
* Evaluate and run each effect in the structure and collect the results,
* discarding results from failed effects.
*
* @since 2.0.0
* @category collecting & elements
*/
export const allSuccesses: <X extends Effect<any, any, any>>(
elements: Iterable<X>,
options?:
| {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
}
| undefined
) => Effect<Array<Effect.Success<X>>, never, Effect.Context<X>> = fiberRuntime.allSuccesses
/**
* Drops all elements until the effectful predicate returns true.
*
* @since 2.0.0
* @category collecting & elements
*/
export const dropUntil: {
<A, E, R>(
predicate: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>
): (elements: Iterable<A>) => Effect<Array<A>, E, R>
<A, E, R>(elements: Iterable<A>, predicate: (a: A, i: number) => Effect<boolean, E, R>): Effect<Array<A>, E, R>
} = effect.dropUntil
/**
* Drops all elements so long as the predicate returns true.
*
* @since 2.0.0
* @category collecting & elements
*/
export const dropWhile: {
<A, E, R>(
predicate: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>
): (elements: Iterable<A>) => Effect<Array<A>, E, R>
<A, E, R>(elements: Iterable<A>, predicate: (a: A, i: number) => Effect<boolean, E, R>): Effect<Array<A>, E, R>
} = effect.dropWhile
/**
* Determines whether all elements of the `Collection<A>` satisfies the effectual
* predicate `f`.
*
* @since 2.0.0
* @category collecting & elements
*/
export const every: {
<A, E, R>(f: (a: A, i: number) => Effect<boolean, E, R>): (elements: Iterable<A>) => Effect<boolean, E, R>
<A, E, R>(elements: Iterable<A>, f: (a: A, i: number) => Effect<boolean, E, R>): Effect<boolean, E, R>
} = effect.every
/**
* Determines whether any element of the `Iterable<A>` satisfies the effectual
* predicate `f`.
*
* @since 2.0.0
* @category collecting & elements
*/
export const exists: {
<A, E, R>(
f: (a: A, i: number) => Effect<boolean, E, R>,
options?:
| {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
}
| undefined
): (elements: Iterable<A>) => Effect<boolean, E, R>
<A, E, R>(
elements: Iterable<A>,
f: (a: A, i: number) => Effect<boolean, E, R>,
options?:
| {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
}
| undefined
): Effect<boolean, E, R>
} = fiberRuntime.exists
/**
* Filters the collection using the specified effectful predicate.
*
* @since 2.0.0
* @category collecting & elements
*/
export const filter: {
<A, E, R>(
f: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly negate?: boolean | undefined
} | undefined
): (elements: Iterable<A>) => Effect<Array<A>, E, R>
<A, E, R>(
elements: Iterable<A>,
f: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly negate?: boolean | undefined
} | undefined
): Effect<Array<A>, E, R>
} = fiberRuntime.filter
/**
* Performs a filter and map in a single step.
*
* @since 2.0.0
* @category collecting & elements
*/
export const filterMap: {
<Eff extends Effect<any, any, any>, B>(
pf: (a: Effect.Success<Eff>) => Option.Option<B>
): (elements: Iterable<Eff>) => Effect<Array<B>, Effect.Error<Eff>, Effect.Context<Eff>>
<Eff extends Effect<any, any, any>, B>(
elements: Iterable<Eff>,
pf: (a: Effect.Success<Eff>) => Option.Option<B>
): Effect<Array<B>, Effect.Error<Eff>, Effect.Context<Eff>>
} = effect.filterMap
/**
* Returns the first element that satisfies the effectful predicate.
*
* @since 2.0.0
* @category collecting & elements
*/
export const findFirst: {
<A, E, R>(
f: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>
): (elements: Iterable<A>) => Effect<Option.Option<A>, E, R>
<A, E, R>(
elements: Iterable<A>,
f: (a: NoInfer<A>, i: number) => Effect<boolean, E, R>
): Effect<Option.Option<A>, E, R>
} = effect.findFirst
/**
* This function takes an iterable of `Effect` values and returns a new
* `Effect` value that represents the first `Effect` value in the iterable
* that succeeds. If all of the `Effect` values in the iterable fail, then
* the resulting `Effect` value will fail as well.
*
* This function is sequential, meaning that the `Effect` values in the
* iterable will be executed in sequence, and the first one that succeeds
* will determine the outcome of the resulting `Effect` value.
*
* @param effects - The iterable of `Effect` values to evaluate.
*
* @returns A new `Effect` value that represents the first successful
* `Effect` value in the iterable, or a failed `Effect` value if all of the
* `Effect` values in the iterable fail.
*
* @since 2.0.0
* @category collecting & elements
*/
export const firstSuccessOf: <Eff extends Effect<any, any, any>>(
effects: Iterable<Eff>
) => Effect<Effect.Success<Eff>, Effect.Error<Eff>, Effect.Context<Eff>> = effect.firstSuccessOf
/**
* @since 2.0.0
* @category collecting & elements
*/
export const forEach: {
<B, E, R, S extends Iterable<any>>(
f: (a: RA.ReadonlyArray.Infer<S>, i: number) => Effect<B, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: false | undefined
} | undefined
): (
self: S
) => Effect<RA.ReadonlyArray.With<S, B>, E, R>
<A, B, E, R>(
f: (a: A, i: number) => Effect<B, E, R>,
options: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard: true
}
): (self: Iterable<A>) => Effect<void, E, R>
<B, E, R, S extends Iterable<any>>(
self: S,
f: (a: RA.ReadonlyArray.Infer<S>, i: number) => Effect<B, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: false | undefined
} | undefined
): Effect<RA.ReadonlyArray.With<S, B>, E, R>
<A, B, E, R>(
self: Iterable<A>,
f: (a: A, i: number) => Effect<B, E, R>,
options: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard: true
}
): Effect<void, E, R>
} = fiberRuntime.forEach as any
/**
* Returns a successful effect with the head of the collection if the collection
* is non-empty, or fails with the error `None` if the collection is empty.
*
* @since 2.0.0
* @category collecting & elements
*/
export const head: <A, E, R>(self: Effect<Iterable<A>, E, R>) => Effect<A, Cause.NoSuchElementException | E, R> =
effect.head
/**
* Merges an `Iterable<Effect<A, E, R>>` to a single effect, working
* sequentially.
*
* @since 2.0.0
* @category collecting & elements
*/
export const mergeAll: {
<Z, Eff extends Effect<any, any, any>>(
zero: Z,
f: (z: Z, a: Effect.Success<Eff>, i: number) => Z,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): (elements: Iterable<Eff>) => Effect<Z, Effect.Error<Eff>, Effect.Context<Eff>>
<Eff extends Effect<any, any, any>, Z>(
elements: Iterable<Eff>,
zero: Z,
f: (z: Z, a: Effect.Success<Eff>, i: number) => Z,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): Effect<Z, Effect.Error<Eff>, Effect.Context<Eff>>
} = fiberRuntime.mergeAll
/**
* Feeds elements of type `A` to a function `f` that returns an effect.
* Collects all successes and failures in a tupled fashion.
*
* @since 2.0.0
* @category collecting & elements
*/
export const partition: {
<A, B, E, R>(
f: (a: A, i: number) => Effect<B, E, R>,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): (elements: Iterable<A>) => Effect<[excluded: Array<E>, satisfying: Array<B>], never, R>
<A, B, E, R>(
elements: Iterable<A>,
f: (a: A, i: number) => Effect<B, E, R>,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): Effect<[excluded: Array<E>, satisfying: Array<B>], never, R>
} = fiberRuntime.partition
/**
* Folds an `Iterable<A>` using an effectual function f, working sequentially
* from left to right.
*
* @since 2.0.0
* @category collecting & elements
*/
export const reduce: {
<Z, A, E, R>(zero: Z, f: (z: Z, a: A, i: number) => Effect<Z, E, R>): (elements: Iterable<A>) => Effect<Z, E, R>
<A, Z, E, R>(elements: Iterable<A>, zero: Z, f: (z: Z, a: A, i: number) => Effect<Z, E, R>): Effect<Z, E, R>
} = effect.reduce
/**
* Reduces an `Iterable<Effect<A, E, R>>` to a single effect.
*
* @since 2.0.0
* @category collecting & elements
*/
export const reduceEffect: {
<Z, E, R, Eff extends Effect<any, any, any>>(
zero: Effect<Z, E, R>,
f: (acc: NoInfer<Z>, a: Effect.Success<Eff>, i: number) => Z,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): (elements: Iterable<Eff>) => Effect<Z, E | Effect.Error<Eff>, R | Effect.Context<Eff>>
<Eff extends Effect<any, any, any>, Z, E, R>(
elements: Iterable<Eff>,
zero: Effect<Z, E, R>,
f: (acc: NoInfer<Z>, a: Effect.Success<Eff>, i: number) => Z,
options?:
| { readonly concurrency?: Concurrency | undefined; readonly batching?: boolean | "inherit" | undefined }
| undefined
): Effect<Z, E | Effect.Error<Eff>, R | Effect.Context<Eff>>
} = fiberRuntime.reduceEffect
/**
* Folds an `Iterable<A>` using an effectual function f, working sequentially from left to right.
*
* @since 2.0.0
* @category collecting & elements
*/
export const reduceRight: {
<A, Z, R, E>(zero: Z, f: (a: A, z: Z, i: number) => Effect<Z, E, R>): (elements: Iterable<A>) => Effect<Z, E, R>
<A, Z, R, E>(elements: Iterable<A>, zero: Z, f: (a: A, z: Z, i: number) => Effect<Z, E, R>): Effect<Z, E, R>
} = effect.reduceRight
/**
* Folds over the elements in this chunk from the left, stopping the fold early
* when the predicate is not satisfied.
*
* @since 2.0.0
* @category collecting & elements
*/
export const reduceWhile: {
<Z, A, E, R>(
zero: Z,
options: { readonly while: Predicate<Z>; readonly body: (s: Z, a: A, i: number) => Effect<Z, E, R> }
): (elements: Iterable<A>) => Effect<Z, E, R>
<A, Z, E, R>(
elements: Iterable<A>,
zero: Z,
options: { readonly while: Predicate<Z>; readonly body: (s: Z, a: A, i: number) => Effect<Z, E, R> }
): Effect<Z, E, R>
} = effect.reduceWhile
/**
* Replicates the given effect `n` times.
*
* @since 2.0.0
* @category collecting & elements
*/
export const replicate: {
(n: number): <A, E, R>(self: Effect<A, E, R>) => Array<Effect<A, E, R>>
<A, E, R>(self: Effect<A, E, R>, n: number): Array<Effect<A, E, R>>
} = fiberRuntime.replicate
/**
* Performs this effect the specified number of times and collects the
* results.
*
* @since 2.0.0
* @category collecting & elements
*/
export const replicateEffect: {
(
n: number,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: false | undefined
}
): <A, E, R>(self: Effect<A, E, R>) => Effect<Array<A>, E, R>
(
n: number,
options: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard: true
}
): <A, E, R>(self: Effect<A, E, R>) => Effect<void, E, R>
<A, E, R>(
self: Effect<A, E, R>,
n: number,
options?: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard?: false | undefined
}
): Effect<Array<A>, E, R>
<A, E, R>(
self: Effect<A, E, R>,
n: number,
options: {
readonly concurrency?: Concurrency | undefined
readonly batching?: boolean | "inherit" | undefined
readonly discard: true
}
): Effect<void, E, R>
} = fiberRuntime.replicateEffect
/**
* Takes elements until the effectual predicate returns true.
*