-
Notifications
You must be signed in to change notification settings - Fork 7
/
fn.ts
916 lines (881 loc) · 21 KB
/
fn.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
/**
* This file contains the Fn algebraic data type. Fn is short for a unary
* function, which is to say a function that only takes one input variable.
* Most computation can be encoded in Fn, but the standard ADT in most
* functional languages is called Reader.
*
* @module Fn
* @since 2.0.0
*/
import type { In, Kind, Out } from "./kind.ts";
import type { Applicable } from "./applicable.ts";
import type { Composable } from "./composable.ts";
import type { Bind, Flatmappable, Tap } from "./flatmappable.ts";
import type { BindTo, Mappable } from "./mappable.ts";
import type { Premappable } from "./premappable.ts";
import type { Wrappable } from "./wrappable.ts";
import { createBind, createTap } from "./flatmappable.ts";
import { createBindTo } from "./mappable.ts";
/**
* A Fn, also known as Reader or Environment, is a type over a unary
* javascript function. ie. (a: number) => string can be
* a Fn. As an algebraic data type, the associated type class instances
* for Fn are limited to single variable inputs so they will look like
* (a: number) => string, with only one argument. The purposes of a Fn
* are many and varied, some common purposes are: computation, reading
* values from a shared environment, and sub-computations in a modified
* environment. In many ways Fn is a more powerful abstraction than
* State, and indeed the State monad in fun is exactly State<S, A> =
* Fn<[S], [A, S]>.
*
* Currently, there is no implementation of Chain recursion or
* trampolining for Fn implemented, but it is likely to be a future
* feature. Once implemented Fn will gain some much needed stack safety.
*
* @since 2.0.0
*/
export type Fn<D, A> = (d: D) => A;
/**
* A Fn type over any, useful for constraining generics that
* take or return Fns.
*
* @since 2.0.0
*/
// deno-lint-ignore no-explicit-any
export type AnyFn = Fn<any, any>;
/**
* Specifies Fn as a Higher Kinded Type, with
* covariant parameter A corresponding to the 0th
* index of any Substitutions and a contravariant
* parameter D corresponding to the 0th index of
* any Substititions. The Fn KindFn is unique in that
* it constrains the Fn type to taking a single
* argument for the purposes of type substitution
* while the implementations of Fn combinators such
* as map, flatmap, etc are mostly variadic (multiple
* arguments).
*
* @since 2.0.0
*/
export interface KindFn extends Kind {
readonly kind: Fn<In<this, 0>, Out<this, 0>>;
}
/**
* Take a variadic Fn and make it unary, collapsing multiple arguments
* into a single tuple argument.
*
* @example
* ```ts
* import * as F from "./fn.ts";
*
* const person = (age: number, name: string) => ({ age, name });
* const personUnary = F.unary(person);
*
* // ({ name: "Brandon", age: 37 })
* const result1 = personUnary([37, "Brandon"]);
* ```
* @since 2.0.0
*/
export function unary<D extends unknown[], A>(fda: (...d: D) => A): Fn<D, A> {
return (d) => fda(...d);
}
/**
* @since 2.0.0
*/
export function curry2<A, B, C>(fn: (a: A, b: B) => C): (a: A) => (b: B) => C {
return (a) => (b) => fn(a, b);
}
/**
* @since 2.0.0
*/
export function uncurry2<A, B, C>(
fn: (b: B) => (a: A) => C,
): (a: A, b: B) => C {
return (a, b) => fn(b)(a);
}
/**
* A common pattern in optics is to apply an input value to a function at the
* beginning and the end of a computation. This can (and has) been achieved by
* the composition of Pair using flow(P.dup, P.map(fn), P.merge). But for
* performance reasons it's nice to have a straighforward function that achieves
* the same result.
*
* @since 2.0.0
*/
export function over<A, I>(faai: (a: A) => (a: A) => I): (a: A) => I {
return (a) => faai(a)(a);
}
/**
* Wrap a thunk (a Fn that takes no arguments) in a try catch block, using
* an onThrow function (that should itself never throw) to handle a default
* case should the original function throw. This is useful for wrapping
* functions that might throw.
*
* @example
* ```ts
* import * as F from "./fn.ts";
*
* const getZero = (): number => {
* if (Math.random() > 0.5) {
* throw new Error("Too high!");
* }
* return 0;
* }
*
* const result = F.tryThunk(getZero, () => 0); // 0
* ```
*
* @since 2.0.0
*/
export function tryThunk<A, E>(ua: Fn<void, A>, onThrow: Fn<E, A>): A {
try {
return ua();
} catch (err) {
return onThrow(err);
}
}
/**
* Wrap any function in a try catch block, passing the result and
* arguments to onResult when the function does not throw as well
* as passing the error and arguments to the onThrow function when
* it does. Neither the onResult nor the onThrow functions should
* ever throw an error themselves. None of the functions in the fun
* library will throw on their own. If they do it is a bug in fun
* or the underlying runtime (or you used fun in javascript). This
* function primarily exists to wrap functions exported from other
* libraries that may use exceptions as their error mechanism. This
* makes those functions safe to use with fun.
*
* @example
* ```ts
* import * as F from "./fn.ts";
*
* const throwOverFive = (n: number): number => {
* if (n > 5) {
* throw new Error("Larger than five");
* }
* return n;
* }
* const caught = F.handleThrow(
* throwOverFive,
* result => result,
* err => 0, // Default to 0
* );
*
* const result1 = caught(0); // 0
* const result2 = caught(5); // 5
* const result3 = caught(6); // 0
* ```
*
* @since 2.0.0
*/
export function handleThrow<D extends unknown[], A, I>(
ua: (...d: D) => A,
onResult: (result: A, args: D) => I,
onThrow: (error: unknown, args: D) => I,
): (...d: D) => I {
return (...d) => {
try {
return onResult(ua(...d), d);
} catch (err) {
return onThrow(err, d);
}
};
}
/**
* @since 2.0.0
*/
export function tryCatch<D extends unknown[], A>(
fda: (...d: D) => A,
onThrow: (e: unknown, d: D) => A,
): (...d: D) => A {
return handleThrow(fda, identity, onThrow);
}
/**
* Memoize a unary function using a Map. This
* means that this algorithm puposefully leaks memory.
*
* TODO: Extend memoize to be variadic.
*
* @example
* ```ts
* import * as F from "./fn.ts";
*
* // Big old expensive recursive algorithm
* const fib = (n: number): number =>
* n < 1 ? 0 :
* n <= 1 ? 1 :
* fib(n - 2) + fib(n - 1);
*
* const mfib = F.memoize(fib);
*
* const result1 = mfib(10); // 55
* const result2 = mfib(10); // 55 but does not recompute
* ```
*
* @since 2.0.0
*/
export function memoize<D, A>(ua: Fn<D, A>): Fn<D, A> {
const cache = new Map<D, A>();
return (d) => {
if (cache.has(d)) {
return cache.get(d) as A;
}
const a = ua(d);
cache.set(d, a);
return a;
};
}
/**
* A function that can be called to output any type. It's used for type
* hole based programming. This allows one to define interfaces and types
* for a function and stub them with todo() until you are ready to implement
* the actual behavior. The todo function will throw if it is ever actually
* called.
*
* @example
* ```ts
* import { todo } from "./fn.ts";
*
* type InOut = {
* read: () => Promise<string>,
* write: (s: string) => Promise<unknown>,
* }
*
* const mockInOut: InOut = todo(); // InOut !!THROWS!!
* ```
*
* @since 2.0.0
*/
export function todo<T>(): T {
throw new Error("TODO: this function has not been implemented");
}
/**
* Does an unsafe type coercion on any type. This is only safe when
* the types A and I have referential transparency. This is to say
* when the type A can be substituted for I and I for A at runtime
* without there being any change to the operation of the program.
* The primary use case for unsafeCoerce is in Newtype implementations.
*
* @since 2.0.0
*/
export function unsafeCoerce<A, I>(a: A): I {
return a as unknown as I;
}
/**
* The flow function is like the pipe function without the initial value. It
* composes up to 9 functions from left to right (top to bottom). The first
* function can take multiple arguments but every subsequent function must
* be unary (only take one argument).
*
* @example
* ```ts
* import { flow } from "./fn.ts";
*
* const add = (m: number) => (n: number) => m + n;
* const multiply = (m: number) => (n: number) => m * n;
*
* const flowed = flow(
* add(1),
* multiply(2),
* add(1),
* multiply(2),
* );
*
* const result1 = flowed(1); // 10
* const result2 = flowed(2); // 14
* const result3 = flowed(3); // 18
* ```
*
* @since 2.0.0
*/
export function flow<A extends unknown[], B>(
ab: (...a: A) => B,
): (...a: A) => B;
export function flow<A extends unknown[], B, C>(
ab: (...a: A) => B,
bc: (b: B) => C,
): (...a: A) => C;
export function flow<A extends unknown[], B, C, D>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
): (...a: A) => D;
export function flow<A extends unknown[], B, C, D, E>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
): (...a: A) => E;
export function flow<A extends unknown[], B, C, D, E, F>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
): (...a: A) => F;
export function flow<A extends unknown[], B, C, D, E, F, G>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
): (...a: A) => G;
export function flow<A extends unknown[], B, C, D, E, F, G, H>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
): (...a: A) => H;
export function flow<A extends unknown[], B, C, D, E, F, G, H, I>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
): (...a: A) => I;
export function flow<
A extends unknown[],
B,
C,
D,
E,
F,
G,
H,
I,
J,
>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
): (...a: A) => J;
export function flow<
A extends unknown[],
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
): (...a: A) => K;
export function flow<
A extends unknown[],
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
>(
ab: (...a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
kl: (k: K) => L,
): (...a: A) => L;
export function flow(
...[ab, bc, cd, de, ef, fg, gh, hi, ij, jk, kl, ...rest]: AnyFn[]
): AnyFn {
switch (arguments.length) {
case 1:
return (...as) => ab(...as);
case 2:
return (...as) => bc(ab(...as));
case 3:
return (...as) => cd(bc(ab(...as)));
case 4:
return (...as) => de(cd(bc(ab(...as))));
case 5:
return (...as) => ef(de(cd(bc(ab(...as)))));
case 6:
return (...as) => fg(ef(de(cd(bc(ab(...as))))));
case 7:
return (...as) => gh(fg(ef(de(cd(bc(ab(...as)))))));
case 8:
return (...as) => hi(gh(fg(ef(de(cd(bc(ab(...as))))))));
case 9:
return (...as) => ij(hi(gh(fg(ef(de(cd(bc(ab(...as)))))))));
case 10:
return (...as) => jk(ij(hi(gh(fg(ef(de(cd(bc(ab(...as))))))))));
case 11:
return (...as) => kl(jk(ij(hi(gh(fg(ef(de(cd(bc(ab(...as)))))))))));
default:
return (...as) =>
rest.reduce(
(val, fn) => fn(val),
kl(jk(ij(hi(gh(fg(ef(de(cd(bc(ab(...as))))))))))),
);
}
}
/**
* The pipe takes a value as the first argument and composes it with subsequent
* function arguments, returning the result of the last function passed in. It
* handles and correctly types up to 10 unary functions. Beyond 10 it makes
* sense to break up pipe into multiple pipes.
*
* @example
* ```ts
* import { pipe } from "./fn.ts";
*
* const add = (n: number) => (m: number) => m + n;
* const multiply = (n: number) => (m: number) => m * n;
*
* const result = pipe(
* 1,
* add(1), // 2
* multiply(2), // 4
* add(1), // 5
* multiply(2), // 10
* ); // 10
* ```
*
* @since 2.0.0
*/
export function pipe<A>(a: A): A;
export function pipe<A, B>(a: A, ab: (a: A) => B): B;
export function pipe<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
export function pipe<A, B, C, D>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
): D;
export function pipe<A, B, C, D, E>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
): E;
export function pipe<A, B, C, D, E, F>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
): F;
export function pipe<A, B, C, D, E, F, G>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
): G;
export function pipe<A, B, C, D, E, F, G, H>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
): H;
export function pipe<A, B, C, D, E, F, G, H, I>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
): I;
export function pipe<A, B, C, D, E, F, G, H, I, J>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
): J;
export function pipe<A, B, C, D, E, F, G, H, I, J, K>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
): K;
export function pipe<A, B, C, D, E, F, G, H, I, J, K, L>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
kl: (K: K) => L,
): L;
export function pipe<A, B, C, D, E, F, G, H, I, J, K, L>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F,
fg: (f: F) => G,
gh: (g: G) => H,
hi: (h: H) => I,
ij: (i: I) => J,
jk: (j: J) => K,
kl: (K: K) => L,
end: never,
): L;
export function pipe(
value: unknown,
...fns: AnyFn[]
): unknown {
return fns.reduce((val, fn) => fn(val), value);
}
/**
* Create a Fn that always returns a value. This is equivalent to
* constant.
*
* @example
* ```ts
* import { wrap } from "./fn.ts";
*
* const alwaysA = wrap("A");
*
* const result = alwaysA(null); // "A"
* ```
*
* @since 2.0.0
*/
export function wrap<A, D = unknown>(a: A): Fn<D, A> {
return () => a;
}
/**
* Create a Fn that always returns a value. This is equivalent to
* of but without the ability to specify a contravariant argument.
*
* @example
* ```ts
* import { constant } from "./fn.ts";
*
* const alwaysA = constant("A");
*
* const result = alwaysA(); // "A"
* ```
*
* @since 2.0.0
*/
export function constant<A>(a: A): () => A {
return () => a;
}
/**
* Given L => A => I and D => A create a new Fn
* D & L => I. In order to preserve type widening for
* ap, it only handles unary functions.
*
* @example
* ```ts
* import * as F from "./fn.ts";
*
* type Person = { name: string, age: number };
*
* const person = (name: string) => (age: number): Person => ({ name, age });
*
* const result = F.pipe(
* F.wrap(person),
* F.apply(F.wrap("Brandon")),
* F.apply(F.wrap(37)),
* ); // Fn<[], Person>
* ```
*
* @since 2.0.0
*/
export function apply<D, A>(
ua: Fn<D, A>,
): <L, I>(ufai: Fn<L, (a: A) => I>) => Fn<D & L, I> {
return (ufai) => (d) => ufai(d)(ua(d));
}
/**
* Map over the output of a Fn. This is equivalent to
* function composition.
* ie. a => pipe(f, map(g))(a) === a => g(f(a))
*
* @example
* ```ts
* import { map, wrap, pipe } from "./fn.ts";
*
* const result = pipe(wrap(1), map(n => n + 1)); // 2
* ```
*
* @since 2.0.0
*/
export function map<A, I>(
fai: (a: A) => I,
): <D>(ta: Fn<D, A>) => Fn<D, I> {
return (ta) => flow(ta, fai);
}
/**
* Create a new Fn by combining A => L => I with
* D => A to produce D & L => I. This is equivalent
* to ap with the first two arguments switched. It is
* also limited to unary functions in order to properly
* handle type widening on the input type.
*
* @example
* ```ts
* import { pipe, flatmap } from "./fn.ts";
* const add = (n: number) => (m: number) => n + m;
*
* const flatmaper = pipe(
* (n: number) => n,
* flatmap(add),
* flatmap(add),
* flatmap(add),
* flatmap(add),
* flatmap(add),
* );
*
* const result1 = flatmaper(1); // 6
* const result2 = flatmaper(2); // 12
* const result3 = flatmaper(3); // 18
* ```
*
* @since 2.0.0
*/
export function flatmap<A, I, L>(
fati: (a: A) => Fn<L, I>,
): <D>(ta: Fn<D, A>) => Fn<D & L, I> {
return (ta) => (d) => fati(ta(d))(d);
}
/**
* Map over the input of a function, turning
* D => A and L => D into L => A.
*
* @example
* ```ts
* import { premap, pipe } from "./fn.ts";
*
* const equalsZero = (n: number): boolean => n === 0;
* const strLength = (s: string): number => s.length;
*
* const isEmpty = pipe(
* equalsZero,
* premap(strLength),
* );
*
* const result1 = isEmpty(""); // true
* const result2 = isEmpty("Hello"); // false
* ```
*
* @since 2.0.0
*/
export function premap<L, D>(
fld: (l: L) => D,
): <A>(ta: Fn<D, A>) => Fn<L, A> {
return (ta) => (d) => ta(fld(d));
}
/**
* A combination of premap and map, dimap applies fld
* to the input of a function and fai to the output.
*
* @example
* ```ts
* import type { NonEmptyArray } from "./array.ts";
* import { dimap, pipe } from "./fn.ts";
* import { plural, split } from "./string.ts";
*
* const are = plural("is", "are");
* const words = plural("word", "words");
* const describe = (n: number) => `There ${are(n)} ${n} ${words(n)}`;
*
* const toWords = split(/\s+/g); // string => string[]
* const count = (ws: NonEmptyArray<string>) => ws.length;
*
* const fromString = pipe(
* count,
* dimap(toWords, describe),
* );
*
* const result1 = fromString("Hello World"); // "There are 2 words"
* const result2 = fromString("Hi"); // "There is 1 word"
* const result3 = fromString("This is a test"); // "There are 4 words"
* ```
*
* @since 2.0.0
*/
export function dimap<A, I, L, D>(
fld: (l: L) => D,
fai: (a: A) => I,
): (ta: Fn<D, A>) => Fn<L, I> {
return (ta) => flow(fld, ta, fai);
}
/**
* The canonical identity function. It returns whatever value was
* passed to it.
*
* @example
* ```ts
* import { identity } from "./fn.ts";
*
* const result1 = identity(1); // 1
* const result2 = identity("Hello"); // "Hello"
* ```
* @since 2.0.0
*/
export function identity<A>(a: A): A {
return a;
}
/**
* A thunk over the identity function. It allows one
* to constrain an identity to a specific type.
*
* @example
* ```ts
* import { id } from "./fn.ts";
*
* const idString = id<string>(); // (s: string) => string
* const idNumber = id<number>(); // (n: number) => number
*
* const result1 = idString("Hello"); // "Hello"
* const result2 = idNumber(1); // 1
* ```
*
* @since 2.0.0
*/
export function id<A>(): Fn<A, A> {
return identity;
}
/**
* Compose two functions by taking the output of
* one and passing it to another. This is equivalent
* to the map function.
*
* @example
* ```ts
* import { compose, pipe } from "./fn.ts";
*
* const length = (s: string) => s.length;
* const dup = (n: number) => n + n;
*
* const composed = pipe(
* length,
* compose(dup),
* );
*
* const result1 = composed("Hello"); // 10
* const result2 = composed(""); // 0
* ```
*
* @since 2.0.0
*/
export function compose<A, I>(
second: Fn<A, I>,
): <D>(first: Fn<D, A>) => Fn<D, I> {
return (first) => flow(first, second);
}
/**
* The canonical implementation of Applicable for Fn. It contains
* the methods of, ap, and map.
*
* @since 2.0.0
*/
export const ApplicableFn: Applicable<KindFn> = { apply, map, wrap };
/**
* @since 2.0.0
*/
export const ComposableFn: Composable<KindFn> = { compose, id };
/**
* The canonical implementation of Flatmappable for Fn. It contains
* the methods of, ap, map, join, and flatmap.
*
* @since 2.0.0
*/
export const FlatmappableFn: Flatmappable<KindFn> = {
apply,
flatmap,
map,
wrap,
};
/**
* The canonical implementation of Mappable for Fn. It contains
* the method map.
*
* @since 2.0.0
*/
export const MappableFn: Mappable<KindFn> = { map };
/**
* The canonical implementation of Premappable for Fn. It contains
* the method premap.
*
* @since 2.0.0
*/
export const PremappableFn: Premappable<KindFn> = { premap, map, dimap };
/**
* @since 2.0.0
*/
export const WrappableFn: Wrappable<KindFn> = { wrap };
/**
* @since 2.0.0
*/
export const tap: Tap<KindFn> = createTap(FlatmappableFn);
/**
* @since 2.0.0
*/
export const bind: Bind<KindFn> = createBind(FlatmappableFn);
/**
* @since 2.0.0
*/
export const bindTo: BindTo<KindFn> = createBindTo(MappableFn);