forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardGraceClass.grace
2255 lines (2113 loc) · 75.4 KB
/
standardGraceClass.grace
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
#pragma ExtendedLineups
dialect "none"
def traits = object {
// these definitions are at module-level to avoid problems with outer
// and the c-backend
type Block1⟦Arg, Res⟧ = type {
apply(a:Arg) -> Res
}
type Block2⟦Arg1, Arg2, Res⟧ = type {
apply(a:Arg1, b:Arg2) -> Res
}
class collection⟦T⟧ {
method asString { "a collection trait" }
method sizeIfUnknown(action) {
action.apply
}
method size {
standardGrace.SizeUnknown.raise "this collection does not know its size"
}
method do { standardGrace.abstract }
method iterator { standardGrace.abstract }
method isEmpty {
// override if size is known
iterator.hasNext.not
}
method first {
def it = self.iterator
if (it.hasNext) then {
it.next
} else {
standardGrace.BoundsError.raise "no first element in {self}"
}
}
method do(block1) separatedBy(block0) {
var firstTime := true
var i := 0
self.do { each ->
if (firstTime) then {
firstTime := false
} else {
block0.apply
}
block1.apply(each)
}
return self
}
method reduce(initial, blk) {
// deprecated; for compatibility with builtInList
fold(blk)startingWith(initial)
}
method fold(blk)startingWith(initial) {
var result := initial
self.do {it ->
result := blk.apply(result, it)
}
return result
}
method map⟦R⟧(block1:Block1⟦T,R⟧) // -> Enumerable⟦R⟧
{
standardGrace.lazySequenceOver⟦T,R⟧(self) mappedBy(block1)
}
method filter(selectionCondition:Block1⟦T,Boolean⟧) // -> Enumerable⟦T⟧
{
standardGrace.lazySequenceOver⟦T⟧(self) filteredBy(selectionCondition)
}
method >> (collFactory) {
collFactory.withAll(self)
}
}
class enumerable⟦T⟧ {
inherit traits.collection⟦T⟧
method iterator { standardGrace.abstract }
method size {
// override if size is known
standardGrace.SizeUnknown.raise "size requested on {asDebugString}"
}
method asDictionary {
def result = standardGrace.dictionary.empty
keysAndValuesDo { k, v ->
result.at(k) put(v)
}
return result
}
method into(existing) // -> Collection⟦T⟧
{
def selfIterator = self.iterator
while {selfIterator.hasNext} do {
existing.add(selfIterator.next)
}
existing
}
method ==(other) {
standardGrace.isEqual (self) toIterable (other)
}
method do(block1:Block1⟦T,Done⟧) -> Done {
def selfIterator = self.iterator
while {selfIterator.hasNext} do {
block1.apply(selfIterator.next)
}
}
method keysAndValuesDo(block2:Block2⟦Number,T,Done⟧) -> Done {
var ix := 0
def selfIterator = self.iterator
while {selfIterator.hasNext} do {
ix := ix + 1
block2.apply(ix, selfIterator.next)
}
}
method values // -> Collection⟦T⟧
{
self
}
method fold⟦R⟧(block2)startingWith(initial) -> R {
var res := initial
def selfIterator = self.iterator
while { selfIterator.hasNext } do {
res := block2.apply(res, selfIterator.next)
}
return res
}
method ++ (other) // -> Enumerable⟦T⟧
{
standardGrace.lazyConcatenation(self, other)
}
method sortedBy(sortBlock:Block2) // -> List⟦T⟧
{
standardGrace.list.withAll(self).sortBy(sortBlock)
}
method sorted // -> List⟦T⟧
{
standardGrace.list.withAll(self).sort
}
method asString {
var s := "⟨"
do { each -> s := s ++ each.asString } separatedBy { s := s ++ ", " }
s ++ "⟩"
}
}
class indexable⟦T⟧ {
inherit traits.collection⟦T⟧
method at(index) { standardGrace.abstract }
method size { standardGrace.abstract }
method isEmpty { size == 0 }
method keysAndValuesDo(action:Block2⟦Number,T,Done⟧) -> Done {
def curSize = size
var i := 1
while {i <= curSize} do {
action.apply(i, self.at(i))
i := i + 1
}
}
method first { at(1) }
method second { at(2) }
method third { at(3) }
method fourth { at(4) }
method fifth { at(5) }
method last { at(size) }
method indices { standardGrace.range.from 1 to(size) }
method indexOf(sought:T) {
indexOf(sought) ifAbsent {
standardGrace.NoSuchObject.raise "collection does not contain {sought}"
}
}
method indexOf(sought:T) ifAbsent(action) {
keysAndValuesDo { ix, v ->
if (v == sought) then { return ix }
}
action.apply
}
method asDictionary {
def result = standardGrace.dictionary.empty
keysAndValuesDo { k, v ->
result.at(k) put(v)
}
return result
}
method into(existing) // -> Collection⟦T⟧
{
def selfIterator = self.iterator
while {selfIterator.hasNext} do {
existing.add(selfIterator.next)
}
existing
}
}
}
class standardGrace {
def BoundsError is public = _prelude.BoundsError
def EnvironmentException is public = _prelude.EnvironmentException
def Exception is public = _prelude.Exception
def NoSuchMethod is public = _prelude.NoSuchMethod
def ProgrammingError is public = _prelude.ProgrammingError
def RequestError is public = _prelude.RequestError
def ResourceException is public = _prelude.ResourceException
def RuntimeError is public = _prelude.RuntimeError
def TypeError is public = _prelude.TypeError
def UninitializedVariable is public = _prelude.UninitializedVariable
def ConcurrentModification is public = ProgrammingError.refine "ConcurrentModification"
def IteratorExhausted is public = ProgrammingError.refine "IteratorExhausted"
def NoSuchObject is public = ProgrammingError.refine "NoSuchObject"
def SizeUnknown is public = Exception.refine "SizeUnknown"
def SubobjectResponsibility is public = ProgrammingError.refine "SubobjectResponsibility"
class SuccessfulMatch.new(result', bindings') {
inherit true
method result { result' }
method bindings { bindings' }
method asString {
"SuccessfulMatch(result = {result}, bindings = {bindings})"
}
}
class FailedMatch.new(result') {
inherit false
method result { result' }
method bindings { emptySequence }
method asString {
"FailedMatch(result = {result})"
}
}
method abstract {
SubobjectResponsibility.raise "abstract method not overriden by subobject"
}
method required {
SubobjectResponsibility.raise "required method not overriden by subobject"
}
method do (action) while (condition) {
while {
action.apply
condition.apply
} do { }
}
method repeat (n) times (action) {
var ix := n
while {ix > 0} do {
ix := ix - 1
action.apply
}
}
method for (cs) and (ds) do (action) -> Done {
def dIter = ds.iterator
cs.do { c ->
if (dIter.hasNext) then {
action.apply(c, dIter.next)
} else {
return
}
}
}
method min(a, b) {
if (a < b) then { a } else { b }
}
method max(a, b) {
if (a > b) then { a } else { b }
}
method valueOf (nullaryBlock) {
nullaryBlock.apply
}
class BasicPattern.new {
method &(o) {
AndPattern.new(self, o)
}
method |(o) {
OrPattern.new(self, o)
}
}
class MatchAndDestructuringPattern.new(pat, items') {
inherit BasicPattern.new
def pattern = pat
def items = items'
method match(o) {
def m = pat.match(o)
if (m) then{
var mbindings := m.bindings
def bindings = []
if (mbindings.size < items.size) then {
if (Extractable.match(o)) then {
mbindings := o.extract
} else {
return FailedMatch.new(o)
}
}
for (items.indices) do {i->
def b = items.at(i).match(mbindings.at(i))
if (!b) then {
return FailedMatch.new(o)
}
for (b.bindings) do {bb->
bindings.push(bb)
}
}
SuccessfulMatch.new(o, bindings)
} else {
FailedMatch.new(o)
}
}
}
class VariablePattern.new(nm) {
inherit BasicPattern.new
method match(o) {
SuccessfulMatch.new(o, [o])
}
}
class BindingPattern.new(pat) {
inherit BasicPattern.new
method match(o) {
def bindings = [o]
def m = pat.match(o)
if (!m) then {
return m
}
for (m.bindings) do {b->
bindings.push(b)
}
SuccessfulMatch.new(o, bindings)
}
}
class WildcardPattern.new {
inherit BasicPattern.new
method match(o) {
SuccessfulMatch.new(done, [])
}
}
class AndPattern.new(p1, p2) {
inherit BasicPattern.new
method match(o) {
def m1 = p1.match(o)
if (!m1) then {
return m1
}
def m2 = p2.match(o)
if (!m2) then {
return m2
}
def bindings = []
for (m1.bindings) do {b->
bindings.push(b)
}
for (m2.bindings) do {b->
bindings.push(b)
}
SuccessfulMatch.new(o, bindings)
}
}
class OrPattern.new(p1, p2) {
inherit BasicPattern.new
method match(o) {
if (p1.match(o)) then {
return SuccessfulMatch.new(o, [])
}
if (p2.match(o)) then {
return SuccessfulMatch.new(o, [])
}
FailedMatch.new(o)
}
}
def Singleton is public = object {
class new {
inherit BasicPattern.new
method match(other) {
if (self.isMe(other)) then {
SuccessfulMatch.new(other, [])
} else {
FailedMatch.new(other)
}
}
method ==(other) { self.isMe(other) }
}
class named(printString) {
inherit Singleton.new
method asString { printString }
}
}
class BaseType.new(name) {
method &(o) {
TypeIntersection.new(self, o)
}
method |(o) {
TypeVariant.new(self, o)
}
method +(o) {
TypeUnion.new(self, o)
}
method -(o) {
TypeSubtraction.new(self, o)
}
method asString {
if (name == "") then { "type ‹anon›" }
else { "type {name}" }
}
}
class TypeIntersection.new(t1, t2) {
inherit AndPattern.new(t1, t2)
// inherit BaseType.new
method &(o) {
TypeIntersection.new(self, o)
}
method |(o) {
TypeVariant.new(self, o)
}
method +(o) {
TypeUnion.new(self, o)
}
method -(o) {
TypeSubtraction.new(self, o)
}
method methodNames {
t1.methodNames.addAll(t2.methodNames)
}
method asString { "({t1} & {t2})" }
}
class TypeVariant.new(t1, t2) {
inherit OrPattern.new(t1, t2)
// inherit BaseType.new
method &(o) {
TypeIntersection.new(self, o)
}
method |(o) {
TypeVariant.new(self, o)
}
method +(o) {
TypeUnion.new(self, o)
}
method -(o) {
TypeSubtraction.new(self, o)
}
method methodNames {
self.TypeVariantsCannotBeCharacterizedByASetOfMethods
}
method asString { "({t1} | {t2})" }
}
class TypeUnion.new(t1, t2) {
inherit BasicPattern.new
// inherit BaseType.new
method &(o) {
TypeIntersection.new(self, o)
}
method |(o) {
TypeVariant.new(self, o)
}
method +(o) {
TypeUnion.new(self, o)
}
method -(o) {
TypeSubtraction.new(self, o)
}
method methodNames {
t1.methodNames ** t2.methodNames
}
method match(o) {
ResourceException.raise "matching against a TypeUnion not yet implemented"
// Why not? Becuase it requires reflection, which
// requires the mirror module, which requires this module.
def mirror = ...
def oMethodNames = mirror.reflect(o).methodNames
for (self.methodNames) do { each ->
if (! oMethodNames.contains(each)) then {
return FailedMatch.new(o)
}
}
return SuccessfulMatch.new(o, [])
}
method asString { "({t1} + {t2})" }
}
class TypeSubtraction.new(t1, t2) {
inherit BasicPattern.new
method &(o) {
TypeIntersection.new(self, o)
}
method |(o) {
TypeVariant.new(self, o)
}
method +(o) {
TypeUnion.new(self, o)
}
method -(o) {
TypeSubtraction.new(self, o)
}
method methodNames {
t1.methodNames.removeAll(t2.methodNames)
}
method asString { "({t1} - {t2})" }
}
// Now define the types. Because some of the types are defined using &,
// TypeIntersection must be defined first.
type Extractable = {
extract
}
type MatchResult = Boolean & type {
result -> Unknown
bindings -> List⟦Unknown⟧
}
type Pattern = {
& (other:Pattern) -> Pattern
| (other:Pattern) -> Pattern
match(value:Object) -> MatchResult
}
type ExceptionKind = Pattern & type {
refine (parentKind:ExceptionKind) -> ExceptionKind
parent -> ExceptionKind
raise (message:String) -> Done
raise (message:String) with (argument:Object) -> Done
}
type Point = {
x -> Number
// the x-coordinates of self
y -> Number
// the y-coordinate of self
== (other:Object) -> Boolean
// true if other is a Point with the same x and y coordinates as self.
+ (other:Point) -> Point
// the Point that is the vector sum of self and other, i.e. (self.x+other.x) @ (self.y+other.y)
- (other:Point) -> Point
// the Point that is the vector difference of self and other, i.e. (self.x-other.x) @ (self.y-other.y)
prefix- -> Point
// the negation of self
* (factor:Number) -> Point
// this point scaled by factor, i.e. (self.x*factor) @ (self.y*factor)
/ (factor:Number) -> Point
// this point scaled by 1/factor, i.e. (self.x/factor) @ (self.y/factor)
length -> Number
// distance from self to the origin
distanceTo(other:Point) -> Number
// distance from self to other
dot (other:Point) -> Number
⋅ (other:Point) -> Number
// dot product of self and other
norm -> Point
// the unit vector (vecor of length 1) in same direction as self
}
class alwaysEqual { // a trait
method == (other) {
self.isMe(other)
}
}
class point2Dx (x') y (y') {
def x is readable = x'
def y is readable = y'
method asString { "({x}@{y})" }
method asDebugString { self.asString }
method distanceTo(other:Point) { (((x - other.x)^2) + ((y - other.y)^2))^(0.5) }
method -(other:Point) { point2Dx (x - other.x) y (y - other.y) }
method +(other:Point) { point2Dx (x + other.x) y (y + other.y) }
method /(other:Number) { point2Dx (x / other) y (y / other) }
method *(other:Number) { point2Dx (x * other) y (y * other) }
method length {((x^2) + (y^2))^0.5}
method ==(other) {
match (other)
case {o:Point -> (x == o.x) && (y == o.y)}
case {_ -> false}
}
method prefix- { point2Dx (-x) y (-y) }
method dot (other:Point) -> Number {
// dot product
(x * other.x) + (y * other.y)
}
method ⋅ (other:Point) -> Number {
// dot product
(x * other.x) + (y * other.y)
}
method reverseTimesNumber(n) { point2Dx (n * x) y (n * x) }
method reversePlusNumber(n) { point2Dx (n + x) y (n + x) }
method reverseDivideNumber(n) { point2Dx (n / x) y (n / x) }
method reverseMinusNumber(n) { point2Dx (n - x) y (n - x) }
method norm { self / self.length }
}
//
// useful types
//
type Block0⟦R⟧ = type {
apply -> R
}
type Block1⟦T,R⟧ = type {
apply(a:T) -> R
}
type Block2⟦S,T,R⟧ = type {
apply(a:S, b:T) -> R
}
type Block3⟦S,T,U,R⟧ = type {
apply(a:S, b:T, c:U) -> R
}
type Cmd = Block0⟦Done⟧
type Fun⟦T,R⟧ = Block1⟦T,R⟧
type Fun2⟦T,U,R⟧ = Block2⟦T,U,R⟧
type Fun3⟦T,U,W,R⟧ = Block3⟦T,U,W,R⟧
type Proc⟦T⟧ = Block1⟦T,Done⟧
type Proc2⟦T,U⟧ = Block2⟦T,U,Done⟧
type Proc3⟦T,U,W⟧ = Block3⟦T,U,W,Done⟧
type SelfType = Unknown // becuase it's not yet in the language
//
// start collections
//
type Iterable⟦T⟧ = Object & type {
iterator -> Iterator⟦T⟧
// the iterator on which I am based
isEmpty -> Boolean
// true if I have no elements
size -> Number
// my size (the number of elements that I contain);
// may raise SizeUnknown.
sizeIfUnknown(action: Block0⟦Number⟧)
// my size; if not known, then the result of applying action
first -> T
// my first element; raises BoundsError if I have none.
do(body: Block1⟦T, Done⟧) -> Done
// an internal iterator; applies body to each of my elements
do(body:Block1⟦T, Done⟧) separatedBy(separator:Block0⟦Done⟧) -> Done
// an internal iterator; applies body to each of my elements, and applies separator in between
++(other: Iterable⟦T⟧) -> Iterable⟦T⟧
// returns a new Iterable over the concatenation of self and other
fold(binaryFunction:Block2⟦T, T, T⟧) startingWith(initial:T) -> T
// the left-associative fold of binaryFunction over self, starting with initial
map⟦U⟧(function:Block1⟦T, U⟧) -> Iterable⟦U⟧
// returns a new iterator that yields my elements mapped by function
filter(condition:Block1⟦T,Boolean⟧) -> Iterable⟦T⟧
// returns a new iterator that yields those of my elements for which condition holds
}
type Expandable⟦T⟧ = Iterable⟦T⟧ & type {
add(x: T) -> SelfType
addAll(xs: Iterable⟦T⟧) -> SelfType
}
type Collection⟦T⟧ = Iterable⟦T⟧
type Enumerable⟦T⟧ = Collection⟦T⟧ & type {
values -> Collection⟦T⟧
asDictionary -> Dictionary⟦Number,T⟧
keysAndValuesDo(action:Block2⟦Number,T,Object⟧) -> Done
into(existing: Expandable⟦Unknown⟧) -> Collection⟦Unknown⟧
sortedBy(comparison:Block2⟦T,T,Number⟧) -> SelfType
sorted -> SelfType
}
type Sequence⟦T⟧ = Enumerable⟦T⟧ & type {
size -> Number
at(n:Number) -> T
indices -> Sequence⟦Number⟧
keys -> Sequence⟦Number⟧
second -> T
third -> T
fourth -> T
fifth -> T
last -> T
indexOf⟦W⟧(elem:T)ifAbsent(action:Block0⟦W⟧) -> Number | W
indexOf(elem:T) -> Number
contains(elem:T) -> Boolean
reversed -> Sequence⟦T⟧
}
type List⟦T⟧ = Sequence⟦T⟧ & type {
add(x: T) -> List⟦T⟧
addAll(xs: Iterable⟦T⟧) -> List⟦T⟧
addFirst(x: T) -> List⟦T⟧
addAllFirst(xs: Iterable⟦T⟧) -> List⟦T⟧
addLast(x: T) -> List⟦T⟧ // same as add
at(ix:Number) put(v:T) -> List⟦T⟧
clear -> List⟦T⟧
removeFirst -> T
removeAt(n: Number) -> T
removeLast -> T
remove(v:T)
remove(v:T) ifAbsent(action:Block0⟦Done⟧)
removeAll(vs: Iterable⟦T⟧)
removeAll(vs: Iterable⟦T⟧) ifAbsent(action:Block0⟦Unknown⟧)
pop -> T
++(o: List⟦T⟧) -> List⟦T⟧
addAll(l: Iterable⟦T⟧) -> List⟦T⟧
copy -> List⟦T⟧
sort -> List⟦T⟧
sortBy(sortBlock:Block2⟦T,T,Number⟧) -> List⟦T⟧
reverse -> List⟦T⟧
reversed -> List⟦T⟧
}
type Set⟦T⟧ = Collection⟦T⟧ & type {
size -> Number
add(x:T) -> SelfType
addAll(elements: Iterable⟦T⟧) -> SelfType
remove(x: T) -> Set⟦T⟧
remove(x: T) ifAbsent(block: Block0⟦Done⟧) -> Set⟦T⟧
clear -> Set⟦T⟧
includes(booleanBlock: Block1⟦T,Boolean⟧) -> Boolean
find(booleanBlock: Block1⟦T,Boolean⟧) ifNone(notFoundBlock: Block0⟦T⟧) -> T
copy -> Set⟦T⟧
contains(elem:T) -> Boolean
** (other:Set⟦T⟧) -> Set⟦T⟧
-- (other:Set⟦T⟧) -> Set⟦T⟧
++ (other:Set⟦T⟧) -> Set⟦T⟧
isSubset(s2: Set⟦T⟧) -> Boolean
isSuperset(s2: Iterable⟦T⟧) -> Boolean
removeAll(elems: Iterable⟦T⟧)
removeAll(elems: Iterable⟦T⟧)ifAbsent(action:Block0⟦Done⟧) -> Set⟦T⟧
into(existing: Expandable⟦Unknown⟧) -> Collection⟦Unknown⟧
}
type Dictionary⟦K,T⟧ = Collection⟦T⟧ & type {
size -> Number
containsKey(k:K) -> Boolean
containsValue(v:T) -> Boolean
contains(elem:T) -> Boolean
at(key:K)ifAbsent(action:Block0⟦Unknown⟧) -> Unknown
at(key:K)put(value:T) -> Dictionary⟦K,T⟧
at(k:K) -> T
removeAllKeys(keys: Iterable⟦K⟧) -> Dictionary⟦K,T⟧
removeKey(key:K) -> Dictionary⟦K,T⟧
removeAllValues(removals: Iterable⟦T⟧) -> Dictionary⟦K,T⟧
removeValue(v:T) -> Dictionary⟦K,T⟧
clear -> Dictionary⟦K,T⟧
keys -> Enumerable⟦K⟧
values -> Enumerable⟦T⟧
bindings -> Enumerable⟦Binding⟦K,T⟧⟧
keysAndValuesDo(action:Block2⟦K,T,Done⟧) -> Done
keysDo(action:Block1⟦K,Done⟧) -> Done
valuesDo(action:Block1⟦T,Done⟧) -> Done
== (other:Object) -> Boolean
copy -> Dictionary⟦K,T⟧
++ (other:Dictionary⟦K, T⟧) -> Dictionary⟦K, T⟧
-- (other:Dictionary⟦K, T⟧) -> Dictionary⟦K, T⟧
asDictionary -> Dictionary⟦K, T⟧
}
type Iterator⟦T⟧ = type {
hasNext -> Boolean
next -> T
}
class lazySequenceOver⟦T,R⟧ (source: Iterable⟦T⟧)
mappedBy (function:Block1⟦T,R⟧) -> Enumerable⟦R⟧ {
inherit traits.enumerable⟦T⟧
class iterator {
def sourceIterator = source.iterator
method asString { "an iterator over a lazy map sequence" }
method hasNext { sourceIterator.hasNext }
method next { function.apply(sourceIterator.next) }
}
method size { source.size }
method isEmpty { source.isEmpty }
method asDebugString { "a lazy sequence mapping over {source}" }
}
class lazySequenceOver⟦T⟧ (source: Iterable⟦T⟧)
filteredBy(predicate:Block1⟦T,Boolean⟧) -> Enumerable⟦T⟧ {
inherit traits.enumerable⟦T⟧
class iterator {
var cache
var cacheLoaded := false
def sourceIterator = source.iterator
method asString { "an iterator over filtered {source}" }
method hasNext {
// To determine if this iterator has a next element, we have to find
// an acceptable element; this is then cached, for the use of next
if (cacheLoaded) then { true } else { hasNextAcceptableElement }
}
method next {
if (cacheLoaded.not) then { cache := nextAcceptableElement }
cacheLoaded := false
return cache
}
method nextAcceptableElement is confidential {
// return the next element of the underlying iterator satisfying
// predicate; if there is none, raises IteratorExhausted.
while { true } do {
def outerNext = sourceIterator.next
def isAcceptable = predicate.apply(outerNext)
if (isAcceptable) then { return outerNext }
}
}
method hasNextAcceptableElement is confidential {
// returns true is there is another element in the underlying iterator
// satisfying predicate, otherwise false
while { true } do {
if (sourceIterator.hasNext.not) then { return false }
def outerNext = sourceIterator.next
def isAcceptable = predicate.apply(outerNext)
if (isAcceptable) then {
cacheLoaded := true
cache := outerNext
return true
}
}
}
}
method asDebugString { "a lazy sequence filtering {source}" }
}
class iteratorConcat⟦T⟧(left:Iterator⟦T⟧, right:Iterator⟦T⟧) {
method next {
if (left.hasNext) then {
left.next
} else {
right.next
}
}
method hasNext {
if (left.hasNext) then { return true }
return right.hasNext
}
method asDebugString { "iteratorConcat of {left} and {right}" }
method asString { "an iterator over a concatenation" }
}
class lazyConcatenation⟦T⟧(left, right) -> Enumerable⟦T⟧ {
inherit traits.enumerable⟦T⟧
method iterator {
iteratorConcat(left.iterator, right.iterator)
}
method asDebugString { "lazy concatenation of {left} and {right}" }
method size { left.size + right.size } // may raise SizeUnknown
}
def emptySequence is confidential = object {
inherit traits.indexable
method size { 0 }
method isEmpty { true }
method at(n) { BoundsError.raise "index {n} of empty sequence" }
method keys { self }
method values { self }
method keysAndValuesDo(block2) { done }
method reversed { self }
method ++(other: Iterable) { sequence.withAll(other) }
method asString { "⟨⟩" }
method contains(element) { false }
method do(block1) { done }
method ==(other) {
match (other)
case {o: Iterable ->
o.isEmpty
}
case {_ ->
false
}
}
class iterator {
method asString { "emptySequenceIterator" }
method hasNext { false }
method next { IteratorExhausted.raise "on empty sequence" }
}
method sorted { self }
method sortedBy(sortBlock:Block2){ self }
}
class sequence⟦T⟧ {
method asString { "a sequence factory" }
method empty {
// this is an optimization: there need be just one empty sequence
emptySequence
}
method withAll(arg: Iterable⟦T⟧) {
var sizeCertain := true
// size might be uncertain if arg is a lazy collection.
def forecastSize = arg.sizeIfUnknown {
sizeCertain := false
8
}
var inner := _prelude.primitiveArray.new(forecastSize)
var innerSize := inner.size
var ix := 0
if (sizeCertain) then {
// common, fast path
for (arg) do { elt ->
inner.at(ix)put(elt)
ix := ix + 1
}
} else {
// less-than-optimal path
for (arg) do { elt ->
if (innerSize <= ix) then {
def newInner = _prelude.primitiveArray.new(innerSize * 2)
for (0..(innerSize-1)) do { i ->
newInner.at(i)put(inner.at(i))
}
inner := newInner
innerSize := inner.size
}
inner.at(ix)put(elt)
ix := ix + 1
}
}
if (ix == 0) then { return emptySequence }
self.fromprimitiveArray(inner, ix)
}
method fromprimitiveArray(pArray, sz) is confidential {
// constructs a sequence from the first sz elements of pArray
object {
inherit traits.indexable⟦T⟧
def size is public = sz
def inner = pArray
method boundsCheck(n) is confidential {
if (!(n >= 1) || !(n <= size)) then {
// the condition is written this way because NaN always
// compares false
BoundsError.raise "index {n} out of bounds 1..{size}"
}
}
method at(n) {
boundsCheck(n)
inner.at(n-1)
}
method keys {
range.from(1)to(size)
}
method values {
self
}
method keysAndValuesDo(block2) {
var i := 0
while {i < size} do {
block2.apply(i+1, inner.at(i))
i := i + 1
}
}
method reversed {
def freshArray = _prelude.primitiveArray.new(size)
var ix := size - 1
do { each ->
freshArray.at (ix) put(each)
ix := ix - 1
}
outer.fromprimitiveArray(freshArray, size)
}
method ++(other: Iterable) {
sequence.withAll(lazyConcatenation(self, other))
}
method asString {
var s := "⟨"
for (0..(size-1)) do {i->
s := s ++ inner.at(i).asString
if (i < (size-1)) then { s := s ++ ", " }
}
s ++ "⟩"
}
method contains(element) {