forked from amber-smalltalk/amber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kernel-Collections.st
1620 lines (1248 loc) · 27.5 KB
/
Kernel-Collections.st
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
Smalltalk current createPackage: 'Kernel-Collections' properties: #{}!
Object subclass: #Association
instanceVariableNames: 'key value'
package: 'Kernel-Collections'!
!Association methodsFor: 'accessing'!
key
^key
!
key: aKey
key := aKey
!
value
^value
!
value: aValue
value := aValue
! !
!Association methodsFor: 'comparing'!
= anAssociation
^self class = anAssociation class and: [
self key = anAssociation key and: [
self value = anAssociation value]]
! !
!Association methodsFor: 'printing'!
printString
"print the contents of the Association into a string and return the string"
^String streamContents: [:aStream |
self storeOn: aStream]
!
storeOn: aStream
"Store in the format: key->value"
key storeOn: aStream.
aStream nextPutAll: '->'.
value storeOn: aStream.
! !
!Association class methodsFor: 'instance creation'!
key: aKey value: aValue
^self new
key: aKey;
value: aValue;
yourself
! !
Object subclass: #Collection
instanceVariableNames: ''
package: 'Kernel-Collections'!
!Collection methodsFor: 'accessing'!
readStream
^self stream
!
size
self subclassResponsibility
!
stream
^self streamClass on: self
!
streamClass
^self class streamClass
!
writeStream
^self stream
! !
!Collection methodsFor: 'adding/removing'!
add: anObject
self subclassResponsibility
!
addAll: aCollection
aCollection do: [:each |
self add: each].
^aCollection
!
remove: anObject
^self remove: anObject ifAbsent: [self errorNotFound]
!
remove: anObject ifAbsent: aBlock
self subclassResponsibility
! !
!Collection methodsFor: 'converting'!
asArray
^Array withAll: self
!
asJSON
^self asArray collect: [:each | each asJSON]
!
asOrderedCollection
^self asArray
!
asSet
^Set withAll: self
! !
!Collection methodsFor: 'copying'!
, aCollection
^self copy
addAll: aCollection;
yourself
!
copyWith: anObject
^self copy add: anObject; yourself
!
copyWithAll: aCollection
^self copy addAll: aCollection; yourself
!
copyWithoutAll: aCollection
"Answer a copy of the receiver that does not contain any elements
equal to those in aCollection."
^ self reject: [:each | aCollection includes: each]
! !
!Collection methodsFor: 'enumerating'!
collect: aBlock
| newCollection |
newCollection := self class new.
self do: [:each |
newCollection add: (aBlock value: each)].
^newCollection
!
detect: aBlock
^self detect: aBlock ifNone: [self errorNotFound]
!
detect: aBlock ifNone: anotherBlock
<
for(var i = 0; i < self.length; i++)
if(aBlock(self[i]))
return self[i];
return anotherBlock();
>
!
do: aBlock
<for(var i=0;i<self.length;i++){aBlock(self[i]);}>
!
do: aBlock separatedBy: anotherBlock
| first |
first := true.
self do: [:each |
first
ifTrue: [first := false]
ifFalse: [anotherBlock value].
aBlock value: each]
!
inject: anObject into: aBlock
| result |
result := anObject.
self do: [:each |
result := aBlock value: result value: each].
^result
!
reject: aBlock
^self select: [:each | (aBlock value: each) = false]
!
select: aBlock
| stream |
stream := self class new writeStream.
self do: [:each |
(aBlock value: each) ifTrue: [
stream nextPut: each]].
^stream contents
! !
!Collection methodsFor: 'error handling'!
errorNotFound
self error: 'Object is not in the collection'
! !
!Collection methodsFor: 'printing'!
printString
"print the contents of the Collection into a string and return it"
^String streamContents: [:aStream |
aStream
nextPutAll: super printString, ' ('.
self do: [:each | aStream nextPutAll: each printString]
separatedBy: [aStream nextPutAll: ' '].
aStream nextPutAll: ')'.]
! !
!Collection methodsFor: 'testing'!
ifEmpty: aBlock
"Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver. Note that the fact that this method returns its argument in case the receiver is not empty allows one to write expressions like the following ones: self classifyMethodAs:
(myProtocol ifEmpty: ['As yet unclassified'])"
^ self isEmpty
ifTrue: [ aBlock value ]
ifFalse: [ self ]
!
ifNotEmpty: aBlock
self notEmpty ifTrue: aBlock.
!
includes: anObject
<
var i = self.length;
while (i--) {
if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
}
return false
>
!
isEmpty
^self size = 0
!
notEmpty
^self isEmpty not
! !
!Collection class methodsFor: 'accessing'!
streamClass
^Stream
! !
!Collection class methodsFor: 'instance creation'!
new: anInteger
^self new
!
with: anObject
^self new
add: anObject;
yourself
!
with: anObject with: anotherObject
^self new
add: anObject;
add: anotherObject;
yourself
!
with: firstObject with: secondObject with: thirdObject
^self new
add: firstObject;
add: secondObject;
add: thirdObject;
yourself
!
withAll: aCollection
^self new
addAll: aCollection;
yourself
! !
Collection subclass: #HashedCollection
instanceVariableNames: ''
package: 'Kernel-Collections'!
!HashedCollection commentStamp!
A HashedCollection is a traditional JavaScript object, or a Smalltalk Dictionary.
Unlike a Dictionary, it can only have strings as keys.!
!HashedCollection methodsFor: 'accessing'!
associations
| associations |
associations := #().
self keys do: [:each |
associations add: (Association key: each value: (self at: each))].
^associations
!
at: aKey
^self at: aKey ifAbsent: [self errorNotFound]
!
at: aKey ifAbsent: aBlock
^(self includesKey: aKey)
ifTrue: [self basicAt: aKey]
ifFalse: aBlock
!
at: aKey ifAbsentPut: aBlock
^self at: aKey ifAbsent: [
self at: aKey put: aBlock value]
!
at: aKey ifPresent: aBlock
"Lookup the given key in the receiver.
If it is present, answer the value of evaluating the given block with the value associated with the key.
Otherwise, answer nil."
^(self includesKey: aKey)
ifTrue: [ aBlock value: (self at: aKey) ]
ifFalse: [ nil ]
!
at: aKey ifPresent: aBlock ifAbsent: anotherBlock
"Lookup the given key in the receiver.
If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
otherwise answer the value of absentBlock."
^(self includesKey: aKey)
ifTrue: [ aBlock value: (self at: aKey) ]
ifFalse: anotherBlock
!
at: aKey put: aValue
^self basicAt: aKey put: aValue
!
keys
<
if ('function'===typeof Object.keys) return Object.keys(self);
var keys = [];
for(var i in self) {
if(self.hasOwnProperty(i)) {
keys.push(i);
}
};
return keys;
>
!
size
^self keys size
!
values
^self keys collect: [:each | self at: each]
! !
!HashedCollection methodsFor: 'adding/removing'!
add: anAssociation
self at: anAssociation key put: anAssociation value
!
addAll: aHashedCollection
super addAll: aHashedCollection associations.
^aHashedCollection
!
remove: aKey ifAbsent: aBlock
^self removeKey: aKey ifAbsent: aBlock
!
removeKey: aKey
^self remove: aKey
!
removeKey: aKey ifAbsent: aBlock
^(self includesKey: aKey)
ifFalse: [aBlock value]
ifTrue: [self basicDelete: aKey]
! !
!HashedCollection methodsFor: 'comparing'!
= aHashedCollection
self class = aHashedCollection class ifFalse: [^false].
self size = aHashedCollection size ifFalse: [^false].
^self associations = aHashedCollection associations
! !
!HashedCollection methodsFor: 'converting'!
asDictionary
^Dictionary fromPairs: self associations
!
asJSON
| c |
c := self class new.
self keysAndValuesDo: [:key :value |
c at: key put: value asJSON].
^c
! !
!HashedCollection methodsFor: 'copying'!
, aCollection
self shouldNotImplement
!
copyFrom: anIndex to: anotherIndex
self shouldNotImplement
!
deepCopy
| copy |
copy := self class new.
self associationsDo: [:each |
copy at: each key put: each value deepCopy].
^copy
!
shallowCopy
| copy |
copy := self class new.
self associationsDo: [:each |
copy at: each key put: each value].
^copy
! !
!HashedCollection methodsFor: 'enumerating'!
associationsDo: aBlock
self associations do: aBlock
!
collect: aBlock
| newDict |
newDict := self class new.
self keysAndValuesDo: [:key :value |
newDict at: key put: (aBlock value: value)].
^newDict
!
detect: aBlock ifNone: anotherBlock
^self values detect: aBlock ifNone: anotherBlock
!
do: aBlock
self values do: aBlock
!
includes: anObject
^self values includes: anObject
!
keysAndValuesDo: aBlock
self associationsDo: [:each |
aBlock value: each key value: each value]
!
select: aBlock
| newDict |
newDict := self class new.
self keysAndValuesDo: [:key :value |
(aBlock value: value) ifTrue: [newDict at: key put: value]].
^newDict
! !
!HashedCollection methodsFor: 'printing'!
printString
"print the contents of the HashedCollection into a string and return the string"
^String streamContents: [:aStream |
aStream nextPutAll: 'a ', self class name, '('.
self associations
do: [:each | each storeOn: aStream]
separatedBy: [ aStream nextPutAll: ' , '].
aStream nextPutAll: ')']
!
storeOn: aStream
aStream nextPutAll: '#{'.
self associations
do: [:each | each storeOn: aStream]
separatedBy: [ aStream nextPutAll: '. '].
aStream nextPutAll: '}'
! !
!HashedCollection methodsFor: 'testing'!
includesKey: aKey
<return self.hasOwnProperty(aKey)>
! !
!HashedCollection class methodsFor: 'instance creation'!
fromPairs: aCollection
| dict |
dict := self new.
aCollection do: [:each | dict add: each].
^dict
! !
HashedCollection subclass: #Dictionary
instanceVariableNames: 'keys values'
package: 'Kernel-Collections'!
!Dictionary methodsFor: 'accessing'!
at: aKey ifAbsent: aBlock
<
var index;
for(var i=0;i<self['@keys'].length;i++){
if(self['@keys'][i].__eq(aKey)) {index = i;}
};
if(typeof index === 'undefined') {
return aBlock();
} else {
return self['@values'][index];
}
>
!
at: aKey put: aValue
<
var index = self['@keys'].indexOf(aKey);
if(index === -1) {
self['@values'].push(aValue);
self['@keys'].push(aKey);
} else {
self['@values'][index] = aValue;
};
return aValue;
>
!
keys
^keys copy
!
values
^values copy
! !
!Dictionary methodsFor: 'adding/removing'!
removeKey: aKey ifAbsent: aBlock
<
var index = self['@keys'].indexOf(aKey);
if(index === -1) {
return aBlock()
} else {
var value;
self['@keys'].splice(index, 1);
value = self['@values'].splice(index, 1);
return value[0];
};
>
! !
!Dictionary methodsFor: 'converting'!
asHashedCollection
^HashedCollection fromPairs: self associations
!
asJSON
^self asHashedCollection asJSON
! !
!Dictionary methodsFor: 'initialization'!
initialize
super initialize.
keys := #().
values := #()
! !
!Dictionary methodsFor: 'testing'!
includesKey: aKey
^keys includes: aKey
! !
Collection subclass: #SequenceableCollection
instanceVariableNames: ''
package: 'Kernel-Collections'!
!SequenceableCollection methodsFor: 'accessing'!
allButFirst
^self copyFrom: 2 to: self size
!
allButLast
^self copyFrom: 1 to: self size - 1
!
at: anIndex
^self at: anIndex ifAbsent: [
self errorNotFound]
!
at: anIndex ifAbsent: aBlock
self subclassResponsibility
!
at: anIndex put: anObject
self subclassResponsibility
!
atRandom
^ self at: self size atRandom
!
first
^self at: 1
!
first: n
"Answer the first n elements of the receiver.
Raise an error if there are not enough elements."
^ self copyFrom: 1 to: n
!
fourth
^self at: 4
!
indexOf: anObject
^self indexOf: anObject ifAbsent: [self errorNotFound]
!
indexOf: anObject ifAbsent: aBlock
<
for(var i=0;i<self.length;i++){
if(self[i].__eq(anObject)) {return i+1}
}
return aBlock();
>
!
indexOf: anObject startingAt: start
"Answer the index of the first occurence of anElement after start
within the receiver. If the receiver does not contain anElement,
answer 0."
^self indexOf: anObject startingAt: start ifAbsent: [0]
!
indexOf: anObject startingAt: start ifAbsent: aBlock
<
for(var i=start-1;i<self.length;i++){
if(self[i].__eq(anObject)) {return i+1}
}
return aBlock();
>
!
last
^self at: self size
!
second
^self at: 2
!
third
^self at: 3
! !
!SequenceableCollection methodsFor: 'adding'!
addLast: anObject
self add: anObject
!
removeLast
self remove: self last
! !
!SequenceableCollection methodsFor: 'comparing'!
= aCollection
(self class = aCollection class and: [
self size = aCollection size]) ifFalse: [^false].
self withIndexDo: [:each :i |
(aCollection at: i) = each ifFalse: [^false]].
^true
! !
!SequenceableCollection methodsFor: 'converting'!
reversed
self subclassResponsibility
! !
!SequenceableCollection methodsFor: 'copying'!
copyFrom: anIndex to: anotherIndex
| range newCollection |
range := anIndex to: anotherIndex.
newCollection := self class new: range size.
range withIndexDo: [:each :i |
newCollection at: i put: (self at: each)].
^newCollection
!
deepCopy
| newCollection |
newCollection := self class new: self size.
self withIndexDo: [:each :index |
newCollection at: index put: each deepCopy].
^newCollection
!
shallowCopy
| newCollection |
newCollection := self class new: self size.
self withIndexDo: [ :each :index |
newCollection at: index put: each].
^newCollection
! !
!SequenceableCollection methodsFor: 'enumerating'!
withIndexDo: aBlock
<for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
! !
SequenceableCollection subclass: #Array
instanceVariableNames: ''
package: 'Kernel-Collections'!
!Array methodsFor: 'accessing'!
at: anIndex ifAbsent: aBlock
<
if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
return self[anIndex - 1];
>
!
at: anIndex put: anObject
<return self[anIndex - 1] = anObject>
!
size
<return self.length>
! !
!Array methodsFor: 'adding/removing'!
add: anObject
<self.push(anObject); return anObject;>
!
remove: anObject
<
for(var i=0;i<self.length;i++) {
if(self[i] == anObject) {
self.splice(i,1);
break;
}
}
>
!
removeFrom: aNumber to: anotherNumber
<self.splice(aNumber - 1,anotherNumber - 1)>
! !
!Array methodsFor: 'converting'!
asJavascript
^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
!
reversed
<return self._copy().reverse()>
! !
!Array methodsFor: 'enumerating'!
join: aString
<return self.join(aString)>
!
sort
^self basicPerform: 'sort'
!
sort: aBlock
<
return self.sort(function(a, b) {
if(aBlock(a,b)) {return -1} else {return 1}
})
>
!
sorted
^self copy sort
!
sorted: aBlock
^self copy sort: aBlock
! !
!Array class methodsFor: 'instance creation'!
new: anInteger
<return new Array(anInteger)>
!
with: anObject
^(self new: 1)
at: 1 put: anObject;
yourself
!
with: anObject with: anObject2
^(self new: 2)
at: 1 put: anObject;
at: 2 put: anObject2;
yourself
!
with: anObject with: anObject2 with: anObject3
^(self new: 3)
at: 1 put: anObject;
at: 2 put: anObject2;
at: 3 put: anObject3;
yourself
!
withAll: aCollection
| instance |
instance := self new: aCollection size.
aCollection withIndexDo: [:each :index |
instance at: index put: each].
^instance
! !
SequenceableCollection subclass: #CharacterArray
instanceVariableNames: ''
package: 'Kernel-Collections'!
!CharacterArray methodsFor: 'accessing'!
at: anIndex put: anObject
self errorReadOnly
! !
!CharacterArray methodsFor: 'adding'!
add: anObject
self errorReadOnly
!
remove: anObject
self errorReadOnly
! !
!CharacterArray methodsFor: 'converting'!
asLowercase
^self class fromString: self asString asLowercase
!
asNumber
^self asString asNumber
!
asString
^self subclassResponsibility
!
asSymbol
^self subclassResponsibility
!
asUppercase
^self class fromString: self asString asUppercase
! !
!CharacterArray methodsFor: 'copying'!
, aString
^self asString, aString asString
! !
!CharacterArray methodsFor: 'error handling'!
errorReadOnly
self error: 'Object is read-only'
! !
!CharacterArray methodsFor: 'printing'!
printString
^self asString printString
! !
!CharacterArray class methodsFor: 'instance creation'!
fromString: aString
self subclassResponsibility
! !
CharacterArray subclass: #String
instanceVariableNames: ''
package: 'Kernel-Collections'!
!String methodsFor: 'accessing'!
asciiValue
<return self.charCodeAt(0);>
!
at: anIndex ifAbsent: aBlock
<return String(self).charAt(anIndex - 1) || aBlock()>
!
escaped
<return escape(self)>
!
size
<return self.length>
!
unescaped
<return unescape(self)>
! !
!String methodsFor: 'comparing'!
< aString
<return String(self) < aString._asString()>
!
<= aString
<return String(self) <= aString._asString()>
!
= aString
aString class = self class ifFalse: [^false].
<return String(self) === String(aString)>
!
== aString
^self = aString
!
> aString
<return String(self) >> aString._asString()>
!
>= aString
<return String(self) >>= aString._asString()>
! !
!String methodsFor: 'converting'!
asJSON
^self
!
asJavaScriptSelector
^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
!
asJavascript
<
if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
else
return "\"" + self + "\"";
>
!
asLowercase
<return self.toLowerCase()>
!
asNumber
<return Number(self)>
!