-
Notifications
You must be signed in to change notification settings - Fork 2
/
wholePrelude.js
6563 lines (5689 loc) · 165 KB
/
wholePrelude.js
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
/* eslint-disable max-lines-per-function */
/* eslint-disable no-undef */
/* eslint-disable strict */
/* eslint-disable no-unused-vars */
// Endo :: (a -> a) -> Endo a
const Endo = f =>
// An endofunction lifted into an Endo object.
// A wrapper around an (a -> a) function, used as
// the monoid of endomorphisms under composition.
({
type: "Endo",
appEndo: f
});
// Just :: a -> Maybe a
const Just = x => ({
type: "Maybe",
Just: x
});
// Left :: a -> Either a b
const Left = x => ({
type: "Either",
Left: x
});
// Node :: a -> [Tree a] -> Tree a
const Node = v =>
// Constructor for a Tree node which connects a
// value of some kind to a list of zero or
// more child trees.
xs => ({
type: "Node",
root: v,
nest: xs || []
});
// Nothing :: Maybe a
const Nothing = () => ({
type: "Maybe",
Nothing: true
});
// Ratio :: Integral a => a -> a -> Ratio a
const Ratio = a => b => {
const go = (x, y) =>
0 !== y
? (() => {
const d = gcd(x)(y);
return {
type: "Ratio",
// numerator
"n": Math.trunc(x / d),
// denominator
"d": Math.trunc(y / d)
};
})()
: undefined;
return go(a * signum(b), abs(b));
};
// Right :: b -> Either a b
const Right = x => ({
type: "Either",
Right: x
});
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
// A pair of values, possibly of
// different types.
b => ({
type: "Tuple",
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
});
// Tuple3 (,,) :: a -> b -> c -> (a, b, c)
const Tuple3 = a => b => c => ({
type: "Tuple3",
"0": a,
"1": b,
"2": c,
length: 3,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
});
// TupleN :: a -> b ... -> (a, b ... )
const TupleN = (...args) => {
// A Tuple of an arbitrary number of items.
const n = args.length;
return {
...args.reduce(
(a, x, i) => ({
...a,
[i]: x
}),
{
type: 2 !== n
? `Tuple${n}`
: "Tuple",
length: n,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
})
};
};
// ZipList :: a -> {getZipList :: [a]}
const ZipList = x => ({
// Constructor for an applicative ZipList
type: "ZipList",
getZipList: x
});
// abs :: Num -> Num
const abs = x =>
// Absolute value of a given number
// without the sign.
0 > x
? -x
: x;
// add (+) :: Num a => a -> a -> a
const add = a =>
// Curried addition.
b => a + b;
// adjust :: (a -> a) -> Key ->
// Dict Key a -> Dict Key a
const adjust = f =>
// The orginal dictionary, unmodified, if k is
// not an existing key.
// Otherwise, a new copy in which the existing
// value of k is updated by application of f.
k => dict => k in dict
? {
...dict,
[k]: f(dict[k])
}
: dict;
// alert :: String => String -> IO String
const alert = title =>
// Display of a given title and message.
s => {
const sa = Object.assign(
Application("System Events"), {
includeStandardAdditions: true
});
return (
sa.activate(),
sa.displayDialog(s, {
withTitle: title,
buttons: ["OK"],
defaultButton: "OK"
}),
s
);
};
// all :: (a -> Bool) -> [a] -> Bool
const all = p =>
// True if p(x) holds for every x in xs.
xs => [...xs].every(p);
// allSame :: [a] -> Bool
const allSame = xs =>
// True if xs has less than 2 items, or every item
// in the tail of the list is identical to the head.
2 > xs.length || (() => {
const [h, ...t] = xs;
return t.every(x => h === x);
})();
// allTree :: (a -> Bool) -> Tree a -> Bool
const allTree = p =>
// True if p holds for all nodes of the
// tree to which allTree(p) is applied.
foldTree(
x => xs => p(x) && xs.every(Boolean)
);
// and :: [Bool] -> Bool
const and = xs =>
// True unless any value in xs is false.
[...xs].every(Boolean);
// any :: (a -> Bool) -> [a] -> Bool
const any = p =>
// True if p(x) holds for at least
// one item in xs.
xs => [...xs].some(p);
// anyTree :: (a -> Bool) -> Tree a -> Bool
const anyTree = p =>
// True if p holds for any node of the
// tree to which anyTree(p) is applied.
foldTree(
x => xs =>
p(x) || xs.some(Boolean)
);
// ap (<*>) :: Monad m => m (a -> b) -> m a -> m b
const ap = mf =>
// Applies wrapped functions to wrapped values,
// for example applying a list of functions to a list
// of values or applying:
// Just(f) to Just(x), Right(f) to Right(x),
// f(x) to g(x) etc.
mx => ({
"Either": () => apLR,
"Maybe": () => apMay,
"Node": () => apTree,
"Tuple": () => apTuple,
"List": () => apList,
"(a -> b)": () => apFn
})[typeName(mx) || "List"]()(mf)(mx);
// apFn :: (a -> b -> c) -> (a -> b) -> (a -> c)
const apFn = f =>
// Applicative instance for functions.
// f(x) applied to g(x).
g => x => f(x)(
g(x)
);
// apLR (<*>) :: Either e (a -> b) -> Either e a -> Either e b
const apLR = flr =>
// Either a Left value, or the application of a
// function in Either to a value in Either.
liftA2LR(x => x)(flr);
// apList (<*>) :: [(a -> b)] -> [a] -> [b]
const apList = fs =>
// The sequential application of each of a list
// of functions to each of a list of values.
// apList([x => 2 * x, x => 20 + x])([1, 2, 3])
// -> [2, 4, 6, 21, 22, 23]
xs => fs.flatMap(f => xs.map(f));
// apMay (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b
const apMay = mf =>
// Just an application of Maybe a function to
// to Maybe a value, or Nothing.
liftA2May(x => x)(mf);
// apTree (<*>) :: Tree (a -> b) -> Tree a -> Tree b
const apTree = tf =>
// A new tree derived by applying each of a tree
// of functions to each node value in another tree.
liftA2Tree(
x => x
)(tf);
// apTuple (<*>) :: Monoid m => (m, (a -> b)) -> (m, a) -> (m, b)
const apTuple = ab =>
// A tuple obtained by applying the function in the second
// value of ab to the second value in an existing tuple,
// and concatenating the first values of each tuple.
liftA2Tuple(x => x)(ab);
// apZL (<*>) :: ZipList (a -> b) -> ZipList a -> ZipList b
// The application of a function in one ZipList
// to each value in another ZipList.
const apZL = zf =>
liftA2ZL(x => x)(zf);
// apZip :: [(a -> b)] -> [a] -> [b]
const apZip = fs =>
// Zip applicative.
// Each function in fs applied to the value
// in the corresponding position in xs.
xs => fs.slice(
0, Math.min(fs.length, xs.length)
)
.map((f, i) => f(xs[i]));
// appEndo :: Endo a -> (a -> a)
const appEndo = endo =>
// Accessor for the function in an Endo type.
endo.appEndo;
// append (<>) :: [a] -> [a] -> [a]
const append = xs =>
// Two lists joined into one.
ys => xs.concat(ys);
// appendFile :: FilePath -> String -> IO Bool
const appendFile = fp =>
// The file at fp updated with a new string
// appended to its existing contents.
txt => {
const fpFull = filePath(fp);
return doesFileExist(fpFull)
? (() => {
const
h = $.NSFileHandle
.fileHandleForWritingAtPath(
$(fpFull)
);
return (
h.seekToEndOfFile,
h.writeData(
$(txt)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
),
h.closeFile,
true
);
})()
: doesDirectoryExist(takeDirectory(fpFull))
? (writeFile(fpFull)(txt), true)
: false;
};
// appendFileMay :: FilePath -> String -> Maybe IO FilePath
const appendFileMay = fp =>
// Just the fully-expanded file path of
// any file at found strPath, after it has been
// updated by appending the given string, or
// Nothing if no file is found at that path,
// or the file is found but can not be updated.
txt => {
const fpFull = filePath(fp);
return doesFileExist(fpFull)
? (() => {
const
h = $.NSFileHandle
.fileHandleForWritingAtPath(
$(fpFull)
);
return (
h.seekToEndOfFile,
h.writeData(
$(txt)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
),
h.closeFile,
Just(fpFull)
);
})()
: doesDirectoryExist(takeDirectory(fpFull))
? (
writeFile(fpFull)(txt),
Just(fpFull)
)
: Nothing();
};
// appendGen (++) :: Gen [a] -> Gen [a] -> Gen [a]
const appendGen = xs =>
// A new generator composed from the
// concatenation of two existing generators.
function *(ys) {
for (const vs of [xs, ys]) {
let nxt = vs.next();
while (!nxt.done) {
yield nxt.value;
nxt = vs.next();
}
}
};
// apply ($) :: (a -> b) -> a -> b
const apply = f =>
// Application operator.
x => f(x);
// applyN :: Int -> (a -> a) -> a -> a
const applyN = n =>
// The value of n applications of f to x.
// (Church numeral n)
f => x => Array.from({
length: n
}, () => f)
.reduce((a, g) => g(a), x);
// approxRatio :: Real -> Real -> Ratio
const approxRatio = epsilon =>
n => {
const
c = gcdApprox(
Boolean(epsilon)
? epsilon
: (1 / 10000)
)(1, n);
return Ratio(
Math.floor(n / c)
)(
Math.floor(1 / c)
);
};
// argvLength :: Function -> Int
const argvLength = f =>
// The number of arguments defined for the given function.
f.length;
// assocs :: Map k a -> [(k, a)]
const assocs = m =>
// A list of (key, value) tuples derived from
// the given dictionary.
Object.entries(m).map(
([k, v]) => Tuple(k)(v)
);
// base64decode :: String -> String
const base64decode = s =>
// Base64 decoding of the given string.
ObjC.unwrap(
$.NSString.alloc.initWithDataEncoding(
$.NSData.alloc.initWithBase64EncodedStringOptions(
s, $.NSDataBase64DecodingIgnoreUnknownCharacters
),
$.NSUTF8StringEncoding
)
);
// base64encode :: String -> String
const base64encode = s =>
// Base64 encoding of the given string.
ObjC.unwrap(
$.NSString.stringWithString(s)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
.base64EncodedStringWithOptions(0)
);
// biList :: (a, a) -> [a]
const biList = ab =>
// A list of two items derived from a tuple.
[...ab];
// bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
const bimap = f =>
// Tuple instance of bimap.
// A tuple of the application of f and g to the
// first and second values respectively.
g => tpl => Tuple(f(tpl[0]))(
g(tpl[1])
);
// bimapLR :: (a -> b) -> (c -> d) -> ֵEither ֵֵa c -> Either b d
const bimapLR = f =>
// Instance of bimap for Either values.
// Either the application of f to a Left value,
// or the application of g to a Right value.
g => lr => lr.Left
? Left(f(lr.Left))
: Right(g(lr.Right));
// bimapN :: (a -> b) -> (c -> d) -> TupleN -> TupleN
const bimapN = f =>
// An n-tuple instance of bimap.
// An n-tuple of unchanged dimension in which
// the final value is an application of g
// and the penultimate value is an application of f.
g => nTuple => {
const n = nTuple.length;
return 1 < n
? TupleN(
...Array.from(nTuple).slice(0, n - 2),
f(nTuple[n - 2]), g(nTuple[n - 1])
)
: null;
};
// bind (>>=) :: Monad m => m a -> (a -> m b) -> m b
const bind = m =>
// Two computations sequentially composed,
// with any value produced by the first
// passed as an argument to the second.
mf => Array.isArray(m)
? bindList(m)(mf)
: ({
"Either": () => bindLR,
"Maybe": () => bindMay,
"Tuple": () => bindTuple,
"function": () => bindFn
})[m.type || typeof m]()(m)(mf);
// bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c
const bindFn = f =>
// Binary operator applied over f x and x.
op => x => op(f(x))(x);
// bindLR (>>=) :: Either a ->
// (a -> Either b) -> Either b
const bindLR = lr =>
// Bind operator for the Either option type.
// If lr has a Left value then lr unchanged,
// otherwise the function mf applied to the
// Right value in lr.
mf => "Left" in lr
? lr
: mf(lr.Right);
// bindList (>>=) :: [a] -> (a -> [b]) -> [b]
const bindList = xs =>
// The bind operator for Arrays.
mf => [...xs].flatMap(mf);
// bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
const bindMay = mb =>
// Nothing if mb is Nothing, or the application of the
// (a -> Maybe b) function mf to the contents of mb.
mf => mb.Nothing
? mb
: mf(mb.Just);
// bindTuple (>>=) :: Monoid a => (a, a) -> (a -> (a, b)) -> (a, b)
const bindTuple = ([a, b]) =>
// The bind operator for Tuples
f => first(mappend(a))(
f(b)
);
// bool :: a -> a -> Bool -> a
const bool = f =>
// t if p(x) else f.
t => p => p ? t : f;
// both :: (a -> b) -> (a, a) -> (b, b)
const both = f =>
// A tuple obtained by separately
// applying f to each of the two
// values in the given tuple.
([a, b]) => Tuple(
f(a)
)(
f(b)
);
// breakOn :: Eq a => [a] -> [a] -> ([a], [a])
// breakOn :: String -> String -> ([Char], [Char])
const breakOn = needle =>
// A tuple of the prefix before the first match
// and the whole remainder (including the match).
haystack => {
const ns = [...needle];
const go = hs =>
isPrefixOf(ns)(hs)
? Tuple([])(hs)
: 0 === hs.length
? Tuple([])([])
: first(
v => [hs[0]].concat(v)
)(
go(hs.slice(1))
);
return go([...haystack]);
};
// breakOnAll :: String -> String -> [(String, String)]
const breakOnAll = needle =>
// Tuples breaking the string at
// all non-overlapping instances
// of the needle in the haystack.
haystack => Boolean(needle)
? haystack.split(needle)
.reduce((a, _, i, xs) =>
0 < i
? a.concat([
Tuple(
xs.slice(0, i).join(needle)
)(
needle + xs.slice(i)
.join(needle)
)
])
: a, [])
: null;
// breakOnMay :: String -> String -> Maybe (String, String)
const breakOnMay = needle =>
// Maybe (prefix before match, match + rest)
haystack => Boolean(needle)
? (() => {
const xs = haystack.split(needle);
return Just(Boolean(xs.length)
? Tuple(
xs[0]
)(
haystack.slice(xs[0].length)
)
: Tuple(haystack)(""));
})()
: Nothing();
// break_ :: (a -> Bool) -> [a] -> ([a], [a])
const break_ = p =>
// The longest prefix of xs in which
// all values return false for p,
// tupled with the rest.
xs => {
const i = xs.findIndex(p);
return -1 !== i
? Tuple(xs.slice(0, i))(
xs.slice(i)
)
: Tuple(xs)([]);
};
// bulleted :: String -> String -> String
const bulleted = strTab =>
// A copy of s in which each line is
// preceded by a whitespace indent,
// followed by a hyphen and space.
s => s.split(/[\n\r]+/u).map(
x => "" !== x
? `${strTab}- ${x}`
: x
)
.join("\n");
// cartesianProduct :: [a] -> [b] -> [[a, b]]
const cartesianProduct = xs =>
// Every tuple in the cartesian product
// of xs and ys.
ys => [...xs].flatMap(
x => [...ys].flatMap(
y => [Tuple(x)(y)]
)
);
// caseOf :: [(a -> Bool, b)] -> b -> a -> b
const caseOf = pvs =>
// List of (Predicate, value) tuples ->
// Default value -> Value to test -> Output value
otherwise => x => {
const mb = pvs.reduce(
(a, pv) => a.Nothing
? pv[0](x)
? Just(pv[1])
: a
: a,
Nothing()
);
return mb.Nothing
? otherwise
: mb.Just;
};
// catMaybes :: [Maybe a] -> [a]
const catMaybes = mbs =>
mbs.flatMap(
m => m.Nothing
? []
: [m.Just]
);
// ceiling :: Num -> Int
const ceiling = x => {
// The least integer not less than x.
const
nr = properFraction(x),
n = nr[0];
return 0 < nr[1]
? 1 + n
: n;
};
// center :: Int -> Char -> String -> String
const center = n =>
// Size of space -> filler Char ->
// String -> Centered String
c => s => {
const gap = n - s.length;
return 0 < gap
? (() => {
const
margin = c.repeat(Math.floor(gap / 2)),
dust = c.repeat(gap % 2);
return `${margin}${s}${margin}${dust}`;
})()
: s;
};
// chars :: String -> [Char]
const chars = s =>
[...s];
// chop :: ([a] -> (b, [a])) -> [a] -> [b]
const chop = f =>
// A segmentation of xs by tail recursion with a
// function which returns a (prefix, residue) tuple.
xs => {
const go = ys =>
0 < ys.length
? (() => {
const [b, bs] = f(ys);
return [b].concat(go(bs));
})()
: [];
return go([...xs]);
};
// chr :: Int -> Char
const chr = x =>
// The character at unix code-point x.
String.fromCodePoint(x);
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n => {
// xs split into sublists of length n.
// The last sublist will be short if n
// does not evenly divide the length of xs .
const go = xs => {
const chunk = xs.slice(0, n);
return 0 < chunk.length
? [chunk, ...go(xs.slice(n))]
: [];
};
return go;
};
// combinations :: Int -> [a] -> [[a]]
const combinations = n =>
// All combinations, without repetition,
// of n items drawn from xs.
xs => {
const go = (m, ys) =>
1 > m
? [[]]
: 0 === ys.length
? []
: (
(h, rest) => [
...go(m - 1, rest)
.map(t => [h, ...t]),
...go(m, rest)
]
)(
ys[0], ys.slice(1)
);
return (go)(n, xs);
};
// combine (</>) :: FilePath -> FilePath -> FilePath
const combine = fp =>
// The concatenation of two filePath segments,
// without omission or duplication of "/".
fp1 => Boolean(fp) && Boolean(fp1)
? "/" === fp1.slice(0, 1)
? fp1
: "/" === fp.slice(-1)
? fp + fp1
: `${fp}/${fp1}`
: (fp + fp1);
// compare :: a -> a -> Ordering
const compare = a =>
b => a < b
? -1
: a > b
? 1
: 0;
// compareList :: [a] -> [a] -> Ordering
const compareList = xs =>
// 0 if two lists are identical.
// -1 if xs is empty, or has a lower leftward value.
// 1 if ys is empty, or has a lower leftward value.
ys => compare(0 === xs.length)(0 === ys.length) || (
compare(xs[0])(ys[0]) || (
compareList(xs.slice(1))(ys.slice(1))
)
);
// comparing :: Ord a => (b -> a) -> b -> b -> Ordering
const comparing = f =>
// The ordering of f(x) and f(y) as a value
// drawn from {-1, 0, 1}, representing {LT, EQ, GT}.
x => y => {
const
a = f(x),
b = f(y);
return a < b
? -1
: a > b
? 1
: 0;
};
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
// composeList :: [(a -> a)] -> (a -> a)
const composeList = fs =>
fs.reduce(
(f, g) => x => f(g(x)),
x => x
);
// composeListR :: [(a -> a)] -> (a -> a)
const composeListR = fs =>
x => fs.reduce((a, f) => f(a), x);
// composeR (>>>) :: (a -> b) -> (b -> c) -> a -> c
const composeR = f =>
g => x => f(g(x));
// concat :: [[a]] -> [a]
const concat = xs =>
// The concatenation of all the lists
// in a list of lists.
xs.flat(1);
// concatGen :: Gen [[a]] -> Gen [a]
const concatGen = gen =>
// A flattened stream of generator values;
(function* (g) {
let m = g.next();
while (!m.done) {
const xs = lazyList(m.value);
let x = xs.next();
while (!x.done) {
yield x.value;
x = xs.next();
}
m = g.next();
}
}(gen));
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f =>
// Concatenated results of a map of f over xs.
// f is any function which returns a list value.
// Any empty lists returned are filtered out by
// the concatenation.
xs => xs.flatMap(f);
// concats :: [String] -> String
const concats = xs =>
xs.join("");
// cons :: a -> [a] -> [a]
const cons = x =>
// A list constructed from the item x,
// followed by the existing list xs.
xs => Array.isArray(xs)
? [x].concat(xs)
: "GeneratorFunction" !== (
xs.constructor.constructor.name
)
? x + xs
: (function *() {
yield x;
let nxt = xs.next();
while (!nxt.done) {
yield nxt.value;
nxt = xs.next();
}
}());
// constant :: a -> b -> a
const constant = k =>
() => k;
// copyFileLR :: FilePath -> FilePath -> Either String IO ()
const copyFileLR = fpFrom =>
fpTo => {
const fpTargetFolder = takeDirectory(fpTo);
return doesFileExist(fpFrom)
? doesDirectoryExist(fpTargetFolder)
? (() => {
const
e = $(),
fpTarget = $(fpTo).stringByStandardizingPath;
return (
$.NSFileManager.defaultManager
.copyItemAtPathToPathError(
$(fpFrom).stringByStandardizingPath,
fpTarget,
e
)
? Right(ObjC.unwrap(fpTarget))
: Left(ObjC.unwrap(e.localizedDescription))
);
})()
: Left(`Target folder not found: ${fpTargetFolder}`)
: Left(`Source file not found: ${fpFrom}`);
};
// createDirectoryIfMissingLR :: Bool -> FilePath
// -> Either String FilePath
const createDirectoryIfMissingLR = blnParents =>
dirPath => {
const fp = filePath(dirPath);
return doesPathExist(fp)
? Right(fp)
: (() => {
const
e = $(),
blnOK = $.NSFileManager
.defaultManager[
"createDirectoryAtPath" + (
"WithIntermediateDirectories"
) + "AttributesError"
](fp, blnParents, void 0, e);
return blnOK
? Right(fp)
: Left(e.localizedDescription);
})();
};
// curry :: ((a, b) -> c) -> a -> b -> c
const curry = f =>
a => b => 1 < f.length
? f(a, b)
: f(Tuple(a)(b));
// curryN :: Curry a b => a -> b
const curryN = f =>
// A curried function derived from a
// function over a tuple of any order.
(...args) => {
const
go = xs => f.length <= xs.length
? f(...xs)
: (...ys) => go(xs.concat(ys));
return go(args);
};
// cycle :: [a] -> Generator [a]
const cycle = function* (xs) {
// An infinite repetition of xs,
// from which a prefix of arbitrary
// length may be drawn.
const n = xs.length;
let i = 0;
while (true) {
yield xs[i];