-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathGeneral.fs
More file actions
1950 lines (1560 loc) · 81.2 KB
/
General.fs
File metadata and controls
1950 lines (1560 loc) · 81.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace FSharpPlus.Tests
#nowarn "686"
open System
open System.Collections.ObjectModel
open FSharpPlus
open FSharpPlus.Data
open FSharpPlus.Control
open NUnit.Framework
open Helpers
open FSharpPlus.Math.Applicative
open CSharpLib
type WrappedMapA<'K,'V when 'K : comparison> = WrappedMapA of Map<'K,'V> with
static member ToMap (WrappedMapA m) = m
static member inline TraverseIndexed (WrappedMapA m, f) =
SideEffects.add "Using WrappedMapA's TraverseIndexed"
WrappedMapA <!> (traversei f m : ^__)
module WrappedMapA=
let inline ofList l = Map.ofList l |> WrappedMapA
type WrappedListA<'s> = WrappedListA of 's list with
static member ToSeq (WrappedListA lst) = SideEffects.add "Using WrappedListA's ToSeq"; List.toSeq lst
static member OfSeq lst = WrappedListA (Seq.toList lst)
static member TryItem (i, WrappedListA x) = List.tryItem i x
static member TryParse x =
if x = "[1;2;3]" then Some (WrappedListA [1;2;3])
else None
static member Exists (x, f) =
SideEffects.add "Using WrappedListA's Exists"
let (WrappedListA lst) = x
List.exists f lst
static member Pick (x, f) =
SideEffects.add "Using WrappedListA's Pick"
let (WrappedListA lst) = x
List.pick f lst
static member Min x =
SideEffects.add "Using WrappedListA's Min"
let (WrappedListA lst) = x
List.min lst
static member MaxBy (x, f) =
SideEffects.add "Using WrappedListA's MaxBy"
let (WrappedListA lst) = x
List.maxBy f lst
member this.Length =
SideEffects.add "Using WrappedListA's Length"
let (WrappedListA lst) = this
List.length lst
type WrappedListB<'s> = WrappedListB of 's list with
static member Return x = WrappedListB [x]
static member (+) (WrappedListB l, WrappedListB x) = WrappedListB (l @ x)
static member Zero = WrappedListB List.empty
static member ToSeq (WrappedListB lst) = List.toSeq lst
static member FoldBack (WrappedListB x, f, z) = List.foldBack f x z
type WrappedListB'<'s> = WrappedListB' of 's list with // Same as B but without clean signatures
static member Return (_:WrappedListB'<'a>, _:Return ) = fun (x:'a) -> WrappedListB' [x]
static member (+) (WrappedListB' l, WrappedListB' x) = WrappedListB' (l @ x)
static member Zero (_:WrappedListB'<'a>, _:Zero) = WrappedListB' List.empty
static member ToSeq (WrappedListB' lst) = List.toSeq lst
static member FoldBack (WrappedListB' x, f, z) = List.foldBack f x z
type WrappedListC<'s> = WrappedListC of 's list with
static member (+) (WrappedListC l, WrappedListC x) = WrappedListC (l @ x)
static member Zero = WrappedListC List.empty
static member Sum (lst: seq<WrappedListC<_>>) = Seq.head lst
type WrappedListD<'s> = WrappedListD of 's list with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedListD x) = x in x :> _ seq).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedListD x) = x in x :> _ seq).GetEnumerator () :> Collections.IEnumerator
static member Return (x) = SideEffects.add "Using WrappedListD's Return"; WrappedListD [x]
static member (>>=) ((WrappedListD x):WrappedListD<'T>, f) = SideEffects.add "Using WrappedListD's Bind"; WrappedListD (List.collect (f >> (fun (WrappedListD x) -> x)) x)
static member inline FoldMap (WrappedListD x, f) =
SideEffects.add "Using optimized foldMap"
Seq.fold (fun x y -> x ++ (f y)) zero x
static member Zip (WrappedListD x, WrappedListD y) = SideEffects.add "Using WrappedListD's zip"; WrappedListD (List.zip x y)
static member Exists (x, f) =
SideEffects.add "Using WrappedListD's Exists"
let (WrappedListD lst) = x
List.exists f lst
static member Pick (x, f) =
SideEffects.add "Using WrappedListD's Pick"
let (WrappedListD lst) = x
List.pick f lst
static member Min x =
SideEffects.add "Using WrappedListD's Min"
let (WrappedListD lst) = x
List.min lst
static member MaxBy (x, f) =
SideEffects.add "Using WrappedListD's MaxBy"
let (WrappedListD lst) = x
List.maxBy f lst
static member MapIndexed (WrappedListD x, f) =
SideEffects.add "Using WrappedListD's MapIndexed"
WrappedListD (List.mapi f x)
static member ChooseIndexed (WrappedListD x, f) =
SideEffects.add "Using WrappedListD's ChooseIndexed"
WrappedListD (List.choosei f x)
static member Lift3 (f, WrappedListD x, WrappedListD y, WrappedListD z) =
SideEffects.add "Using WrappedListD's Lift3"
WrappedListD (List.lift3 f x y z)
static member IterateIndexed (WrappedListD x, f) =
SideEffects.add "Using WrappedListD's IterateIndexed"
List.iteri f x
static member inline FoldIndexed (WrappedListD x, f, z) =
SideEffects.add "Using WrappedListD's FoldIndexed"
foldi f z x
static member inline TraverseIndexed (WrappedListD x, f) =
SideEffects.add "Using WrappedListD's TraverseIndexed"
WrappedListD <!> (traversei f x : ^r)
static member FindIndex (WrappedListD x, y) =
SideEffects.add "Using WrappedListD's FindIndex"
findIndex y x
static member FindSliceIndex (WrappedListD x, WrappedListD y) =
SideEffects.add "Using WrappedListD's FindSliceIndex"
findSliceIndex y x
static member FindLastSliceIndex (WrappedListD x, WrappedListD y) =
SideEffects.add "Using WrappedListD's FindLastSliceIndex"
findLastSliceIndex y x
member this.Length =
SideEffects.add "Using WrappedListD's Length"
let (WrappedListD lst) = this
List.length lst
type WrappedListE<'s> = WrappedListE of 's list with
static member Return x = WrappedListE [x]
static member (>>=) (WrappedListE x: WrappedListE<'T>, f) = WrappedListE (List.collect (f >> (fun (WrappedListE x) -> x)) x)
static member get_Empty () = WrappedListE List.empty
static member (<|>) (WrappedListE l, WrappedListE x) = WrappedListE (l @ x)
type WrappedListF<'s> = WrappedListF of 's list with
static member Return x = WrappedListF [x]
static member (>>=) (WrappedListF x: WrappedListF<'T>, f) = WrappedListF (List.collect (f >> (fun (WrappedListF x) -> x)) x)
static member Join (WrappedListF wlst) = SideEffects.add "Join"; WrappedListF wlst >>= id
static member get_Empty () = WrappedListF List.empty
static member (<|>) (WrappedListF l, WrappedListF x) = WrappedListF (l @ x)
type WrappedListG<'s> = WrappedListG of 's list with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedListG x) = x in x :> _ seq).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedListG x) = x in x :> _ seq).GetEnumerator () :> Collections.IEnumerator
static member Return x = WrappedListG [x]
static member (>>=) (WrappedListG x: WrappedListG<'T>, f) = WrappedListG (List.collect (f >> (fun (WrappedListG x) -> x)) x)
static member Join (WrappedListG wlst) = (*SideEffects.add "Join";*) WrappedListG wlst >>= id
static member get_Empty () = WrappedListG List.empty
static member (<|>) (WrappedListG l, WrappedListG x) = WrappedListG (l @ x)
static member Delay (f: unit -> WrappedListG<_>) = SideEffects.add "Using WrappedListG's Delay"; f ()
static member Using (resource, body) = SideEffects.add "Using WrappedListG's Using"; using resource body
type WrappedListH<'s> = WrappedListH of 's list with
static member Map (WrappedListH lst, f) = WrappedListH (List.map f lst)
static member inline Sequence (x: WrappedListH<'``Functor<'T>``>) =
let (WrappedListH lst) = x
let s = sequence lst : '``Functor<List<'T>>``
map WrappedListH s : '``Functor<WrappedListH<'T>>``
type WrappedListI<'s> = WrappedListI of 's list with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedListI x) = x in x :> _ seq).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedListI x) = x in x :> _ seq).GetEnumerator () :> Collections.IEnumerator
static member Return (x) = SideEffects.add "Using WrappedListI's Return"; WrappedListI [x]
static member Sum (lst: seq<WrappedListI<_>>) = Seq.head lst
type WrappedSeqA<'s> = WrappedSeqA of 's seq with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedSeqA x) = x in x).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedSeqA x) = x in x).GetEnumerator () :> Collections.IEnumerator
static member Return x = WrappedSeqA [x]
static member (>>=) (WrappedSeqA x: WrappedSeqA<'T>, f) = WrappedSeqA (Seq.collect (f >> (fun (WrappedSeqA x) -> x)) x)
static member Join (WrappedSeqA wlst) = WrappedSeqA wlst >>= id
static member get_Empty () = WrappedSeqA List.empty
static member (<|>) (WrappedSeqA l, WrappedSeqA x) = WrappedSeqA (Seq.append l x)
static member Delay (f: unit -> WrappedSeqA<_>) =
let run (WrappedSeqA s) = s
WrappedSeqA (Seq.delay (f >> run))
type WrappedSeqB<'s> = WrappedSeqB of 's seq with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedSeqB x) = x in x).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedSeqB x) = x in x).GetEnumerator () :> Collections.IEnumerator
static member Return x = WrappedSeqB [x]
static member (>>=) (WrappedSeqB x: WrappedSeqB<'T>, f) = WrappedSeqB (Seq.collect (f >> (fun (WrappedSeqB x) -> x)) x)
static member Join (WrappedSeqB wlst) = WrappedSeqB wlst >>= id
static member get_Empty () = WrappedSeqB List.empty
static member (<|>) (WrappedSeqB l, WrappedSeqB x) = WrappedSeqB (Seq.append l x)
static member Delay (f: unit -> WrappedSeqB<_>) =
let run (WrappedSeqB s) = s
WrappedSeqB (Seq.delay (f >> run))
static member TryFinally (computation, compensation) =
SideEffects.add "Using WrappedSeqA's TryFinally"
try computation finally compensation ()
static member Using (resource, body) =
SideEffects.add "Using WrappedSeqB's Using"
using resource body
type WrappedSeqC<'s> = WrappedSeqC of 's seq with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedSeqC x) = x in x).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedSeqC x) = x in x).GetEnumerator () :> Collections.IEnumerator
static member Return x = WrappedSeqC [x]
static member (>>=) (WrappedSeqC x: WrappedSeqC<'T>, f) = WrappedSeqC (Seq.collect (f >> (fun (WrappedSeqC x) -> x)) x)
static member Join (WrappedSeqC wlst) = WrappedSeqC wlst >>= id
static member get_Empty () = WrappedSeqC List.empty
static member (<|>) (WrappedSeqC l, WrappedSeqC x) = WrappedSeqC (Seq.append l x)
static member Delay (f: unit -> WrappedSeqC<_>) =
let run (WrappedSeqC s) = s
WrappedSeqC (Seq.delay (f >> run))
static member TryFinally (computation, compensation) =
SideEffects.add "Using WrappedSeqC's TryFinally"
try computation finally compensation ()
type WrappedSeqD<'s> = WrappedSeqD of 's seq with
static member Return x = SideEffects.add "Using WrappedSeqD's Return"; WrappedSeqD (Seq.singleton x)
static member (<*>) (WrappedSeqD f, WrappedSeqD x) = SideEffects.add "Using WrappedSeqD's Apply"; WrappedSeqD (f <*> x)
static member ToList (WrappedSeqD x) = Seq.toList x
static member ChooseIndexed (WrappedSeqD x, f) =
SideEffects.add "Using WrappedSeqD's ChooseIndexed"
WrappedSeqD (Seq.choosei f x)
static member Lift3 (f, WrappedSeqD x, WrappedSeqD y, WrappedSeqD z) =
SideEffects.add "Using WrappedSeqD's Lift3"
WrappedSeqD (Seq.lift3 f x y z)
type WrappedSeqE<'s> = WrappedSeqE of 's seq with
static member Reduce (WrappedSeqE x, reduction) = SideEffects.add "Using WrappedSeqE's Reduce"; Seq.reduce reduction x
static member ToSeq (WrappedSeqE x) = SideEffects.add "Using WrappedSeqE's ToSeq"; x
type WrappedSeqF<'s> = WrappedSeqF of 's seq with
interface Collections.Generic.IEnumerable<'s> with member x.GetEnumerator () = (let (WrappedSeqF x) = x in x).GetEnumerator ()
interface Collections.IEnumerable with member x.GetEnumerator () = (let (WrappedSeqF x) = x in x).GetEnumerator () :> Collections.IEnumerator
static member Return x = SideEffects.add "Using WrappedSeqF's Return"; WrappedSeqF (Seq.singleton x)
static member (<*>) (WrappedSeqF f, WrappedSeqF x) = SideEffects.add "Using WrappedSeqF's Apply"; WrappedSeqF (f <*> x)
static member ToList (WrappedSeqF x) = Seq.toList x
type TestNonEmptyCollection<'a> = private { Singleton: 'a } with
interface NonEmptySeq<'a> with
member this.First =
SideEffects.add "Using TestNonEmptyCollection's Head"
this.Singleton
member this.GetEnumerator() =
SideEffects.add "Using TestNonEmptyCollection's GetEnumerator<>"
(Seq.singleton this.Singleton).GetEnumerator()
member this.GetEnumerator() =
SideEffects.add "Using TestNonEmptyCollection's GetEnumerator"
(Seq.singleton this.Singleton).GetEnumerator() :> System.Collections.IEnumerator
module TestNonEmptyCollection =
let Create x = { Singleton = x } :> NonEmptySeq<_>
open System.Collections.Generic
open System.Collections
open System.Threading.Tasks
type ListOnlyIndex<'s> (l: 's list) =
interface IList<'s> with
member __.Count = List.length l
member __.IsReadOnly with get () = true
member __.Item with get index = List.item index l and set _ _ = failwith "set"
member __.Add _ = failwith "Add"
member __.Clear () = failwith "Clear"
member __.Contains _ = failwith "Contains"
member __.CopyTo (_, _) = failwith "CopyTo"
member __.GetEnumerator () : IEnumerator<'s> = failwith "ListOnlyIndex.GetEnumerator"
member __.GetEnumerator () : IEnumerator = failwith "ListOnlyIndex.GetEnumerator"
member __.IndexOf _ = failwith "IndexOf"
member __.Insert (index: int, item:'s) = failwithf "Insert %i %A" index item
member __.Remove _ = failwith "Remove"
member __.RemoveAt _ = failwith "RemoveAt"
type ReadOnlyListOnlyIndex<'s> (l: 's list) =
interface IReadOnlyList<'s> with
member __.Count = List.length l
member __.Item with get index = List.item index l
member __.GetEnumerator () : IEnumerator<'s> = failwith "ReadOnlyListOnlyIndex.GetEnumerator"
member __.GetEnumerator () : IEnumerator = failwith "ReadOnlyListOnlyIndex.GetEnumerator"
module MonoidTestCompile =
open System.Collections
open System.Collections.Generic
open System.Threading.Tasks
type MyList<'t> = MyList of list<'t> with
static member get_Empty () = MyList []
static member (<|>) (MyList x, MyList y) = MyList (x @ y)
type MyNum = MyNum of int with
static member get_Empty () = MyNum 0
static member FromInt32 x = MyNum x
let testCompile =
let _res1n2 = MyList [1] ++ MyList [2] ++ zero
let _res0 : MyNum = zero
let _asQuotation = plus <@ ResizeArray (["1"]) @> <@ ResizeArray (["2;3"]) @>
let _quot123 = plus <@ ResizeArray ([1]) @> <@ ResizeArray ([2;3]) @>
let _quot1 = plus <@ ResizeArray ([1]) @> (zero)
let _quot23 = plus (zero) <@ ResizeArray ([2;3]) @>
let _quot13 = plus (zero) <@ ("1","3") @>
let lzy1 = plus (lazy [1]) (lazy [2;3])
let _lzy = plus (zero) lzy1
let asy1 = plus (async.Return [1]) (async.Return [2;3])
let _asy = plus (zero) asy1
let _bigNestedTuple1 = (1, System.Tuple (8, "ff",3,4,5,6,7,8,9,10,11,12,(),14,15,16,17,18,19,20)) ++ (2, System.Tuple (8, "ff",3,4,5,6,7,8,9,10,11,12,(),14,15,16,17,18,19,20)) ++ (3, System.Tuple (8, "ff",3,4,5,6,7,8,9,10,11,12,(),14,15,16,17,18,19,20))
let _bigNestedTuple2 = (1, System.Tuple (8, "ff",3,4,5,6,7,8,9,10,11,12,(),14,15,16,17,18,19,20)) ++ (zero, System.Tuple (8, "ff",3,4,5,6,7,8,9,10,11,12,(),14,15,16,17,18,19,20)) ++ zero
let _nes : NonEmptySeq<_> = plus (NonEmptySeq.singleton 1) (NonEmptySeq.singleton 2)
let mapA = Map.empty
|> Map.add 1 (async.Return "Hey")
|> Map.add 2 (async.Return "Hello")
let mapB = Map.empty
|> Map.add 3 (async.Return " You")
|> Map.add 2 (async.Return " World")
let mapAB = plus mapA mapB
let _greeting1 = Async.RunSynchronously mapAB.[2]
let _greeting2 = Async.RunSynchronously (Seq.sum [mapA; zero; mapB]).[2]
let dicA = new Dictionary<string,Task<string>> ()
dicA.["keya"] <- (result "Hey" : Task<_>)
dicA.["keyb"] <- (result "Hello": Task<_>)
let dicB = new Dictionary<string,Task<string>> ()
dicB.["keyc"] <- (result " You" : Task<_>)
dicB.["keyb"] <- (result " World": Task<_>)
let dicAB = plus dicA dicB
let _iDicAb = plus (dicA :> IDictionary<_,_>) (dicB :> IDictionary<_,_>)
let _iroDicAb = plus (dicA :> IReadOnlyDictionary<_,_>) (dicB :> IReadOnlyDictionary<_,_>)
let _greeting3 = extract dicAB.["keyb"]
let _greeting4 = extract (Seq.sum [dicA; zero; dicB]).["keyb"]
let _res2 = Seq.sum [async {return Endo ((+) 2)}; async {return Endo ((*) 10)}; async {return Endo id}; async {return Endo ((%) 3)}; async {return zero} ] |> Async.RunSynchronously |> Endo.run <| 3
let _res330 = Seq.sum [async {return (fun (x:int) -> string x)}; async {return (fun (x:int) -> string (x*10))}; async {return zero } ] </Async.RunSynchronously/> 3
()
module Functor =
[<Test>]
let mapDefaultCustom () =
SideEffects.reset ()
// NonEmptyList<_> has Map but at the same time is a seq<_>
let testVal1 = map ((+) 1) (nelist {10; 20; 30})
Assert.IsInstanceOf<Option<NonEmptyList<int>>> (Some testVal1)
let testVal2 = map ((+) 1) ((ofSeq :seq<_*_> -> Dictionary<_,_>) (seq ["a", 1; "b", 2]))
Assert.IsInstanceOf<Option<Dictionary<string,int>>> (Some testVal2)
let testVal3 = map ((+) 1) (dict (seq ["a", 1; "b", 2]))
Assert.IsInstanceOf<Option<IDictionary<string,int>>> (Some testVal3)
let testVal4 = map ((+) 1) (NonEmptySet.Create(10, 20, 30))
Assert.IsInstanceOf<Option<NonEmptySet<int>>> (Some testVal4)
let testVal5 = map ((+) 1) (NonEmptyMap.Create(("a", 1), ("b", 2)))
Assert.IsInstanceOf<Option<NonEmptyMap<string,int>>> (Some testVal5)
let testVal6 = map ((+) 1) (TestNonEmptyCollection.Create 1)
Assert.IsInstanceOf<Option<NonEmptySeq<int>>> (Some testVal6)
// WrappedSeqD is Applicative. Applicatives are Functors => map should work
Assert.AreEqual (list<string>.Empty, SideEffects.get ())
let testVal4 = map ((+) 1) (WrappedSeqD [1..3])
Assert.IsInstanceOf<Option<WrappedSeqD<int>>> (Some testVal4)
Assert.AreEqual (["Using WrappedSeqD's Return"; "Using WrappedSeqD's Apply"], SideEffects.get ())
SideEffects.reset ()
// WrappedListE is a Monad. Monads are Functors => map should work
let testVal5 = map ((+) 1) (WrappedListE [1..3])
Assert.IsInstanceOf<Option<WrappedListE<int>>> (Some testVal5)
// Same with WrappedListD but WrappedListD is also IEnumerable<_>
Assert.AreEqual (list<string>.Empty, SideEffects.get ())
let testVal6 = map ((+) 1) (WrappedListD [1..3])
Assert.IsInstanceOf<Option<WrappedListD<int>>> (Some testVal6)
Assert.AreEqual (["Using WrappedListD's Bind"; "Using WrappedListD's Return"; "Using WrappedListD's Return"; "Using WrappedListD's Return"], SideEffects.get ())
SideEffects.reset ()
let testVal7 = TestNonEmptyCollection.Create 42
head testVal7 |> ignore
Assert.AreEqual (["Using TestNonEmptyCollection's Head"], SideEffects.get ())
let testVal8 = testVal7 >>= fun i -> result (string i)
Assert.IsInstanceOf<Option<NonEmptySeq<string>>> (Some testVal8)
let testVal9 = map ((+) 1) (IReadOnlyCollection.ofList [1..3])
Assert.IsInstanceOf<Option<IReadOnlyCollection<int>>> (Some testVal9)
let testVal10 = map ((+) 1) (async { return 1})
Assert.IsInstanceOf<Option<Async<int>>> (Some testVal10)
areEqual 2 (testVal10 |> Async.RunSynchronously)
let testVal11 = (+) "h" <!> dict [1, "i"; 2, "ello"]
CollectionAssert.AreEqual (dict [(1, "hi"); (2, "hello")], testVal11)
let testVal12 =
let h: IDictionary<int, string> = result "h"
try
(+) <!> h <*> dict [1, "i"; 2, "ello"]
with _ -> dict [0, "failure"]
CollectionAssert.AreEqual (dict [0, "failure"], testVal12)
[<Test>]
let mapSquared () =
let x =
[Some 1; Some 2]
|>>> string
Assert.AreEqual ([Some "1"; Some "2"], x)
[<Test>]
let unzip () =
let testVal = unzip {Head = (1, 'a'); Tail = [(2, 'b');(3, 'b')]}
Assert.IsInstanceOf<Option<NonEmptyList<int> * NonEmptyList<char>>> (Some testVal)
let testVal2 = unzip (NonEmptyMap.Create((1,(true, 'a')), (2, (false, 'b'))))
Assert.IsInstanceOf<Option<NonEmptyMap<int, bool> * NonEmptyMap<int, char>>> (Some testVal2)
[<Test>]
let zipTest () =
SideEffects.reset ()
let _a = zip (seq [1;2;3]) (seq [1. .. 3. ])
Assert.AreEqual (list<string>.Empty, SideEffects.get ())
let _b = zip (WrappedListD [1;2;3]) (WrappedListD [1. .. 3. ])
Assert.AreEqual (["Using WrappedListD's zip"], SideEffects.get ())
let _c = zip (dict [1,'1' ; 2,'2' ; 4,'4']) (dict [1,'1' ; 2,'2' ; 3,'3'])
let _d = zip [ 1;2;3 ] [ 1. .. 3. ]
let _e = zip [|1;2;3|] [|1. .. 3.|]
let _g = zip ((seq [1;2;3]).GetEnumerator ()) ((seq [1. .. 3. ]).GetEnumerator ())
let _h = zip (Map.ofSeq [1,'1' ; 2,'2' ; 4,'4']) (Map.ofSeq [1,'1' ; 2,'2' ; 3,'3'])
let _i = zip (ofSeq [1,'1' ; 2,'2' ; 4,'4'] : Dictionary<_,_>) (ofSeq [1,'1' ; 2,'2' ; 3,'3'] : Dictionary<_,_>)
let _j = zip (async {return 1}) (async {return '2'})
let _h = zip (Task.FromResult 1) (Task.FromResult '2')
let _i = zip List.singleton<int> Array.singleton<int>
let _k = zip (TestNonEmptyCollection.Create 1) (result 2)
let _fa a = zip a (seq [1. .. 3. ])
let _fb a = zip a (WrappedListD [1. .. 3. ])
let _fc a = zip a (dict [1,'1' ; 2,'2' ; 3,'3'])
let _fd a = zip a [ 1. .. 3. ]
let _fe a = zip a [|1. .. 3.|]
let _fg a = zip a ((seq [1. .. 3. ]).GetEnumerator ())
let _fh a = zip a (Map.ofSeq [1,'1' ; 2,'2' ; 3,'3'])
let _fi a = zip a (ofSeq [1,'1' ; 2,'2' ; 3,'3'] : Dictionary<_,_>)
let _fj a = zip a (async {return '2'})
let _fh a = zip a (Task.FromResult '2')
let _fi a = zip a Array.singleton<int>
let _fj a = zip a (TestNonEmptyCollection.Create 2)
let _ga b = zip (seq [1;2;3]) b
let _gb b = zip (WrappedListD [1;2;3]) b
let _gc b = zip (dict [1,'1' ; 2,'2' ; 4,'4']) b
let _gd b = zip [ 1;2;3 ] b
let _ge b = zip [|1;2;3|] b
let _gg b = zip ((seq [1;2;3]).GetEnumerator ()) b
let _gh b = zip (Map.ofSeq [1,'1' ; 2,'2' ; 4,'4']) b
let _gi b = zip (ofSeq [1,'1' ; 2,'2' ; 4,'4'] : Dictionary<_,_>) b
let _gj b = zip (async {return 1}) b
let _gh b = zip (Task.FromResult 1) b
let _gh b = zip List.singleton<int> b
let _gj b = zip (TestNonEmptyCollection.Create 1) b
let _ha : _ -> _ -> _ seq = zip
let _hb : _ -> _ -> _ WrappedListD = zip
let _hc : _ -> _ -> IDictionary<_,_> = zip
let _hd : _ -> _ -> _ list = zip
let _he : _ -> _ -> _ [] = zip
let _hg : _ -> _ -> _ IEnumerator = zip
let _hh : _ -> _ -> Map<_,_> = zip
let _hi : _ -> _ -> Dictionary<_,_> = zip
let _hj : _ -> _ -> Async<_> = zip
let _hh : _ -> _ -> Task<_> = zip
let _hi : _ -> _ -> (int -> _ ) = zip
let _hj : _ -> _ -> NonEmptySeq<_> = zip
()
[<Test>]
let genericZipShortest () =
let a = zip [|1; 2; 3|] [|"a"; "b"|]
CollectionAssert.AreEqual ([|1,"a"; 2,"b"|], a)
let l = zip [1; 2] ["a"; "b"; "c"]
CollectionAssert.AreEqual ([1,"a"; 2,"b"], l)
let e = zip (ResizeArray [1; 2]) (ResizeArray ["a"; "b"; "c"])
CollectionAssert.AreEqual (ResizeArray [1,"a"; 2,"b"], e)
let nel = zip (NonEmptyList.ofList [1; 2]) (NonEmptyList.ofList ["a"; "b"; "c"])
CollectionAssert.AreEqual (NonEmptyList.ofList [1,"a"; 2,"b"], nel)
[<Test>]
let iterTests () =
let li = [1, 2; 3, 4]
let di: Dictionary<int, int> = ofList li
let id = dict li
let ir = readOnlyDict li
let ma = Map.ofList li
let r = ResizeArray<string> []
iter (r.Add << string) di
iter (r.Add << string) id
iter (r.Add << string) ir
iter (r.Add << string) ma
CollectionAssert.AreEqual (ResizeArray ["2"; "4"; "2"; "4"; "2"; "4"; "2"; "4"], r)
module Foldable =
let foldables =
let _10 = foldBack (+) (seq [1;2;3;4]) 0
let _323 = toList (seq [3;2;3])
let _03 = filter ((=) 3) (seq [1;2;3])
()
[<Test>]
let foldMapDefaultCustom () =
SideEffects.reset ()
let x = foldMap ((+) 10) (WrappedListD [1..4]) //= 50 w side effect
Assert.AreEqual (50, x)
Assert.AreEqual (["Using optimized foldMap"], SideEffects.get ())
SideEffects.reset ()
let _ = foldMap ((+) 10) {1..4} //= 50 w/o side effect
Assert.AreEqual (50, x)
Assert.AreEqual (list<string>.Empty, SideEffects.get ())
[<Test>]
let filterDefaultCustom () =
let wlA1 = WrappedListA [1..10]
let testVal = filter ((=)2) wlA1
Assert.AreEqual (WrappedListA [2], testVal)
Assert.IsInstanceOf<Option<WrappedListA<int>>> (Some testVal)
let _twos = filter ((=) (box 2)) (([1;2;3;4;3;2;1;2;3] |> ofSeq) : Collections.ArrayList)
let _five = filter ((=) 5) (WrappedListB' [1;2;3;4;5;6]) // <- Uses the default method for filter.
let _optionFilter = filter ((=) 3) (Some 4)
()
[<Test>]
let foldAternatives () =
let x = choice [None; Some 3; Some 4; None]
let y = choice [| []; [3]; [4]; [] |]
Assert.AreEqual (Some 3, x)
Assert.AreEqual ([3; 4], y)
[<Test>]
let fromToSeq () =
let s = seq [Collections.Generic.KeyValuePair(1, "One"); Collections.Generic.KeyValuePair(2, "Two")]
let t = {'a'..'d'}
let dc2:Collections.Generic.Dictionary<_,_> = ofSeq s
let s' = toSeq dc2
let arr:_ [] = ofSeq s
let s'' = toSeq arr
let str:string = ofSeq t
let t' = toSeq str
Assert.AreEqual (toList s, toList s')
Assert.AreEqual (toList s, toList s'')
Assert.AreEqual (toList t, toList t')
Assert.IsInstanceOf ((Some s' ).GetType (), Some s)
Assert.IsInstanceOf ((Some s'').GetType (), Some s)
Assert.IsInstanceOf ((Some t' ).GetType (), Some t)
[<Test>]
let sortBy () =
let l = [10;4;6;89]
let l' = sortBy id l
let s = WrappedListB [10;4;6;89]
let s' = sortBy id s
Assert.AreEqual ([4;6;10;89], l')
Assert.AreEqual (WrappedListB [4;6;10;89], s')
let sortedList = sortBy string [ 11;2;3;9;5;6;7;8;9;10 ]
let sortedSeq = sortBy string (seq [11;2;3;9;5;6;7;8;9;10])
Assert.IsInstanceOf<Option<list<int>>> (Some sortedList)
Assert.IsInstanceOf<Option<seq<int>>> (Some sortedSeq)
[<Test>]
let intersperse () =
Assert.AreEqual ("a,b,c,d,e", intersperse ',' "abcde")
Assert.AreEqual (["a";",";"b";",";"c";",";"d";",";"e"], intersperse "," ["a";"b";"c";"d";"e"])
[<Test>]
let readOnlyIntercalate () =
Assert.AreEqual ("Lorem, ipsum, dolor", intercalate ", " ["Lorem"; "ipsum"; "dolor"])
Assert.AreEqual ("Lorem, ipsum, dolor", intercalate ", " (ReadOnlyCollection( [|"Lorem"; "ipsum"; "dolor"|] )))
[<Test>]
let readOnlyTryPick () =
let readOnlyCollection = ReadOnlyCollection( [|1..10|] )
let iReadOnlyList = readOnlyCollection :> IReadOnlyList<_>
let picker i = if i % 3 = 0 then Some i else None
Assert.AreEqual (Some 3, tryPick picker [1..10])
Assert.AreEqual (Some 3, tryPick picker readOnlyCollection)
Assert.AreEqual (Some 3, tryPick picker iReadOnlyList)
[<Test>]
let readOnlyTryFind () =
let predicate i = i % 3 = 0
let readOnlyCollection = ReadOnlyCollection( [|1..10|] )
let iReadOnlyList = readOnlyCollection :> IReadOnlyList<_>
Assert.AreEqual (Some 3, tryFind predicate [1..10])
Assert.AreEqual (Some 3, tryFind predicate readOnlyCollection)
Assert.AreEqual (Some 3, tryFind predicate iReadOnlyList)
[<Test>]
let readOnlyfoldMap () =
let readOnlyCollection = ReadOnlyCollection( [|1..4|] )
let iReadOnlyList = readOnlyCollection :> IReadOnlyList<_>
Assert.AreEqual (50, foldMap ((+) 10) readOnlyCollection)
Assert.AreEqual (50, foldMap ((+) 10) iReadOnlyList)
[<Test>]
let exists () =
SideEffects.reset ()
let _ = exists ((=) 2) [1..3]
let _ = exists ((=) '2') (System.Text.StringBuilder "abc")
let _ = exists ((=) 2) (NonEmptySet.Create(1,2,3))
let _ = exists ((=) 2) (WrappedListA [1..3])
let _ = exists ((=) 2) (WrappedListD [1..3])
areEqual ["Using WrappedListA's Exists"; "Using WrappedListD's Exists"] (SideEffects.get ())
()
[<Test>]
let pick () =
SideEffects.reset ()
let _ = pick Some [1..3]
let _ = pick Some (System.Text.StringBuilder "abc")
let _ = pick Some (NonEmptySet.Create(1,2,3))
let _ = pick Some (WrappedListA [1..3])
let _ = pick Some (WrappedListD [1..3])
areEqual ["Using WrappedListA's Pick"; "Using WrappedListD's Pick"] (SideEffects.get ())
()
[<Test>]
let minimum () =
SideEffects.reset ()
let _ = minimum [1..3]
let _ = minimum (System.Text.StringBuilder "abc")
let _ = minimum (NonEmptySet.Create(1,2,3))
let _ = minimum (WrappedListA [1..3])
let _ = minimum (WrappedListD [1..3])
areEqual ["Using WrappedListA's Min"; "Using WrappedListD's Min"] (SideEffects.get ())
()
[<Test>]
let maxBy () =
SideEffects.reset ()
let _ = maxBy id [1..3]
let _ = maxBy id (System.Text.StringBuilder "abc")
let _ = maxBy id (NonEmptySet.Create(1,2,3))
let _ = maxBy id (WrappedListA [1..3])
let _ = maxBy id (WrappedListD [1..3])
areEqual ["Using WrappedListA's MaxBy"; "Using WrappedListD's MaxBy"] (SideEffects.get ())
()
[<Test>]
let length () =
SideEffects.reset ()
let _ = length [1..3]
let _ = length (System.Text.StringBuilder "abc")
let _ = length (NonEmptySet.Create(1,2,3))
let _ = length (WrappedListA [1..3])
let _ = length (WrappedListD [1..3])
areEqual ["Using WrappedListA's Length"; "Using WrappedListD's Length"] (SideEffects.get ())
()
[<Test>]
let tryHead () =
let s = tryHead <| seq [1;2]
let s': int option = tryHead <| seq []
areEqual s (Some 1)
areEqual s' None
let l = tryHead [1;2;3]
let l': int option = tryHead []
areEqual l (Some 1)
areEqual l' None
let a = tryHead [|1|]
let a': int option = tryHead [||]
areEqual a (Some 1)
areEqual a' None
let nes = tryHead <| NonEmptySeq.ofList [1;2]
areEqual nes (Some 1)
let str = tryHead "string"
let str': char option = tryHead ""
areEqual str (Some 's')
areEqual str' None
let sb = tryHead (System.Text.StringBuilder("string"))
let sb' = tryHead (System.Text.StringBuilder())
areEqual sb (Some 's')
areEqual sb' None
()
[<Test>]
let tryLast () =
let s = tryLast <| seq [1;2]
let s': int option = tryLast <| seq []
areEqual s (Some 2)
areEqual s' None
let l = tryLast [1;2;3]
let l': int option = tryLast []
areEqual l (Some 3)
areEqual l' None
let a = tryLast [|1|]
let a': int option = tryLast [||]
areEqual a (Some 1)
areEqual a' None
let nes = tryLast <| NonEmptySeq.ofList [1;2]
areEqual nes (Some 2)
let str = tryLast "string"
let str': char option = tryLast ""
areEqual str (Some 'g')
areEqual str' None
let sb = tryLast (System.Text.StringBuilder("string"))
let sb' = tryLast (System.Text.StringBuilder())
areEqual sb (Some 'g')
areEqual sb' None
()
[<Test>]
let testCompileAndExecuteTryItem () =
let a = Map.ofSeq [1, "one"; 2, "two"]
let _ = tryItem 1 a
let b = dict [1, "one"; 2, "two"]
let _ = tryItem 1 b
let c = "two"
let _ = tryItem 1 c
let d = System.Text.StringBuilder "one"
let _ = tryItem 1 d
let e = array2D [[1;2];[3;4];[5;6]]
let _ = tryItem (1, 1) e
let f = [1, "one"; 2, "two"]
let _ = tryItem 1 f
let g = [|1, "one"; 2, "two"|]
let _ = tryItem 1 g
let h = ResizeArray [1, "one"; 2, "two"]
let _ = tryItem 1 h
let i = Array3D.create 3 2 2 0
let _ = tryItem (1, 1, 1) i
let j = Array4D.create 3 2 2 3 0
let _ = tryItem (1, 1, 1, 1) j
let k = NonEmptyMap.Create (("a", 1), ("b", 2))
let _ = tryItem "b" k
let w = WrappedListA [1, "one"; 2, "two"]
let _ = tryItem 1 w
()
[<Test>]
let testCompileAndExecuteTraverseIndexed () =
let nem = NonEmptyMap.Create (("a", Some 1), ("b", Some 2), ("c", Some 3))
let rs1 = traversei (fun _ v -> v) nem
Assert.IsInstanceOf<option<NonEmptyMap<string, int>>> rs1
[<Test>]
let tryItemReadonly () =
let d = ReadOnlyDictionary (dict [1, "one"; 2, "two"])
let iReadOnlyDict = d :> IReadOnlyDictionary<_,_>
let l = ReadOnlyCollection [|1..10|]
let iReadOnlyList = l :> IReadOnlyList<_>
let rarr = ResizeArray [|1..10|]
Assert.AreEqual (Some "one", tryItem 1 d)
Assert.AreEqual (Some "one", tryItem 1 iReadOnlyDict)
Assert.AreEqual ("one", item 1 d)
Assert.AreEqual ("one", item 1 iReadOnlyDict)
Assert.AreEqual (2, item 1 l)
Assert.AreEqual (2, item 1 rarr)
Assert.AreEqual (2, item 1 iReadOnlyList)
Assert.AreEqual (Some 2, tryItem 1 l)
Assert.AreEqual (Some 2, tryItem 1 iReadOnlyList)
Assert.AreEqual (Some 2, tryItem 1 rarr)
[<Test>]
let mapiUsage () =
let m = Map.ofList [1, "one"; 2, "two"]
let l = ReadOnlyCollection [|1..2|]
let iReadOnlyList = l :> IReadOnlyList<_>
let rarr = ResizeArray [|1..2|]
let mapDS = sprintf "%d-%s"
areEquivalent [KeyValuePair(1,"1-one"); KeyValuePair(2,"2-two")] (mapi mapDS m)
let mapDD = sprintf "%d-%d"
areEquivalent ["0-1";"1-2"] (mapi mapDD l)
areEquivalent ["0-1";"1-2"] (mapi mapDD iReadOnlyList)
areEquivalent ["0-1";"1-2"] (mapi mapDD rarr)
// correct overload:
SideEffects.reset ()
areEquivalent ["0-1";"1-2"] (mapi mapDD (WrappedListD [1..2]))
areEqual ["Using WrappedListD's MapIndexed"] (SideEffects.get ())
SideEffects.reset ()
areEquivalent ["0-1";"1-2"] (MapIndexed.InvokeOnInstance mapDD (WrappedListD [1..2]))
areEqual ["Using WrappedListD's MapIndexed"] (SideEffects.get ())
[<Test>]
let iteriUsage () =
let m = Map.ofList [1, "one"; 2, "two"]
SideEffects.reset ()
iteri (fun i v -> SideEffects.add <| sprintf "Got %d-%s" i v) m
areEquivalent ["Got 1-one";"Got 2-two"] (SideEffects.get ())
SideEffects.reset ()
let onIteration i v= ()
iteri onIteration (WrappedListD [1..2])
areEqual ["Using WrappedListD's IterateIndexed"] (SideEffects.get ())
SideEffects.reset ()
IterateIndexed.InvokeOnInstance onIteration (WrappedListD [1..2])
areEqual ["Using WrappedListD's IterateIndexed"] (SideEffects.get ())
[<Test>]
let foldiUsage () =
SideEffects.reset ()
let folder (s:int) (i:int) (t:int) = t * s - i
let wlist = WrappedListD [1..2]
let res = foldi folder 10 wlist
areEquivalent ["Using WrappedListD's FoldIndexed"] (SideEffects.get ())
areEqual 19 res
SideEffects.reset ()
let res1 = FoldIndexed.InvokeOnInstance folder 10 wlist
areEquivalent ["Using WrappedListD's FoldIndexed"] (SideEffects.get ())
areEqual 19 res1
[<Test>]
let traverseiUsage () =
let m1 = WrappedMapA.ofList [(1, [1;1;1]); (2, [2;2;2])]
SideEffects.reset ()
let r1 = m1 |> TraverseIndexed.InvokeOnInstance (fun _ _ -> None)
Assert.AreEqual(None, r1)
areEquivalent ["Using WrappedMapA's TraverseIndexed"] (SideEffects.get ())
SideEffects.reset ()
let r1 = m1 |> traversei (fun _ _ -> None)
Assert.AreEqual(None, r1)
areEquivalent ["Using WrappedMapA's TraverseIndexed"] (SideEffects.get ())
SideEffects.reset ()
let r2 = m1 |> TraverseIndexed.InvokeOnInstance (fun i v -> if List.forall ((=) i) v then Some (i :: v) else None)
areEqual (WrappedMapA.ofList [(1, [1;1;1;1]); (2, [2;2;2;2])]) r2.Value
areEquivalent ["Using WrappedMapA's TraverseIndexed"] (SideEffects.get ())
SideEffects.reset ()
let r3 = m1 |> traversei (fun i v -> if List.forall ((=) i) v then Some (i :: v) else None)
areEqual (WrappedMapA.ofList [(1, [1;1;1;1]); (2, [2;2;2;2])]) r3.Value
areEquivalent ["Using WrappedMapA's TraverseIndexed"] (SideEffects.get ())
[<Test>]
let findIndexUsage () =
let m1 = WrappedListD [0..4]
SideEffects.reset ()
let i1 = findIndex ((=) 2) m1
areEquivalent ["Using WrappedListD's FindIndex"] (SideEffects.get ())
areEqual i1 2
[<Test>]
let findSliceIndexUsage () =
let m1 = WrappedListD [0..4]
let m2 = WrappedListD [1..3]
SideEffects.reset ()
let i1 = findSliceIndex m2 m1
areEquivalent ["Using WrappedListD's FindSliceIndex"] (SideEffects.get ())
areEqual i1 1
[<Test>]
let findLastSliceIndexUsage () =
let m1 = WrappedListD [0..4]
let m2 = WrappedListD [1..3]
SideEffects.reset ()
let i1 = findLastSliceIndex m2 m1
areEquivalent ["Using WrappedListD's FindLastSliceIndex"] (SideEffects.get ())
areEqual i1 1
module Monad =
[<Test>]
let joinDefaultCustom () =
let x = join [[1];[2]]
Assert.AreEqual ([1;2], x)
let y: WrappedListE<_> = join (WrappedListE [WrappedListE [1];WrappedListE [2]])
Assert.AreEqual (WrappedListE [1;2], y)
SideEffects.reset ()
let z = join (WrappedListF [WrappedListF [1];WrappedListF [2]])
Assert.AreEqual (WrappedListF [1;2], z)
Assert.AreEqual (["Join"], SideEffects.get ())
[<Test>]
let workFlow () =
let testVal =
monad {
let! x1 = WrappedListD [1;2]
let! x2 = WrappedListD [10;20]
return ((+) x1 x2) }
Assert.IsInstanceOf<WrappedListD<int>> (testVal)
[<Test>]
let DelayForCont () =
// If Delay is not properly implemented this will stack-overflow
// See http://stackoverflow.com/questions/11188779/stackoverflow-in-continuation-monad
#if MONO
Assert.Ignore ()
#else
let map f xs =
let rec loop xs =
monad {
match xs with
| [] -> return []
| x :: xs ->
let! xs = loop xs
return f x :: xs }
Cont.run (loop xs) id
let _ = [1..100000] |> map ((+) 1)
Assert.Pass ()
#endif
type ZipList<'s> = ZipList of 's seq with
static member Map (ZipList x, f:'a->'b) = ZipList (Seq.map f x)
static member Return (x:'a) = ZipList (Seq.initInfinite (konst x))
static member (<*>) (ZipList (f:seq<'a->'b>), ZipList x) = ZipList (Seq.zip f x |> Seq.map (fun (f, x) -> f x)) : ZipList<'b>
type ZipList'<'s> = ZipList' of 's seq with
static member Return (x:'a) = ZipList' (Seq.initInfinite (konst x))
static member (<*>) (ZipList' (f:seq<'a->'b>), ZipList' x) = ZipList' (Seq.zip f x |> Seq.map (fun (f, x) -> f x)) : ZipList'<'b>
module Applicative =
[<Test>]
let applicativeMath () =
let inline (+) (a: 'T) (b: 'T) : 'T = a + b
let inline ( .+ ) (x: 'Functor't) (y: 't) = map ((+)/> y) x : 'Functor't
let inline ( +. ) (x: 't) (y: 'Functor't) = map ((+) x) y : 'Functor't
let inline ( .+. ) (x: 'Applicative't) (y: 'Applicative't) = (+) <!> x <*> y : 'Applicative't
let testVal = [1;2] .+. [10;20] .+. [100;200] .+ 2
Assert.AreEqual ([113; 213; 123; 223; 114; 214; 124; 224], testVal)
Assert.IsInstanceOf<Option<list<int>>> (Some testVal)
let testVal2 = NonEmptySeq.create 1 [2] .+. NonEmptySeq.create 10 [20] .+. NonEmptySeq.create 100 [200] .+ 2
Assert.AreEqual ([113; 213; 123; 223; 114; 214; 124; 224], Seq.toList testVal2)
Assert.IsInstanceOf<Option<NonEmptySeq<int>>> (Some testVal2)
let testLTE1 = Some 1 .<=. Some 2
Assert.AreEqual (Some true, testLTE1)
Assert.IsInstanceOf<Option<bool>> testLTE1
let testLTE2 = Some 1 .<= 2
Assert.AreEqual (Some true, testLTE2)
Assert.IsInstanceOf<Option<bool>> testLTE2
let testLTE3 = 3 <=. Some 1
Assert.AreEqual (Some false, testLTE3)
Assert.IsInstanceOf<Option<bool>> testLTE3
let testLTE4 = Some 3 .<=. Some 3
Assert.AreEqual (Some true, testLTE4)
Assert.IsInstanceOf<Option<bool>> testLTE4