-
Notifications
You must be signed in to change notification settings - Fork 0
/
asPrelude.applescript
6961 lines (6327 loc) · 174 KB
/
asPrelude.applescript
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
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
/* eslint-disable max-lines-per-function */
/* eslint-disable no-undef */
/* eslint-disable strict */
/* eslint-disable no-unused-vars */
-- Just :: a -> Maybe a
on Just(x)
-- Constructor for an inhabited Maybe (option type) value.
-- Wrapper containing the result of a computation.
{type:"Maybe", Nothing:false, Just:x}
end Just
-- Left :: a -> Either a b
on |Left|(x)
{type:"Either", |Left|:x, |Right|:missing value}
end |Left|
-- Node :: a -> [Tree a] -> Tree a
on Node(v, xs)
{type:"Node", root:v, nest:xs}
end Node
-- Nothing :: Maybe a
on Nothing()
-- Constructor for an empty Maybe (option type) value.
-- Empty wrapper returned where a computation is not possible.
{type: "Maybe", Nothing: true}
end Nothing
-- Right :: b -> Either a b
on |Right|(x)
{type:"Either", |Left|:missing value, |Right|:x}
end |Right|
-- Tuple (,) :: a -> b -> (a, b)
on Tuple(a, b)
-- Constructor for a pair of values, possibly of two different types.
{type:"Tuple", |1|:a, |2|:b, length:2}
end Tuple
-- Tuple3 (,,) :: a -> b -> c -> (a, b, c)
on Tuple3(x, y, z)
{type:"Tuple3", |1|:x, |2|:y, |3|:z, length:3}
end Tuple3
-- Requires N arguments to be wrapped as one list in AS
-- (the JS version accepts N separate arguments)
-- TupleN :: a -> b ... -> (a, b ... )
on TupleN(argv)
tupleFromList(argv)
end TupleN
-- abs :: Num -> Num
on abs(x)
-- Absolute value.
if 0 > x then
-x
else
x
end if
end abs
-- add (+) :: Num a => a -> a -> a
on add(a)
-- Curried addition.
script
on |λ|(b)
a + b
end |λ|
end script
end add
-- all :: (a -> Bool) -> [a] -> Bool
on all(p, xs)
-- True if p holds for every value in xs
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if not |λ|(item i of xs, i, xs) then return false
end repeat
true
end tell
end all
-- allSame :: [a] -> Bool
on allSame(xs)
if 2 > length of xs then
true
else
script p
property h : item 1 of xs
on |λ|(x)
h = x
end |λ|
end script
all(p, rest of xs)
end if
end allSame
-- allTree :: (a -> Bool) -> Tree a -> Bool
on allTree(p, tree)
-- True if p holds for the value of every node in tree
script go
property mp : mReturn(p)'s |λ|
on |λ|(oNode)
if mp(root of oNode) then
repeat with v in nest of oNode
if not (contents of |λ|(v)) then return false
end repeat
true
else
false
end if
end |λ|
end script
|λ|(tree) of go
end allTree
-- and :: [Bool] -> Bool
on |and|(xs)
-- True if every value in the list is true.
repeat with x in xs
if not (contents of x) then return false
end repeat
return true
end |and|
-- any :: (a -> Bool) -> [a] -> Bool
on any(p, xs)
-- Applied to a predicate and a list,
-- |any| returns true if at least one element of the
-- list satisfies the predicate.
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then return true
end repeat
false
end tell
end any
-- anyTree :: (a -> Bool) -> Tree a -> Bool
on anyTree(p, tree)
-- True if p holds for the value of any node in the tree.
script go
property mp : mReturn(p)'s |λ|
on |λ|(oNode)
if mp(root of oNode) then
true
else
repeat with v in nest of oNode
if contents of |λ|(v) then return true
end repeat
false
end if
end |λ|
end script
|λ|(tree) of go
end anyTree
-- ap (<*>) :: Monad m => m (a -> b) -> m a -> m b
on ap(mf, mx)
-- 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), etc
if class of mx is list then
apList(mf, mx)
else
set t to typeName(mf)
if "(a -> b)" = t then
apFn(mf, mx)
else if "Either" = t then
apLR(mf, mx)
else if "Maybe" = t then
apMay(mf, mx)
else if "Node" = t then
apTree(mf, mx)
else if "Tuple" = t then
apTuple(mf, mx)
else
missing value
end if
end if
end ap
-- apFn :: (a -> b -> c) -> (a -> b) -> (a -> c)
on apFn(f, g)
script go
property mf : |λ| of mReturn(f)
property mg : |λ| of mReturn(g)
on |λ|(x)
mf(x, mg(x))
end |λ|
end script
end apFn
-- apLR (<*>) :: Either e (a -> b) -> Either e a -> Either e b
on apLR(flr, lr)
if missing value is |Left| of flr then
if missing value is |Left| of lr then
|Right|(|λ|(|Right| of lr) of mReturn(|Right| of flr))
else
lr
end if
else
flr
end if
end apLR
-- apList (<*>) :: [(a -> b)] -> [a] -> [b]
on apList(fs, xs)
-- e.g. [(*2),(/2), sqrt] <*> [1,2,3]
-- --> ap([dbl, hlf, root], [1, 2, 3])
-- --> [2,4,6,0.5,1,1.5,1,1.4142135623730951,1.7320508075688772]
-- Each member of a list of functions applied to
-- each of a list of arguments, deriving a list of new values
set lst to {}
repeat with f in fs
tell mReturn(contents of f)
repeat with x in xs
set end of lst to |λ|(contents of x)
end repeat
end tell
end repeat
return lst
end apList
-- apMay (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b
on apMay(mf, mx)
-- Maybe f applied to Maybe x, deriving a Maybe y
if Nothing of mf or Nothing of mx then
Nothing()
else
Just(|λ|(Just of mx) of mReturn(Just of mf))
end if
end apMay
-- apTree (<*>) :: Tree (a -> b) -> Tree a -> Tree b
on apTree(tf, tx)
set fmap to curry(my fmapTree)
script go
on |λ|(t)
set f to root of t
Node(mReturn(f)'s |λ|(root of tx), ¬
map(fmap's |λ|(f), nest of tx) & ¬
map(go, nest of t))
end |λ|
end script
return go's |λ|(tf)
end apTree
-- apTuple (<*>) :: Monoid m => (m, (a -> b)) -> (m, a) -> (m, b)
on apTuple(tf, tx)
Tuple(mappend(|1| of tf, |1| of tx), |λ|(|2| of tx) of mReturn(|2| of tf))
end apTuple
-- append (<>) :: [a] -> [a] -> [a]
-- append (<>) :: String -> String -> String
on append(xs, ys)
-- Append two lists.
xs & ys
end append
-- appendFile :: FilePath -> String -> IO Bool
on appendFile(strPath, txt)
-- Write a string to the end of a file.
-- Returns true if the path exists
-- and the write succeeded.
-- Otherwise returns false.
set ca to current application
set oFullPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
set {blnExists, intFolder} to (ca's NSFileManager's defaultManager()'s ¬
fileExistsAtPath:oFullPath isDirectory:(reference))
if blnExists then
if 0 = intFolder then
set oData to (ca's NSString's stringWithString:txt)'s ¬
dataUsingEncoding:(ca's NSUTF8StringEncoding)
set h to ca's NSFileHandle's fileHandleForWritingAtPath:oFullPath
h's seekToEndOfFile
h's writeData:oData
h's closeFile()
true
else
-- text appended to folder is undefined
false
end if
else
if doesDirectoryExist(takeDirectory(oFullPath as string)) then
writeFile(oFullPath, txt)
true
else
false
end if
end if
end appendFile
-- appendFileMay :: FilePath -> String -> Maybe IO FilePath
on appendFileMay(strPath, txt)
-- Write a string to the end of a file.
-- Returns a Just FilePath value if the
-- path exists and the write succeeded.
-- Otherwise returns Nothing.
set ca to current application
set oFullPath to (ca's NSString's stringWithString:strPath)'s ¬
stringByStandardizingPath
set strFullPath to oFullPath as string
set {blnExists, intFolder} to (ca's NSFileManager's defaultManager()'s ¬
fileExistsAtPath:oFullPath isDirectory:(reference))
if blnExists then
if 0 = intFolder then -- Not a directory
set oData to (ca's NSString's stringWithString:txt)'s ¬
dataUsingEncoding:(ca's NSUTF8StringEncoding)
set h to ca's NSFileHandle's fileHandleForWritingAtPath:oFullPath
h's seekToEndOfFile
h's writeData:oData
h's closeFile()
Just(strFullPath)
else
Nothing()
end if
else
if doesDirectoryExist(takeDirectory(strFullPath)) then
writeFile(oFullPath, txt)
Just(strFullPath)
else
Nothing()
end if
end if
end appendFileMay
-- appendGen (++) :: Gen [a] -> Gen [a] -> Gen [a]
on appendGen(xs, ys)
script
property vs : xs
on |λ|()
set v to |λ|() of vs
if missing value is not v then
v
else
set vs to ys
|λ|() of ys
end if
end |λ|
end script
end appendGen
-- apply ($) :: (a -> b) -> a -> b
on apply(f, x)
mReturn(f)'s |λ|(x)
end apply
-- applyN :: Int -> (a -> a) -> a -> a
on applyN(n, f, x)
script go
on |λ|(a, g)
|λ|(a) of mReturn(g)
end |λ|
end script
foldl(go, x, replicate(n, f))
end applyN
-- approxRatio :: Float -> Float -> Ratio
on approxRatio(epsilon, n)
if {real, integer} contains (class of epsilon) and 0 < epsilon then
set e to epsilon
else
set e to 1 / 10000
end if
script gcde
on |λ|(e, x, y)
script _gcd
on |λ|(a, b)
if b < e then
a
else
|λ|(b, a mod b)
end if
end |λ|
end script
|λ|(abs(x), abs(y)) of _gcd
end |λ|
end script
set c to |λ|(e, 1, n) of gcde
Ratio((n div c), (1 div c))
end approxRatio
-- argvLength :: Function -> Int
on argvLength(h)
try
mReturn(h)'s |λ|()
0
on error errMsg
set {dlm, my text item delimiters} to {my text item delimiters, ","}
set xs to text items of errMsg
set my text item delimiters to dlm
length of xs
end try
end argvLength
-- assocs :: Map k a -> [(k, a)]
on assocs(m)
script go
on |λ|(k)
set mb to lookupDict(k, m)
if true = |Nothing| of mb then
{}
else
{{k, |Just| of mb}}
end if
end |λ|
end script
concatMap(go, keys(m))
end assocs
-- base64decode :: String -> String
on base64decode(s)
tell current application
set encoding to its NSUTF8StringEncoding
set ignore to its NSDataBase64DecodingIgnoreUnknownCharacters
(((alloc() of its NSString)'s initWithData:((its (NSData's alloc()'s ¬
initWithBase64EncodedString:s ¬
options:(ignore)))) encoding:encoding)) as text
end tell
end base64decode
-- base64encode :: String -> String
on base64encode(s)
tell current application
set encodingOption to its NSUTF8StringEncoding
base64EncodedStringWithOptions_(0) of ¬
dataUsingEncoding_(encodingOption) of ¬
(stringWithString_(s) of its NSString) as string
end tell
end base64encode
-- bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
on bimap(f, g)
-- Tuple instance of bimap.
-- A tuple of the application of f and g to the
-- first and second values of tpl respectively.
script
on |λ|(x)
{|λ|(fst(x)) of mReturn(f), ¬
|λ|(snd(x)) of mReturn(g)}
end |λ|
end script
end bimap
-- bimapLR :: (a -> b) -> (c -> d) -> ֵEither ֵֵa c -> Either b d
on bimapLR(f, g)
script go
on |λ|(e)
if missing value is |Left| of e then
tell mReturn(g) to |Right|(|λ|(|Right| of e))
else
tell mReturn(f) to |Left|(|λ|(|Left| of e))
end if
end |λ|
end script
end bimapLR
-- bimapN :: (a -> b) -> (c -> d) -> TupleN -> TupleN
on bimapN(f, g, tplN)
set z to length of tplN
set k1 to (z - 1) as string
set k2 to z as string
insertDict(k2, mReturn(g)'s |λ|(Just of lookupDict(k2, tplN)), ¬
insertDict(k1, mReturn(f)'s |λ|(Just of lookupDict(k1, tplN)), tplN))
end bimapN
-- bind (>>=) :: Monad m => m a -> (a -> m b) -> m b
on bind(m, mf)
set c to class of m
if list = c then
bindList(m, mf)
else if record = c then
set ks to keys(m)
if ks contains "type" then
set t to type of m
if "Maybe" = t then
bindMay(m, mf)
else if "Either" = t then
bindLR(m, mf)
else if "Tuple" = t then
bindTuple(m, mf)
else
missing value
end if
else
missing value
end if
else if handler is c or script is c then
bindFn(m, mf)
else
missing value
end if
end bind
-- bindFn (>>=) :: (a -> b) -> (b -> a -> c) -> a -> c
on bindFn(f, bop)
-- Where either bop or f is a binary operator.
script
property mf : mReturn(f)
property mop : mReturn(bop)
on |λ|(x)
try
curry(mop)'s |λ|(mf's |λ|(x))'s |λ|(x)
on error
mop's |λ|(curry(mf)'s |λ|(x))'s |λ|(x)
end try
end |λ|
end script
end bindFn
-- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
on bindLR(m, mf)
if missing value is not |Left| of m then
m
else
mReturn(mf)'s |λ|(|Right| of m)
end if
end bindLR
-- bindList (>>=) :: [a] -> (a -> [b]) -> [b]
on bindList(xs, f)
set acc to {}
tell mReturn(f)
repeat with x in xs
set acc to acc & |λ|(contents of x)
end repeat
end tell
return acc
end bindList
-- bindMay (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b
on bindMay(mb, mf)
-- bindMay provides the mechanism for composing a
-- sequence of (a -> Maybe b) functions.
-- If m is Nothing, it is passed straight through.
-- If m is Just(x), the result is an application
-- of the (a -> Maybe b) function (mf) to x.
if Nothing of mb then
mb
else
tell mReturn(mf) to |λ|(Just of mb)
end if
end bindMay
-- bindTuple (>>=) :: Monoid a => (a, a) -> (a -> (a, b)) -> (a, b)
on bindTuple(tpl, f)
set t2 to mReturn(f)'s |λ|(|2| of tpl)
Tuple(mappend(|1| of tpl, |1| of t2), |2| of t2)
end bindTuple
-- bool :: a -> a -> Bool -> a
on bool(ff, tf)
-- The evaluation of either tf or ff,
-- depending on a boolean value.
script
on |λ|(bln)
if bln then
set e to tf
else
set e to ff
end if
set c to class of e
if {script, handler} contains c then
|λ|() of mReturn(e)
else
e
end if
end |λ|
end script
end bool
-- break :: (a -> Bool) -> [a] -> ([a], [a])
on break(p, xs)
set bln to false
tell mReturn(p)
set lng to length of xs
repeat with i from 1 to lng
if |λ|(item i of xs) then
set bln to true
exit repeat
end if
end repeat
end tell
if bln then
if 1 < i then
{items 1 thru (i - 1) of xs, items i thru -1 of xs}
else
{{}, xs}
end if
else
{xs, {}}
end if
end break
-- breakOn :: String -> String -> (String, String)
on breakOn(pat, src)
-- non null needle -> haystack -> (prefix before match, match + rest)
if pat ≠ "" then
set {dlm, my text item delimiters} to {my text item delimiters, pat}
set lstParts to text items of src
set lngParts to length of lstParts
if 1 < lngParts then
set tpl to {item 1 of lstParts, pat & ¬
((items 2 thru -1 of lstParts) as text)}
else
set tpl to Tuple(src, "")
end if
set my text item delimiters to dlm
return tpl
else
missing value
end if
end breakOn
-- breakOnAll :: String -> String -> [(String, String)]
on breakOnAll(pat, src)
-- breakOnAll "/" "a/b/c/"
-- ==> [("a", "/b/c/"), ("a/b", "/c/"), ("a/b/c", "/")]
if "" ≠ pat then
script
on |λ|(a, _, i, xs)
if 1 < i then
a & {{intercalate(pat, take(i - 1, xs)), ¬
pat & intercalate(pat, drop(i - 1, xs))}}
else
a
end if
end |λ|
end script
foldl(result, {}, splitOn(pat, src))
else
missing value
end if
end breakOnAll
-- breakOnMay :: String -> String -> Maybe (String, String)
on breakOnMay(pat, src)
-- needle -> haystack -> maybe (prefix before match, match + rest)
if pat ≠ "" then
set {dlm, my text item delimiters} to {my text item delimiters, pat}
set lstParts to text items of src
if length of lstParts > 1 then
set mbTuple to Just({item 1 of lstParts, pat & ¬
((items 2 thru -1 of lstParts) as text)})
else
set mbTuple to Just({src, ""})
end if
set my text item delimiters to dlm
return mbTuple
else
Nothing()
end if
end breakOnMay
-- bulleted :: String -> String -> String
on bulleted(strIndent, s)
script go
on |λ|(x)
if "" ≠ x then
strIndent & "- " & x
else
x
end if
end |λ|
end script
unlines(map(go, paragraphs of s))
end bulleted
-- cartesianProduct :: [a] -> [b] -> [[a, b]]
on cartesianProduct(xs, ys)
script
on |λ|(x)
script
on |λ|(y)
{x, y}
end |λ|
end script
concatMap(result, ys)
end |λ|
end script
concatMap(result, xs)
end cartesianProduct
-- caseOf :: [(a -> Bool, b)] -> b -> a -> b
on caseOf (pvs, otherwise, x)
-- List of (Predicate, value) tuples -> Default value -> Value to test -> Output value
repeat with tpl in pvs
if mReturn(|1| of tpl)'s |λ|(x) then return |2| of tpl
end repeat
return otherwise
end caseOf
-- catMaybes :: [Maybe a] -> [a]
on catMaybes(mbs)
script emptyOrListed
on |λ|(m)
if Nothing of m then
{}
else
{Just of m}
end if
end |λ|
end script
concatMap(emptyOrListed, mbs)
end catMaybes
-- ceiling :: Num -> Int
on ceiling(x)
set nr to properFraction(x)
set n to |1| of nr
if 0 < (|2| of nr) then
n + 1
else
n
end if
end ceiling
-- center :: Int -> Char -> String -> String
on |center|(n, cFiller, strText)
set lngFill to n - (length of strText)
if lngFill > 0 then
set strPad to replicate(lngFill div 2, cFiller) as text
set strCenter to strPad & strText & strPad
if lngFill mod 2 > 0 then
cFiller & strCenter
else
strCenter
end if
else
strText
end if
end |center|
-- chars :: String -> [Char]
on chars(s)
characters of s
end chars
-- chop :: ([a] -> (b, [a])) -> [a] -> [b]
on chop(f, xs)
script go
property g : mReturn(f)
on |λ|(xs)
if 0 < length of xs then
set {b, ys} to g's |λ|(xs)
{b} & |λ|(ys)
else
{}
end if
end |λ|
end script
go's |λ|(xs)
end chop
-- chr :: Int -> Char
on chr(n)
character id n
end chr
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(k, xs)
script
on go(ys)
set ab to splitAt(k, ys)
set a to item 1 of ab
if {} ≠ a then
{a} & go(item 2 of ab)
else
a
end if
end go
end script
result's go(xs)
end chunksOf
-- combine (</>) :: FilePath -> FilePath -> FilePath
on combine(fp, fp1)
-- The concatenation of two filePath segments,
-- without omission or duplication of "/".
if "" = fp or "" = fp1 then
fp & fp1
else if "/" = item 1 of fp1 then
fp1
else if "/" = item -1 of fp then
fp & fp1
else
fp & "/" & fp1
end if
end combine
-- compare :: a -> a -> Ordering
on compare(a, b)
if a < b then
-1
else if a > b then
1
else
0
end if
end compare
-- comparing :: (a -> b) -> (a -> a -> Ordering)
on comparing(f)
script
on |λ|(a, b)
tell mReturn(f)
set fa to |λ|(a)
set fb to |λ|(b)
if fa < fb then
-1
else if fa > fb then
1
else
0
end if
end tell
end |λ|
end script
end comparing
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
on compose(f, g)
script
property mf : mReturn(f)
property mg : mReturn(g)
on |λ|(x)
mf's |λ|(mg's |λ|(x))
end |λ|
end script
end compose
-- composeList :: [(a -> a)] -> (a -> a)
on composeList(fs)
script
on |λ|(x)
script go
on |λ|(f, a)
mReturn(f)'s |λ|(a)
end |λ|
end script
foldr(go, x, fs)
end |λ|
end script
end composeList
-- composeListR :: [(a -> a)] -> (a -> a)
on composeListR(fs)
script
on |λ|(x)
script go
on |λ|(a, f)
mReturn(f)'s |λ|(a)
end |λ|
end script
foldl(go, x, fs)
end |λ|
end script
end composeListLR
-- composeR (>>>) :: (a -> b) -> (b -> c) -> a -> c
on composeR(f, g)
script
on |λ|(x)
|λ|(|λ|(x) of mReturn(f)) of mReturn(g)
end |λ|
end script
end composeR
-- concat :: [[a]] -> [a]
on concat(xs)
((current application's NSArray's arrayWithArray:xs)'s ¬
valueForKeyPath:"@unionOfArrays.self") as list
end concat
-- concatMap :: (a -> [b]) -> [a] -> [b]on concatMap(f, xs) set lng to length of xs set acc to {} tell mReturn(f) repeat with i from 1 to lng set acc to acc & (|λ|(item i of xs, i, xs)) end repeat end tell accend concatMap
-- cons :: a -> [a] -> [a]
on cons(x, xs)
set c to class of xs
if list is c then
{x} & xs
else if script is c then
script
property pRead : false
on |λ|()
if pRead then
|λ|() of xs
else
set pRead to true
return x
end if
end |λ|
end script
else
x & xs
end if
end cons
-- constant :: a -> b -> a
on |constant|(k)
script
on |λ|(_)
k
end |λ|
end script
end |constant|
-- createDirectoryIfMissingLR :: Bool -> FilePath -> Either String FilePath
on createDirectoryIfMissingLR(blnParents, fp)
if doesPathExist(fp) then
|Right|(fp)
else
set e to reference
set ca to current application
set oPath to (ca's NSString's stringWithString:(fp))'s ¬
stringByStandardizingPath
set {blnOK, e} to ca's NSFileManager's ¬
defaultManager's createDirectoryAtPath:(oPath) ¬
withIntermediateDirectories:(blnParents) ¬
attributes:(missing value) |error|:(e)
if blnOK then
|Right|(fp)
else
|Left|((localizedDescription of e) as string)
end if
end if
end createDirectoryIfMissingLR
-- curry :: ((a, b) -> c) -> a -> b -> c
on curry(f)
script
on |λ|(a)
script
on |λ|(b)
|λ|(a, b) of mReturn(f)
end |λ|
end script
end |λ|
end script
end curry
-- cycle :: [a] -> Generator [a]
on cycle(xs)
script
property lng : 1 + (length of xs)
property i : missing value
on |λ|()
if missing value is i then
set i to 1
else
set nxt to (1 + i) mod lng
if 0 = ((1 + i) mod lng) then
set i to 1
else
set i to nxt
end if
end if
return item i of xs
end |λ|
end script
end cycle
-- decodedPath :: Percent Encoded String -> FilePath
on decodedPath(fp)
-- use framework "Foundation"
tell current application to ¬
(stringByRemovingPercentEncoding ¬
of stringWithString_(fp) ¬
of its NSString) as string
end decodedPath
-- degrees :: Float x => Radians x -> Degrees x
on degrees(r)
(180 / pi) * r
end degrees
-- delete :: Eq a => a -> [a] -> [a]
on |delete|(x, xs)
set mbIndex to elemIndex(x, xs)