Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Function.ts

1,415 lines29.7 KB

1/**
2 * Provides small helpers for defining and reusing TypeScript functions.
3 *
4 * The main helpers are `pipe` and `flow` for left-to-right composition and
5 * `dual` for APIs that support both direct and pipe-friendly call styles. The
6 * module also contains small identity, constant, tuple, type-level, and
7 * memoization helpers used across the library.
8 *
9 * @since 2.0.0
10 */
11import type { TypeLambda } from "./HKT.ts"
12import { pipeArguments } from "./Pipeable.ts"
13​
14/**
15 * Type lambda for function types, used for higher-kinded type operations.
16 *
17 * **When to use**
18 *
19 * Use when defining higher-kinded abstractions that must accept function types
20 * as one of their type-lambda inputs.
21 *
22 * **Example** (Creating a function type with a type lambda)
23 *
24 * ```ts
25 * import type { Function, HKT } from "effect"
26 *
27 * // Create a function type using the type lambda
28 * type StringToNumber = HKT.Kind<Function.FunctionTypeLambda, string, never, never, number>
29 * // Equivalent to: (a: string) => number
30 * ```
31 *
32 * @category type lambdas
33 * @since 2.0.0
34 */
35export interface FunctionTypeLambda extends TypeLambda {
36 readonly type: (a: this["In"]) => this["Target"]
38​
39/**
40 * Creates a function that can be called in data-first style or data-last
41 * (`pipe`-friendly) style.
42 *
43 * **When to use**
44 *
45 * Use to expose one implementation through both direct and `pipe`-friendly
46 * call styles.
47 *
48 * **Details**
49 *
50 * Pass either the arity of the uncurried function or a predicate that decides
51 * whether the current call is data-first. Arity is the common case. Use a
52 * predicate when optional arguments make arity ambiguous.
53 *
54 * **Example** (Selecting data-first or data-last style by arity)
55 *
56 * ```ts
57 * import { Function, pipe } from "effect"
58 *
59 * const sum = Function.dual<
60 * (that: number) => (self: number) => number,
61 * (self: number, that: number) => number
62 * >(2, (self, that) => self + that)
63 *
64 * console.log(sum(2, 3)) // 5
65 * console.log(pipe(2, sum(3))) // 5
66 * ```
67 *
68 * **Example** (Defining overloads with call signatures)
69 *
70 * ```ts
71 * import { Function, pipe } from "effect"
72 *
73 * const sum: {
74 * (that: number): (self: number) => number
75 * (self: number, that: number): number
76 * } = Function.dual(2, (self: number, that: number): number => self + that)
77 *
78 * console.log(sum(2, 3)) // 5
79 * console.log(pipe(2, sum(3))) // 5
80 * ```
81 *
82 * **Example** (Selecting data-first or data-last style with a predicate)
83 *
84 * ```ts
85 * import { Function, pipe } from "effect"
86 *
87 * const sum = Function.dual<
88 * (that: number) => (self: number) => number,
89 * (self: number, that: number) => number
90 * >(
91 * (args) => args.length === 2,
92 * (self, that) => self + that
93 * )
94 *
95 * console.log(sum(2, 3)) // 5
96 * console.log(pipe(2, sum(3))) // 5
97 * ```
98 *
99 * @category combinators
100 * @since 2.0.0
101 */
102export const dual: {
103 <DataLast extends (...args: Array<any>) => any, DataFirst extends (...args: Array<any>) => any>(
104 arity: Parameters<DataFirst>["length"],
105 body: DataFirst
106 ): DataLast & DataFirst
107 <DataLast extends (...args: Array<any>) => any, DataFirst extends (...args: Array<any>) => any>(
108 isDataFirst: (args: IArguments) => boolean,
109 body: DataFirst
110 ): DataLast & DataFirst
111} = function(arity, body) {
112 if (typeof arity === "function") {
113 return function(this: any) {
114 return arity(arguments)
115 ? body.apply(this, arguments as any)
116 : ((self: any) => body(self, ...arguments)) as any
117 }
118 }
119​
120 switch (arity) {
121 case 0:
122 case 1:
123 throw new RangeError(`Invalid arity ${arity}`)
124​
125 case 2:
126 return function(a, b) {
127 if (arguments.length >= 2) {
128 return body(a, b)
129 }
130 return function(self: any) {
131 return body(self, a)
132 }
133 }
134​
135 case 3:
136 return function(a, b, c) {
137 if (arguments.length >= 3) {
138 return body(a, b, c)
139 }
140 return function(self: any) {
141 return body(self, a, b)
142 }
143 }
144​
145 default:
146 return function() {
147 if (arguments.length >= arity) {
148 // @ts-expect-error
149 return body.apply(this, arguments)
150 }
151 const args = arguments
152 return function(self: any) {
153 return body(self, ...args)
154 }
155 }
156 }
158/**
159 * Applies a function to a given value.
160 *
161 * **When to use**
162 *
163 * Use to pass a fixed value into a unary function, especially when the function
164 * is the value flowing through `pipe`.
165 *
166 * **Details**
167 *
168 * `apply(a)(f)` is equivalent to `f(a)`.
169 *
170 * **Example** (Applying an argument to a function)
171 *
172 * ```ts
173 * import { Function, pipe, String } from "effect"
174 * import * as assert from "node:assert"
175 *
176 * assert.deepStrictEqual(pipe(String.length, Function.apply("hello")), 5)
177 * ```
178 *
179 * @see {@link pipe} for building left-to-right pipelines
180 *
181 * @category combinators
182 * @since 2.0.0
183 */
184export const apply = <A>(a: A) => <B>(self: (a: A) => B): B => self(a)
185​
186/**
187 * A zero-argument function that produces a value when invoked.
188 *
189 * **When to use**
190 *
191 * Use to type a lazy value provider that should not run until called.
192 *
193 * **Example** (Creating a lazy argument)
194 *
195 * ```ts
196 * import { Function } from "effect"
197 *
198 * const constNull: Function.LazyArg<null> = Function.constant(null)
199 * ```
200 *
201 * @category models
202 * @since 2.0.0
203 */
204export type LazyArg<A> = () => A
205​
206/**
207 * Represents a function with multiple arguments.
208 *
209 * **When to use**
210 *
211 * Use to describe a function whose argument list is represented as a tuple
212 * type.
213 *
214 * **Example** (Typing a variadic function)
215 *
216 * ```ts
217 * import type { Function } from "effect"
218 * import * as assert from "node:assert"
219 *
220 * const sum: Function.FunctionN<[number, number], number> = (a, b) => a + b
221 * assert.deepStrictEqual(sum(2, 3), 5)
222 * ```
223 *
224 * @category models
225 * @since 2.0.0
226 */
227export type FunctionN<A extends ReadonlyArray<unknown>, B> = (...args: A) => B
228​
229/**
230 * Returns its input argument unchanged.
231 *
232 * **When to use**
233 *
234 * Use to return a value unchanged where a function is required.
235 *
236 * **Example** (Returning the same value)
237 *
238 * ```ts
239 * import { identity } from "effect"
240 * import * as assert from "node:assert"
241 *
242 * assert.deepStrictEqual(identity(5), 5)
243 * ```
244 *
245 * @category combinators
246 * @since 2.0.0
247 */
248export const identity = <A>(a: A): A => a
249​
250/**
251 * Ensures that the type of an expression matches some type,
252 * without changing the resulting type of that expression.
253 *
254 * **When to use**
255 *
256 * Use to check assignability while preserving the expression's precise inferred
257 * type.
258 *
259 * **Example** (Checking an expression against a type)
260 *
261 * ```ts
262 * import { Function } from "effect"
263 * import * as assert from "node:assert"
264 *
265 * const test1 = Function.satisfies<number>()(5 as const)
266 * // ^? const test: 5
267 * // @ts-expect-error
268 * const test2 = Function.satisfies<string>()(5)
269 * // ^? Argument of type 'number' is not assignable to parameter of type 'string'
270 *
271 * assert.deepStrictEqual(Function.satisfies<number>()(5), 5)
272 * ```
273 *
274 * @see {@link cast} for changing only the static TypeScript type
275 *
276 * @category utility types
277 * @since 2.0.0
278 */
279export const satisfies = <A>() => <B extends A>(b: B) => b
280​
281/**
282 * Returns the input value with a different static type.
283 *
284 * **When to use**
285 *
286 * Use when you need an explicit type-level cast and accept that the value is
287 * returned unchanged at runtime.
288 *
289 * **Gotchas**
290 *
291 * This is a type-level cast only; it performs no runtime validation or
292 * conversion.
293 *
294 * @see {@link satisfies} for checking assignability without changing the resulting type
295 *
296 * @category utility types
297 * @since 4.0.0
298 */
299export const cast: <A, B>(a: A) => B = identity as any
300​
301/**
302 * Creates a zero-argument function that always returns the provided value.
303 *
304 * **When to use**
305 *
306 * Use when you need a thunk or callback that returns the same value on every
307 * invocation.
308 *
309 * **Example** (Creating a constant thunk)
310 *
311 * ```ts
312 * import { Function } from "effect"
313 * import * as assert from "node:assert"
314 *
315 * const constNull = Function.constant(null)
316 *
317 * assert.deepStrictEqual(constNull(), null)
318 * assert.deepStrictEqual(constNull(), null)
319 * ```
320 *
321 * @category constructors
322 * @since 2.0.0
323 */
324export const constant = <A>(value: A): LazyArg<A> => () => value
325​
326/**
327 * Returns `true` when called.
328 *
329 * **When to use**
330 *
331 * Use when you need a thunk that returns `true` on every invocation.
332 *
333 * **Example** (Returning true from a thunk)
334 *
335 * ```ts
336 * import { Function } from "effect"
337 * import * as assert from "node:assert"
338 *
339 * assert.deepStrictEqual(Function.constTrue(), true)
340 * ```
341 *
342 * @category constants
343 * @since 2.0.0
344 */
345export const constTrue: LazyArg<boolean> = constant(true)
346​
347/**
348 * Returns `false` when called.
349 *
350 * **When to use**
351 *
352 * Use when you need a thunk that returns `false` on every invocation.
353 *
354 * **Example** (Returning false from a thunk)
355 *
356 * ```ts
357 * import { Function } from "effect"
358 * import * as assert from "node:assert"
359 *
360 * assert.deepStrictEqual(Function.constFalse(), false)
361 * ```
362 *
363 * @category constants
364 * @since 2.0.0
365 */
366export const constFalse: LazyArg<boolean> = constant(false)
367​
368/**
369 * Returns `null` when called.
370 *
371 * **When to use**
372 *
373 * Use when you need a thunk that returns `null` on every invocation.
374 *
375 * **Example** (Returning null from a thunk)
376 *
377 * ```ts
378 * import { Function } from "effect"
379 * import * as assert from "node:assert"
380 *
381 * assert.deepStrictEqual(Function.constNull(), null)
382 * ```
383 *
384 * @category constants
385 * @since 2.0.0
386 */
387export const constNull: LazyArg<null> = constant(null)
388​
389/**
390 * Returns `undefined` when called.
391 *
392 * **When to use**
393 *
394 * Use when you need a thunk that returns `undefined` on every invocation.
395 *
396 * **Example** (Returning undefined from a thunk)
397 *
398 * ```ts
399 * import { Function } from "effect"
400 * import * as assert from "node:assert"
401 *
402 * assert.deepStrictEqual(Function.constUndefined(), undefined)
403 * ```
404 *
405 * @category constants
406 * @since 2.0.0
407 */
408export const constUndefined: LazyArg<undefined> = constant(undefined)
409​
410/**
411 * Returns no meaningful value when called.
412 *
413 * **When to use**
414 *
415 * Use when you need a thunk that is called only for its effect and has no
416 * meaningful return value.
417 *
418 * **Example** (Returning void from a thunk)
419 *
420 * ```ts
421 * import { Function } from "effect"
422 * import * as assert from "node:assert"
423 *
424 * assert.deepStrictEqual(Function.constVoid(), undefined)
425 * ```
426 *
427 * @category constants
428 * @since 2.0.0
429 */
430export const constVoid: LazyArg<void> = constUndefined
431​
432/**
433 * Reverses the order of arguments for a curried function.
434 *
435 * **When to use**
436 *
437 * Use to adapt a curried function when its argument groups need to be supplied
438 * in the opposite order.
439 *
440 * **Example** (Flipping curried arguments)
441 *
442 * ```ts
443 * import { Function } from "effect"
444 * import * as assert from "node:assert"
445 *
446 * const f = (a: number) => (b: string) => a - b.length
447 *
448 * assert.deepStrictEqual(Function.flip(f)("aaa")(2), -1)
449 * ```
450 *
451 * @category combinators
452 * @since 2.0.0
453 */
454export const flip = <A extends Array<unknown>, B extends Array<unknown>, C>(
455 f: (...a: A) => (...b: B) => C
456): (...b: B) => (...a: A) => C =>
457(...b) =>
458(...a) => f(...a)(...b)
459​
460/**
461 * Composes two functions, `ab` and `bc` into a single function that takes in an argument `a` of type `A` and returns a result of type `C`.
462 * The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
463 *
464 * **When to use**
465 *
466 * Use to compose exactly two unary functions into a reusable unary function.
467 *
468 * **Example** (Composing two functions)
469 *
470 * ```ts
471 * import { Function } from "effect"
472 * import * as assert from "node:assert"
473 *
474 * const increment = (n: number) => n + 1
475 * const square = (n: number) => n * n
476 *
477 * assert.strictEqual(Function.compose(increment, square)(2), 9)
478 * ```
479 *
480 * @see {@link flow} for composing a left-to-right sequence of functions
481 * @see {@link pipe} for applying a value through a left-to-right sequence immediately
482 *
483 * @category combinators
484 * @since 2.0.0
485 */
486export const compose: {
487 <B, C>(bc: (b: B) => C): <A>(self: (a: A) => B) => (a: A) => C
488 <A, B, C>(self: (a: A) => B, bc: (b: B) => C): (a: A) => C
489} = dual(2, <A, B, C>(ab: (a: A) => B, bc: (b: B) => C): (a: A) => C => (a) => bc(ab(a)))
490​
491/**
492 * Marks an impossible branch by accepting a `never` value and returning any
493 * type.
494 *
495 * **When to use**
496 *
497 * Use when you need a return value in a branch that exhaustive checks prove
498 * cannot be reached.
499 *
500 * **Gotchas**
501 *
502 * Calling `absurd` throws, because a value of type `never` should be
503 * impossible at runtime.
504 *
505 * **Example** (Handling impossible values)
506 *
507 * ```ts
508 * import { absurd } from "effect"
509 *
510 * const handleNever = (value: never) => {
511 * return absurd(value) // This will throw an error if called
512 * }
513 * ```
514 *
515 * @category utility types
516 * @since 2.0.0
517 */
518export const absurd = <A>(_: never): A => {
519 throw new Error("Called `absurd` function which should be uncallable")
521​
522/**
523 * Creates a tupled version of this function: instead of `n` arguments, it accepts a single tuple argument.
524 *
525 * **When to use**
526 *
527 * Use to adapt a multi-argument function so it accepts one tuple argument.
528 *
529 * **Example** (Converting arguments to a tuple)
530 *
531 * ```ts
532 * import { Function } from "effect"
533 * import * as assert from "node:assert"
534 *
535 * const sumTupled = Function.tupled((x: number, y: number): number => x + y)
536 *
537 * assert.deepStrictEqual(sumTupled([1, 2]), 3)
538 * ```
539 *
540 * @see {@link untupled} for adapting a tuple-argument function back to multiple arguments
541 *
542 * @category combinators
543 * @since 2.0.0
544 */
545export const tupled = <A extends ReadonlyArray<unknown>, B>(f: (...a: A) => B): (a: A) => B => (a) => f(...a)
546​
547/**
548 * Converts a tupled function back to an uncurried function.
549 *
550 * **When to use**
551 *
552 * Use to adapt a tuple-argument function so it accepts multiple arguments.
553 *
554 * **Example** (Converting a tuple to arguments)
555 *
556 * ```ts
557 * import { Function } from "effect"
558 * import * as assert from "node:assert"
559 *
560 * const getFirst = Function.untupled(<A, B>(tuple: [A, B]): A => tuple[0])
561 *
562 * assert.deepStrictEqual(getFirst(1, 2), 1)
563 * ```
564 *
565 * @see {@link tupled} for adapting a multi-argument function to one tuple argument
566 *
567 * @category combinators
568 * @since 2.0.0
569 */
570export const untupled = <A extends ReadonlyArray<unknown>, B>(f: (a: A) => B): (...a: A) => B => (...a) => f(a)
571​
572/**
573 * Pipes the value of an expression through a left-to-right sequence of
574 * functions.
575 *
576 * **When to use**
577 *
578 * Use when you need to compose data-last functions into readable
579 * transformation pipelines instead of method-style chains.
580 *
581 * **Details**
582 *
583 * Takes an initial value, passes it to the first function, then passes each
584 * result to the next function in order. The final function result is returned.
585 *
586 * **Gotchas**
587 *
588 * Each function passed after the initial value must accept a single argument,
589 * because `pipe` calls each step with only the previous result.
590 *
591 * **Example** (Piping values through functions)
592 *
593 * In this example, `1` is passed to the first function, and each result becomes
594 * the input for the next function.
595 *
596 * ```ts
597 * import { pipe } from "effect"
598 *
599 * const result = pipe(
600 * 1,
601 * (n) => n + 1,
602 * (n) => n * 2,
603 * (n) => `result: ${n}`
604 * )
605 *
606 * console.log(result) // "result: 4"
607 * ```
608 *
609 * **Example** (Chaining methods before conversion)
610 *
611 * ```ts
612 * const numbers = [1, 2, 3, 4]
613 * const double = (n: number) => n * 2
614 * const greaterThanFour = (n: number) => n > 4
615 *
616 * const result = numbers.map(double).filter(greaterThanFour)
617 *
618 * console.log(result) // [6, 8]
619 * ```
620 *
621 * **Example** (Rewriting method chains with pipe)
622 *
623 * The same transformation can be written with data-last functions.
624 *
625 * ```ts
626 * import { Array, pipe } from "effect"
627 *
628 * const numbers = [1, 2, 3, 4]
629 * const double = (n: number) => n * 2
630 * const greaterThanFour = (n: number) => n > 4
631 *
632 * const result = pipe(
633 * numbers,
634 * Array.map(double),
635 * Array.filter(greaterThanFour)
636 * )
637 *
638 * console.log(result) // [6, 8]
639 * ```
640 *
641 * **Example** (Chaining arithmetic operations)
642 *
643 * ```ts
644 * import { pipe } from "effect"
645 *
646 * // Define simple arithmetic operations
647 * const increment = (x: number) => x + 1
648 * const double = (x: number) => x * 2
649 * const subtractTen = (x: number) => x - 10
650 *
651 * // Sequentially apply these operations using `pipe`
652 * const result = pipe(5, increment, double, subtractTen)
653 *
654 * console.log(result)
655 * // Output: 2
656 * ```
657 *
658 * **Example** (Building a simple transformation pipeline)
659 *
660 * ```ts
661 * import { pipe } from "effect"
662 *
663 * // Simple transformation pipeline
664 * const result = pipe(
665 * 5,
666 * (x) => x * 2, // 10
667 * (x) => x + 1, // 11
668 * (x) => x.toString() // "11"
669 * )
670 *
671 * console.log(result) // "11"
672 * ```
673 *
674 * @category combinators
675 * @since 2.0.0
676 */
677export function pipe<A>(a: A): A
678export function pipe<A, B = never>(a: A, ab: (a: A) => B): B
679export function pipe<A, B = never, C = never>(
680 a: A,
681 ab: (a: A) => B,
682 bc: (b: B) => C
683): C
684export function pipe<A, B = never, C = never, D = never>(
685 a: A,
686 ab: (a: A) => B,
687 bc: (b: B) => C,
688 cd: (c: C) => D
689): D
690export function pipe<A, B = never, C = never, D = never, E = never>(
691 a: A,
692 ab: (a: A) => B,
693 bc: (b: B) => C,
694 cd: (c: C) => D,
695 de: (d: D) => E
696): E
697export function pipe<A, B = never, C = never, D = never, E = never, F = never>(
698 a: A,
699 ab: (a: A) => B,
700 bc: (b: B) => C,
701 cd: (c: C) => D,
702 de: (d: D) => E,
703 ef: (e: E) => F
704): F
705export function pipe<
706 A,
707 B = never,
708 C = never,
709 D = never,
710 E = never,
711 F = never,
712 G = never
713>(
714 a: A,
715 ab: (a: A) => B,
716 bc: (b: B) => C,
717 cd: (c: C) => D,
718 de: (d: D) => E,
719 ef: (e: E) => F,
720 fg: (f: F) => G
721): G
722export function pipe<
723 A,
724 B = never,
725 C = never,
726 D = never,
727 E = never,
728 F = never,
729 G = never,
730 H = never
731>(
732 a: A,
733 ab: (a: A) => B,
734 bc: (b: B) => C,
735 cd: (c: C) => D,
736 de: (d: D) => E,
737 ef: (e: E) => F,
738 fg: (f: F) => G,
739 gh: (g: G) => H
740): H
741export function pipe<
742 A,
743 B = never,
744 C = never,
745 D = never,
746 E = never,
747 F = never,
748 G = never,
749 H = never,
750 I = never
751>(
752 a: A,
753 ab: (a: A) => B,
754 bc: (b: B) => C,
755 cd: (c: C) => D,
756 de: (d: D) => E,
757 ef: (e: E) => F,
758 fg: (f: F) => G,
759 gh: (g: G) => H,
760 hi: (h: H) => I
761): I
762export function pipe<
763 A,
764 B = never,
765 C = never,
766 D = never,
767 E = never,
768 F = never,
769 G = never,
770 H = never,
771 I = never,
772 J = never
773>(
774 a: A,
775 ab: (a: A) => B,
776 bc: (b: B) => C,
777 cd: (c: C) => D,
778 de: (d: D) => E,
779 ef: (e: E) => F,
780 fg: (f: F) => G,
781 gh: (g: G) => H,
782 hi: (h: H) => I,
783 ij: (i: I) => J
784): J
785export function pipe<
786 A,
787 B = never,
788 C = never,
789 D = never,
790 E = never,
791 F = never,
792 G = never,
793 H = never,
794 I = never,
795 J = never,
796 K = never
797>(
798 a: A,
799 ab: (a: A) => B,
800 bc: (b: B) => C,
801 cd: (c: C) => D,
802 de: (d: D) => E,
803 ef: (e: E) => F,
804 fg: (f: F) => G,
805 gh: (g: G) => H,
806 hi: (h: H) => I,
807 ij: (i: I) => J,
808 jk: (j: J) => K
809): K
810export function pipe<
811 A,
812 B = never,
813 C = never,
814 D = never,
815 E = never,
816 F = never,
817 G = never,
818 H = never,
819 I = never,
820 J = never,
821 K = never,
822 L = never
823>(
824 a: A,
825 ab: (a: A) => B,
826 bc: (b: B) => C,
827 cd: (c: C) => D,
828 de: (d: D) => E,
829 ef: (e: E) => F,
830 fg: (f: F) => G,
831 gh: (g: G) => H,
832 hi: (h: H) => I,
833 ij: (i: I) => J,
834 jk: (j: J) => K,
835 kl: (k: K) => L
836): L
837export function pipe<
838 A,
839 B = never,
840 C = never,
841 D = never,
842 E = never,
843 F = never,
844 G = never,
845 H = never,
846 I = never,
847 J = never,
848 K = never,
849 L = never,
850 M = never
851>(
852 a: A,
853 ab: (a: A) => B,
854 bc: (b: B) => C,
855 cd: (c: C) => D,
856 de: (d: D) => E,
857 ef: (e: E) => F,
858 fg: (f: F) => G,
859 gh: (g: G) => H,
860 hi: (h: H) => I,
861 ij: (i: I) => J,
862 jk: (j: J) => K,
863 kl: (k: K) => L,
864 lm: (l: L) => M
865): M
866export function pipe<
867 A,
868 B = never,
869 C = never,
870 D = never,
871 E = never,
872 F = never,
873 G = never,
874 H = never,
875 I = never,
876 J = never,
877 K = never,
878 L = never,
879 M = never,
880 N = never
881>(
882 a: A,
883 ab: (a: A) => B,
884 bc: (b: B) => C,
885 cd: (c: C) => D,
886 de: (d: D) => E,
887 ef: (e: E) => F,
888 fg: (f: F) => G,
889 gh: (g: G) => H,
890 hi: (h: H) => I,
891 ij: (i: I) => J,
892 jk: (j: J) => K,
893 kl: (k: K) => L,
894 lm: (l: L) => M,
895 mn: (m: M) => N
896): N
897export function pipe<
898 A,
899 B = never,
900 C = never,
901 D = never,
902 E = never,
903 F = never,
904 G = never,
905 H = never,
906 I = never,
907 J = never,
908 K = never,
909 L = never,
910 M = never,
911 N = never,
912 O = never
913>(
914 a: A,
915 ab: (a: A) => B,
916 bc: (b: B) => C,
917 cd: (c: C) => D,
918 de: (d: D) => E,
919 ef: (e: E) => F,
920 fg: (f: F) => G,
921 gh: (g: G) => H,
922 hi: (h: H) => I,
923 ij: (i: I) => J,
924 jk: (j: J) => K,
925 kl: (k: K) => L,
926 lm: (l: L) => M,
927 mn: (m: M) => N,
928 no: (n: N) => O
929): O
930export function pipe<
931 A,
932 B = never,
933 C = never,
934 D = never,
935 E = never,
936 F = never,
937 G = never,
938 H = never,
939 I = never,
940 J = never,
941 K = never,
942 L = never,
943 M = never,
944 N = never,
945 O = never,
946 P = never
947>(
948 a: A,
949 ab: (a: A) => B,
950 bc: (b: B) => C,
951 cd: (c: C) => D,
952 de: (d: D) => E,
953 ef: (e: E) => F,
954 fg: (f: F) => G,
955 gh: (g: G) => H,
956 hi: (h: H) => I,
957 ij: (i: I) => J,
958 jk: (j: J) => K,
959 kl: (k: K) => L,
960 lm: (l: L) => M,
961 mn: (m: M) => N,
962 no: (n: N) => O,
963 op: (o: O) => P
964): P
965export function pipe<
966 A,
967 B = never,
968 C = never,
969 D = never,
970 E = never,
971 F = never,
972 G = never,
973 H = never,
974 I = never,
975 J = never,
976 K = never,
977 L = never,
978 M = never,
979 N = never,
980 O = never,
981 P = never,
982 Q = never
983>(
984 a: A,
985 ab: (a: A) => B,
986 bc: (b: B) => C,
987 cd: (c: C) => D,
988 de: (d: D) => E,
989 ef: (e: E) => F,
990 fg: (f: F) => G,
991 gh: (g: G) => H,
992 hi: (h: H) => I,
993 ij: (i: I) => J,
994 jk: (j: J) => K,
995 kl: (k: K) => L,
996 lm: (l: L) => M,
997 mn: (m: M) => N,
998 no: (n: N) => O,
999 op: (o: O) => P,
1000 pq: (p: P) => Q
1001): Q
1002export function pipe<
1003 A,
1004 B = never,
1005 C = never,
1006 D = never,
1007 E = never,
1008 F = never,
1009 G = never,
1010 H = never,
1011 I = never,
1012 J = never,
1013 K = never,
1014 L = never,
1015 M = never,
1016 N = never,
1017 O = never,
1018 P = never,
1019 Q = never,
1020 R = never
1022 a: A,
1023 ab: (a: A) => B,
1024 bc: (b: B) => C,
1025 cd: (c: C) => D,
1026 de: (d: D) => E,
1027 ef: (e: E) => F,
1028 fg: (f: F) => G,
1029 gh: (g: G) => H,
1030 hi: (h: H) => I,
1031 ij: (i: I) => J,
1032 jk: (j: J) => K,
1033 kl: (k: K) => L,
1034 lm: (l: L) => M,
1035 mn: (m: M) => N,
1036 no: (n: N) => O,
1037 op: (o: O) => P,
1038 pq: (p: P) => Q,
1039 qr: (q: Q) => R
1040): R
1041export function pipe<
1042 A,
1043 B = never,
1044 C = never,
1045 D = never,
1046 E = never,
1047 F = never,
1048 G = never,
1049 H = never,
1050 I = never,
1051 J = never,
1052 K = never,
1053 L = never,
1054 M = never,
1055 N = never,
1056 O = never,
1057 P = never,
1058 Q = never,
1059 R = never,
1060 S = never
1062 a: A,
1063 ab: (a: A) => B,
1064 bc: (b: B) => C,
1065 cd: (c: C) => D,
1066 de: (d: D) => E,
1067 ef: (e: E) => F,
1068 fg: (f: F) => G,
1069 gh: (g: G) => H,
1070 hi: (h: H) => I,
1071 ij: (i: I) => J,
1072 jk: (j: J) => K,
1073 kl: (k: K) => L,
1074 lm: (l: L) => M,
1075 mn: (m: M) => N,
1076 no: (n: N) => O,
1077 op: (o: O) => P,
1078 pq: (p: P) => Q,
1079 qr: (q: Q) => R,
1080 rs: (r: R) => S
1081): S
1082export function pipe<
1083 A,
1084 B = never,
1085 C = never,
1086 D = never,
1087 E = never,
1088 F = never,
1089 G = never,
1090 H = never,
1091 I = never,
1092 J = never,
1093 K = never,
1094 L = never,
1095 M = never,
1096 N = never,
1097 O = never,
1098 P = never,
1099 Q = never,
1100 R = never,
1101 S = never,
1102 T = never
1104 a: A,
1105 ab: (a: A) => B,
1106 bc: (b: B) => C,
1107 cd: (c: C) => D,
1108 de: (d: D) => E,
1109 ef: (e: E) => F,
1110 fg: (f: F) => G,
1111 gh: (g: G) => H,
1112 hi: (h: H) => I,
1113 ij: (i: I) => J,
1114 jk: (j: J) => K,
1115 kl: (k: K) => L,
1116 lm: (l: L) => M,
1117 mn: (m: M) => N,
1118 no: (n: N) => O,
1119 op: (o: O) => P,
1120 pq: (p: P) => Q,
1121 qr: (q: Q) => R,
1122 rs: (r: R) => S,
1123 st: (s: S) => T
1124): T
1125export function pipe(a: unknown, ...args: Array<any>): unknown {
1126 return pipeArguments(a, args as any)
1128​
1129/**
1130 * Performs left-to-right function composition.
1132 * **When to use**
1134 * Use to build a reusable function from a left-to-right sequence of
1135 * transformations.
1137 * **Details**
1139 * The first function may have any arity. Every following function must be
1140 * unary.
1142 * **Example** (Composing functions left to right)
1144 * ```ts
1145 * import { flow } from "effect"
1146 * import * as assert from "node:assert"
1148 * const len = (s: string): number => s.length
1149 * const double = (n: number): number => n * 2
1151 * const f = flow(len, double)
1153 * assert.strictEqual(f("aaa"), 6)
1154 * ```
1156 * @see {@link pipe} for applying a value through a left-to-right sequence immediately
1157 * @see {@link compose} for composing exactly two functions
1159 * @category combinators
1160 * @since 2.0.0
1161 */
1162export function flow<A extends ReadonlyArray<unknown>, B = never>(
1163 ab: (...a: A) => B
1164): (...a: A) => B
1165export function flow<A extends ReadonlyArray<unknown>, B = never, C = never>(
1166 ab: (...a: A) => B,
1167 bc: (b: B) => C
1168): (...a: A) => C
1169export function flow<
1170 A extends ReadonlyArray<unknown>,
1171 B = never,
1172 C = never,
1173 D = never
1174>(ab: (...a: A) => B, bc: (b: B) => C, cd: (c: C) => D): (...a: A) => D
1175export function flow<
1176 A extends ReadonlyArray<unknown>,
1177 B = never,
1178 C = never,
1179 D = never,
1180 E = never
1182 ab: (...a: A) => B,
1183 bc: (b: B) => C,
1184 cd: (c: C) => D,
1185 de: (d: D) => E
1186): (...a: A) => E
1187export function flow<
1188 A extends ReadonlyArray<unknown>,
1189 B = never,
1190 C = never,
1191 D = never,
1192 E = never,
1193 F = never
1195 ab: (...a: A) => B,
1196 bc: (b: B) => C,
1197 cd: (c: C) => D,
1198 de: (d: D) => E,
1199 ef: (e: E) => F
1200): (...a: A) => F
1201export function flow<
1202 A extends ReadonlyArray<unknown>,
1203 B = never,
1204 C = never,
1205 D = never,
1206 E = never,
1207 F = never,
1208 G = never
1210 ab: (...a: A) => B,
1211 bc: (b: B) => C,
1212 cd: (c: C) => D,
1213 de: (d: D) => E,
1214 ef: (e: E) => F,
1215 fg: (f: F) => G
1216): (...a: A) => G
1217export function flow<
1218 A extends ReadonlyArray<unknown>,
1219 B = never,
1220 C = never,
1221 D = never,
1222 E = never,
1223 F = never,
1224 G = never,
1225 H = never
1227 ab: (...a: A) => B,
1228 bc: (b: B) => C,
1229 cd: (c: C) => D,
1230 de: (d: D) => E,
1231 ef: (e: E) => F,
1232 fg: (f: F) => G,
1233 gh: (g: G) => H
1234): (...a: A) => H
1235export function flow<
1236 A extends ReadonlyArray<unknown>,
1237 B = never,
1238 C = never,
1239 D = never,
1240 E = never,
1241 F = never,
1242 G = never,
1243 H = never,
1244 I = never
1246 ab: (...a: A) => B,
1247 bc: (b: B) => C,
1248 cd: (c: C) => D,
1249 de: (d: D) => E,
1250 ef: (e: E) => F,
1251 fg: (f: F) => G,
1252 gh: (g: G) => H,
1253 hi: (h: H) => I
1254): (...a: A) => I
1255export function flow<
1256 A extends ReadonlyArray<unknown>,
1257 B = never,
1258 C = never,
1259 D = never,
1260 E = never,
1261 F = never,
1262 G = never,
1263 H = never,
1264 I = never,
1265 J = never
1267 ab: (...a: A) => B,
1268 bc: (b: B) => C,
1269 cd: (c: C) => D,
1270 de: (d: D) => E,
1271 ef: (e: E) => F,
1272 fg: (f: F) => G,
1273 gh: (g: G) => H,
1274 hi: (h: H) => I,
1275 ij: (i: I) => J
1276): (...a: A) => J
1277export function flow(
1278 ab: Function,
1279 bc?: Function,
1280 cd?: Function,
1281 de?: Function,
1282 ef?: Function,
1283 fg?: Function,
1284 gh?: Function,
1285 hi?: Function,
1286 ij?: Function
1287): unknown {
1288 switch (arguments.length) {
1289 case 1:
1290 return ab
1291 case 2:
1292 return function(this: unknown) {
1293 return bc!(ab.apply(this, arguments))
1295 case 3:
1296 return function(this: unknown) {
1297 return cd!(bc!(ab.apply(this, arguments)))
1299 case 4:
1300 return function(this: unknown) {
1301 return de!(cd!(bc!(ab.apply(this, arguments))))
1303 case 5:
1304 return function(this: unknown) {
1305 return ef!(de!(cd!(bc!(ab.apply(this, arguments)))))
1307 case 6:
1308 return function(this: unknown) {
1309 return fg!(ef!(de!(cd!(bc!(ab.apply(this, arguments))))))
1311 case 7:
1312 return function(this: unknown) {
1313 return gh!(fg!(ef!(de!(cd!(bc!(ab.apply(this, arguments)))))))
1315 case 8:
1316 return function(this: unknown) {
1317 return hi!(gh!(fg!(ef!(de!(cd!(bc!(ab.apply(this, arguments))))))))
1319 case 9:
1320 return function(this: unknown) {
1321 return ij!(hi!(gh!(fg!(ef!(de!(cd!(bc!(ab.apply(this, arguments)))))))))
1324 return
1326​
1327/**
1328 * Creates a compile-time placeholder for a value of any type.
1330 * **When to use**
1332 * Use as a temporary typed placeholder while developing incomplete code.
1334 * **Gotchas**
1336 * `hole` is intended for temporary development use. If the placeholder is
1337 * evaluated at runtime, it throws.
1339 * **Example** (Creating a development placeholder)
1341 * ```ts
1342 * import { hole } from "effect"
1344 * // Intentionally not called: `hole` throws if the placeholder is evaluated.
1345 * const buildUser = (id: number): { readonly id: number; readonly name: string } => ({
1346 * id,
1347 * name: hole<string>()
1348 * })
1350 * console.log(typeof buildUser) // "function"
1351 * ```
1353 * @category utility types
1354 * @since 2.0.0
1355 */
1356export const hole: <T>() => T = cast(absurd)
1357​
1358/**
1359 * Returns the second argument and discards the first. The SK combinator is
1360 * a fundamental combinator in the lambda calculus and the SKI combinator
1361 * calculus.
1363 * **When to use**
1365 * Use to discard the first argument and return the second argument.
1367 * **Example** (Discarding the first argument)
1369 * ```ts
1370 * import { Function } from "effect"
1371 * import * as assert from "node:assert"
1373 * assert.deepStrictEqual(Function.SK(0, "hello"), "hello")
1374 * ```
1376 * @category combinators
1377 * @since 2.0.0
1378 */
1379export const SK = <A, B>(_: A, b: B): B => b
1380​
1381/**
1382 * Creates a memoized function whose input is an object, caching results by
1383 * object identity.
1385 * **When to use**
1387 * Use to reuse the result of a synchronous computation whose output is stable
1388 * for a given object reference.
1390 * **Details**
1392 * Each memoized wrapper owns a private `WeakMap` keyed by object identity.
1393 * Cached `undefined` results are still returned because the cache is checked
1394 * with `WeakMap.has`.
1396 * **Gotchas**
1398 * Structurally equal objects do not share cache entries. If the same object is
1399 * mutated after its first call, later calls still return the cached result for
1400 * that reference.
1402 * @category caching
1403 * @since 4.0.0
1404 */
1405export function memoize<A extends object, O>(f: (a: A) => O): (ast: A) => O {
1406 const cache = new WeakMap<object, O>()
1407 return (a) => {
1408 if (cache.has(a)) {
1409 return cache.get(a)!
1411 const result = f(a)
1412 cache.set(a, result)
1413 return result