smalltalk: SequenceableCollection methods (13) + String at:/copyFrom:to: + 28 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-25 10:58:08 +00:00
parent 0b5f3c180e
commit 3be722d5b6
4 changed files with 212 additions and 1 deletions

View File

@@ -402,6 +402,82 @@
(st-class-define! "Error" "Exception" (list))
(st-class-define! "ZeroDivide" "Error" (list))
(st-class-define! "MessageNotUnderstood" "Error" (list))
;; SequenceableCollection — shared iteration / inspection methods.
;; Defined on the parent class so Array, String, Symbol, and
;; OrderedCollection all inherit. Each method calls `self do:`,
;; which dispatches to the receiver's primitive do: implementation.
(st-class-add-method! "SequenceableCollection" "inject:into:"
(st-parse-method
"inject: initial into: aBlock
| acc |
acc := initial.
self do: [:e | acc := aBlock value: acc value: e].
^ acc"))
(st-class-add-method! "SequenceableCollection" "detect:"
(st-parse-method
"detect: aBlock
self do: [:e | (aBlock value: e) ifTrue: [^ e]].
^ nil"))
(st-class-add-method! "SequenceableCollection" "detect:ifNone:"
(st-parse-method
"detect: aBlock ifNone: noneBlock
self do: [:e | (aBlock value: e) ifTrue: [^ e]].
^ noneBlock value"))
(st-class-add-method! "SequenceableCollection" "count:"
(st-parse-method
"count: aBlock
| n |
n := 0.
self do: [:e | (aBlock value: e) ifTrue: [n := n + 1]].
^ n"))
(st-class-add-method! "SequenceableCollection" "allSatisfy:"
(st-parse-method
"allSatisfy: aBlock
self do: [:e | (aBlock value: e) ifFalse: [^ false]].
^ true"))
(st-class-add-method! "SequenceableCollection" "anySatisfy:"
(st-parse-method
"anySatisfy: aBlock
self do: [:e | (aBlock value: e) ifTrue: [^ true]].
^ false"))
(st-class-add-method! "SequenceableCollection" "includes:"
(st-parse-method
"includes: target
self do: [:e | e = target ifTrue: [^ true]].
^ false"))
(st-class-add-method! "SequenceableCollection" "do:separatedBy:"
(st-parse-method
"do: aBlock separatedBy: sepBlock
| first |
first := true.
self do: [:e |
first ifFalse: [sepBlock value].
first := false.
aBlock value: e].
^ self"))
(st-class-add-method! "SequenceableCollection" "indexOf:"
(st-parse-method
"indexOf: target
| idx |
idx := 1.
self do: [:e | e = target ifTrue: [^ idx]. idx := idx + 1].
^ 0"))
(st-class-add-method! "SequenceableCollection" "indexOf:ifAbsent:"
(st-parse-method
"indexOf: target ifAbsent: noneBlock
| idx |
idx := 1.
self do: [:e | e = target ifTrue: [^ idx]. idx := idx + 1].
^ noneBlock value"))
(st-class-add-method! "SequenceableCollection" "reject:"
(st-parse-method
"reject: aBlock ^ self select: [:e | (aBlock value: e) not]"))
(st-class-add-method! "SequenceableCollection" "isEmpty"
(st-parse-method "isEmpty ^ self size = 0"))
(st-class-add-method! "SequenceableCollection" "notEmpty"
(st-parse-method "notEmpty ^ self size > 0"))
(st-class-add-method! "SequenceableCollection" "asString"
(st-parse-method "asString ^ self printString"))
"ok")))
;; Initialise on load. Tests can re-bootstrap to reset state.