Revert "Revert "Made use of ::= in core libraries and defined a RebindError condition...
[cslatevm.git] / src / lib / repetition.slate
blob27f6cf3f7f02abf2e90b1dd1b9403ffb327dce73
1 define: #Repetition &parents: {Sequence}
2 "A Repetition is a Sequence of the same element repeated over its length."
3   &slots: {#number. "The number of times the element occurs."
4            #element. "The repeated element." }.
6 r@(Repetition traits) new &capacity: n
7 [r clone `setting: #{#number. #element} to: {n. Nil}].
9 r@(Repetition traits) newFor: obj sized: n
10 [r clone `setting: #{#number. #element} to: {n. obj}].
12 obj@(Root traits) repeatedTimes: n
13 "Convenient syntax to instantiate it."
14 [Repetition newFor: obj sized: n].
16 r@(Repetition traits) size
17 [r number].
19 r@(Repetition traits) at: index ifAbsent: block
20 [(index between: 0 and: r number) ifTrue: [r element] ifFalse: [block do]].
22 r@(Repetition traits) at: index put: obj
24   r element
25     ifNil: [r element := obj]
26     ifNotNil: [error: 'An attempt was made to modify a Repetition.']
29 r@(Repetition traits) do: block
30 [r number timesRepeat: [block applyWith: r element]].
32 r@(Repetition traits) collect: block
33 [r collect: block into: (Array newSizeOf: r)].
35 c@(Repetition traits) printContentsOn: o@(PrettyPrinterMixin traits) separatedBy: block
36 "Print out the element and occurrences like a faux multiplication."
38   c element printOn: o.
39   o ; ' x '.
40   c number printOn: o.
41   o