Kernel-Collections.st 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. Smalltalk current createPackage: 'Kernel-Collections' properties: #{}!
  2. Object subclass: #Association
  3. instanceVariableNames: 'key value'
  4. package: 'Kernel-Collections'!
  5. !Association methodsFor: 'accessing'!
  6. key
  7. ^key
  8. !
  9. key: aKey
  10. key := aKey
  11. !
  12. value
  13. ^value
  14. !
  15. value: aValue
  16. value := aValue
  17. ! !
  18. !Association methodsFor: 'comparing'!
  19. = anAssociation
  20. ^self class = anAssociation class and: [
  21. self key = anAssociation key and: [
  22. self value = anAssociation value]]
  23. ! !
  24. !Association methodsFor: 'printing'!
  25. printString
  26. "print the contents of the Association into a string and return the string"
  27. ^String streamContents: [:aStream |
  28. self storeOn: aStream]
  29. !
  30. storeOn: aStream
  31. "Store in the format: key->value"
  32. key storeOn: aStream.
  33. aStream nextPutAll: '->'.
  34. value storeOn: aStream.
  35. ! !
  36. !Association class methodsFor: 'instance creation'!
  37. key: aKey value: aValue
  38. ^self new
  39. key: aKey;
  40. value: aValue;
  41. yourself
  42. ! !
  43. Object subclass: #Collection
  44. instanceVariableNames: ''
  45. package: 'Kernel-Collections'!
  46. !Collection methodsFor: 'accessing'!
  47. occurrencesOf: anObject
  48. "Answer how many of the receiver's elements are equal to anObject."
  49. | tally |
  50. tally := 0.
  51. self do: [:each | anObject = each ifTrue: [tally := tally + 1]].
  52. ^tally
  53. !
  54. readStream
  55. ^self stream
  56. !
  57. size
  58. self subclassResponsibility
  59. !
  60. stream
  61. ^self streamClass on: self
  62. !
  63. streamClass
  64. ^self class streamClass
  65. !
  66. writeStream
  67. ^self stream
  68. ! !
  69. !Collection methodsFor: 'adding/removing'!
  70. add: anObject
  71. self subclassResponsibility
  72. !
  73. addAll: aCollection
  74. aCollection do: [:each |
  75. self add: each].
  76. ^aCollection
  77. !
  78. remove: anObject
  79. ^self remove: anObject ifAbsent: [self errorNotFound]
  80. !
  81. remove: anObject ifAbsent: aBlock
  82. self subclassResponsibility
  83. ! !
  84. !Collection methodsFor: 'converting'!
  85. asArray
  86. ^Array withAll: self
  87. !
  88. asJSON
  89. ^self asArray collect: [:each | each asJSON]
  90. !
  91. asOrderedCollection
  92. ^self asArray
  93. !
  94. asSet
  95. ^Set withAll: self
  96. ! !
  97. !Collection methodsFor: 'copying'!
  98. , aCollection
  99. ^self copy
  100. addAll: aCollection;
  101. yourself
  102. !
  103. copyWith: anObject
  104. ^self copy add: anObject; yourself
  105. !
  106. copyWithAll: aCollection
  107. ^self copy addAll: aCollection; yourself
  108. !
  109. copyWithoutAll: aCollection
  110. "Answer a copy of the receiver that does not contain any elements
  111. equal to those in aCollection."
  112. ^ self reject: [:each | aCollection includes: each]
  113. ! !
  114. !Collection methodsFor: 'enumerating'!
  115. collect: aBlock
  116. | stream |
  117. stream := self class new writeStream.
  118. self do: [ :each |
  119. stream nextPut: (aBlock value: each) ].
  120. ^stream contents
  121. !
  122. detect: aBlock
  123. ^self detect: aBlock ifNone: [self errorNotFound]
  124. !
  125. detect: aBlock ifNone: anotherBlock
  126. self subclassResponsibility
  127. !
  128. do: aBlock
  129. self subclassResponsibility
  130. !
  131. do: aBlock separatedBy: anotherBlock
  132. | actionBeforeElement |
  133. actionBeforeElement := [actionBeforeElement := anotherBlock].
  134. self do: [:each |
  135. actionBeforeElement value.
  136. aBlock value: each]
  137. !
  138. inject: anObject into: aBlock
  139. | result |
  140. result := anObject.
  141. self do: [:each |
  142. result := aBlock value: result value: each].
  143. ^result
  144. !
  145. intersection: aCollection
  146. "Answer the set theoretic intersection of two collections."
  147. | set outputSet |
  148. set := self asSet.
  149. outputSet := Set new.
  150. aCollection do: [ :each |
  151. ((set includes: each) and: [(outputSet includes: each) not])
  152. ifTrue: [
  153. outputSet add: each]].
  154. ^ self class withAll: outputSet asArray
  155. !
  156. reject: aBlock
  157. ^self select: [:each | (aBlock value: each) = false]
  158. !
  159. select: aBlock
  160. | stream |
  161. stream := self class new writeStream.
  162. self do: [:each |
  163. (aBlock value: each) ifTrue: [
  164. stream nextPut: each]].
  165. ^stream contents
  166. ! !
  167. !Collection methodsFor: 'error handling'!
  168. errorNotFound
  169. self error: 'Object is not in the collection'
  170. ! !
  171. !Collection methodsFor: 'printing'!
  172. printString
  173. "print the contents of the Collection into a string and return it"
  174. ^String streamContents: [:aStream |
  175. aStream
  176. nextPutAll: super printString, ' ('.
  177. self do: [:each | aStream nextPutAll: each printString]
  178. separatedBy: [aStream nextPutAll: ' '].
  179. aStream nextPutAll: ')']
  180. ! !
  181. !Collection methodsFor: 'testing'!
  182. ifEmpty: aBlock
  183. "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:
  184. (myProtocol ifEmpty: ['As yet unclassified'])"
  185. ^ self isEmpty
  186. ifTrue: [ aBlock value ]
  187. ifFalse: [ self ]
  188. !
  189. ifNotEmpty: aBlock
  190. self notEmpty ifTrue: aBlock.
  191. !
  192. includes: anObject
  193. | sentinel |
  194. sentinel := Object new.
  195. ^(self detect: [ :each | each = anObject] ifNone: [ sentinel ]) ~= sentinel
  196. !
  197. isEmpty
  198. ^self size = 0
  199. !
  200. notEmpty
  201. ^self isEmpty not
  202. ! !
  203. !Collection class methodsFor: 'accessing'!
  204. streamClass
  205. ^Stream
  206. ! !
  207. !Collection class methodsFor: 'instance creation'!
  208. new: anInteger
  209. ^self new
  210. !
  211. with: anObject
  212. ^self new
  213. add: anObject;
  214. yourself
  215. !
  216. with: anObject with: anotherObject
  217. ^self new
  218. add: anObject;
  219. add: anotherObject;
  220. yourself
  221. !
  222. with: firstObject with: secondObject with: thirdObject
  223. ^self new
  224. add: firstObject;
  225. add: secondObject;
  226. add: thirdObject;
  227. yourself
  228. !
  229. withAll: aCollection
  230. ^self new
  231. addAll: aCollection;
  232. yourself
  233. ! !
  234. Collection subclass: #IndexableCollection
  235. instanceVariableNames: ''
  236. package: 'Kernel-Collections'!
  237. !IndexableCollection commentStamp!
  238. An IndexableCollection is a key-value store, that is,
  239. it stores values under indexes.
  240. As a rule of thumb, if a collection has at: and at:put:,
  241. it is an IndexableCollection.!
  242. !IndexableCollection methodsFor: 'accessing'!
  243. at: anIndex
  244. "Lookup the given index in the receiver.
  245. If it is present, answer the value stored at anIndex.
  246. Otherwise, raise an error."
  247. ^self at: anIndex ifAbsent: [ self errorNotFound ]
  248. !
  249. at: anIndex ifAbsent: aBlock
  250. "Lookup the given index in the receiver.
  251. If it is present, answer the value stored at anIndex.
  252. Otherwise, answer the value of aBlock."
  253. self subclassReponsibility
  254. !
  255. at: anIndex ifPresent: aBlock
  256. "Lookup the given index in the receiver.
  257. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  258. Otherwise, answer nil."
  259. ^self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  260. !
  261. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  262. "Lookup the given index in the receiver.
  263. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  264. Otherwise, answer the value of anotherBlock."
  265. self subclassReponsibility
  266. !
  267. at: anIndex put: anObject
  268. "Store anObject under the given index in the receiver."
  269. self subclassReponsibility
  270. ! !
  271. !IndexableCollection methodsFor: 'enumeration'!
  272. with: anotherCollection do: aBlock
  273. "Calls aBlock with every value from self
  274. and with indetically-indexed value from anotherCollection"
  275. self withIndexDo: [ :each :index |
  276. aBlock value: each value: (anotherCollection at: index) ]
  277. !
  278. withIndexDo: aBlock
  279. "Calls aBlock with every value from self
  280. and with its index as the second argument"
  281. self subclassReponsibility
  282. ! !
  283. IndexableCollection subclass: #HashedCollection
  284. instanceVariableNames: ''
  285. package: 'Kernel-Collections'!
  286. !HashedCollection commentStamp!
  287. A HashedCollection is a traditional JavaScript object, or a Smalltalk Dictionary.
  288. Unlike a Dictionary, it can only have strings as keys.!
  289. !HashedCollection methodsFor: 'accessing'!
  290. associations
  291. | associations |
  292. associations := #().
  293. self associationsDo: [:each | associations add: each].
  294. ^associations
  295. !
  296. at: aKey ifAbsent: aBlock
  297. ^(self includesKey: aKey)
  298. ifTrue: [self basicAt: aKey]
  299. ifFalse: aBlock
  300. !
  301. at: aKey ifAbsentPut: aBlock
  302. ^self at: aKey ifAbsent: [
  303. self at: aKey put: aBlock value]
  304. !
  305. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  306. "Lookup the given key in the receiver.
  307. If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
  308. otherwise answer the value of absentBlock."
  309. ^(self includesKey: aKey)
  310. ifTrue: [ aBlock value: (self at: aKey) ]
  311. ifFalse: anotherBlock
  312. !
  313. at: aKey put: aValue
  314. ^self basicAt: aKey put: aValue
  315. !
  316. keys
  317. <
  318. if ('function'===typeof Object.keys) return Object.keys(self);
  319. var keys = [];
  320. for(var i in self) {
  321. if(self.hasOwnProperty(i)) {
  322. keys.push(i);
  323. }
  324. };
  325. return keys;
  326. >
  327. !
  328. size
  329. ^self keys size
  330. !
  331. values
  332. ^self keys collect: [:each | self at: each]
  333. ! !
  334. !HashedCollection methodsFor: 'adding/removing'!
  335. add: anAssociation
  336. self at: anAssociation key put: anAssociation value
  337. !
  338. addAll: aHashedCollection
  339. super addAll: aHashedCollection associations.
  340. ^aHashedCollection
  341. !
  342. remove: aKey ifAbsent: aBlock
  343. ^self removeKey: aKey ifAbsent: aBlock
  344. !
  345. removeKey: aKey
  346. ^self remove: aKey
  347. !
  348. removeKey: aKey ifAbsent: aBlock
  349. ^(self includesKey: aKey)
  350. ifFalse: [aBlock value]
  351. ifTrue: [self basicDelete: aKey]
  352. ! !
  353. !HashedCollection methodsFor: 'comparing'!
  354. = aHashedCollection
  355. self class = aHashedCollection class ifFalse: [^false].
  356. self size = aHashedCollection size ifFalse: [^false].
  357. ^self associations = aHashedCollection associations
  358. ! !
  359. !HashedCollection methodsFor: 'converting'!
  360. asDictionary
  361. ^Dictionary fromPairs: self associations
  362. !
  363. asJSON
  364. | c |
  365. c := self class new.
  366. self keysAndValuesDo: [:key :value |
  367. c at: key put: value asJSON].
  368. ^c
  369. ! !
  370. !HashedCollection methodsFor: 'copying'!
  371. , aCollection
  372. self shouldNotImplement
  373. !
  374. deepCopy
  375. | copy |
  376. copy := self class new.
  377. self keysAndValuesDo: [:key :value |
  378. copy at: key put: value deepCopy].
  379. ^copy
  380. !
  381. shallowCopy
  382. | copy |
  383. copy := self class new.
  384. self keysAndValuesDo: [:key :value |
  385. copy at: key put: value].
  386. ^copy
  387. ! !
  388. !HashedCollection methodsFor: 'enumerating'!
  389. associationsDo: aBlock
  390. self keysAndValuesDo: [:key :value |
  391. aBlock value: (Association key: key value: value)]
  392. !
  393. collect: aBlock
  394. | newDict |
  395. newDict := self class new.
  396. self keysAndValuesDo: [:key :value |
  397. newDict at: key put: (aBlock value: value)].
  398. ^newDict
  399. !
  400. detect: aBlock ifNone: anotherBlock
  401. ^self values detect: aBlock ifNone: anotherBlock
  402. !
  403. do: aBlock
  404. self valuesDo: aBlock
  405. !
  406. includes: anObject
  407. ^self values includes: anObject
  408. !
  409. keysAndValuesDo: aBlock
  410. self keysDo: [:each |
  411. aBlock value: each value: (self at: each)]
  412. !
  413. keysDo: aBlock
  414. self keys do: aBlock
  415. !
  416. select: aBlock
  417. | newDict |
  418. newDict := self class new.
  419. self keysAndValuesDo: [:key :value |
  420. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  421. ^newDict
  422. !
  423. valuesDo: aBlock
  424. self keysAndValuesDo: [ :key :value | aBlock value: value ]
  425. !
  426. withIndexDo: aBlock
  427. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  428. ! !
  429. !HashedCollection methodsFor: 'printing'!
  430. printString
  431. "print the contents of the HashedCollection into a string and return the string"
  432. ^String streamContents: [:aStream |
  433. aStream nextPutAll: 'a ', self class name, '('.
  434. self associations
  435. do: [:each | each storeOn: aStream]
  436. separatedBy: [ aStream nextPutAll: ' , '].
  437. aStream nextPutAll: ')']
  438. !
  439. storeOn: aStream
  440. aStream nextPutAll: '#{'.
  441. self associations
  442. do: [:each | each storeOn: aStream]
  443. separatedBy: [ aStream nextPutAll: '. '].
  444. aStream nextPutAll: '}'
  445. ! !
  446. !HashedCollection methodsFor: 'testing'!
  447. includesKey: aKey
  448. <return self.hasOwnProperty(aKey)>
  449. ! !
  450. !HashedCollection class methodsFor: 'instance creation'!
  451. fromPairs: aCollection
  452. | dict |
  453. dict := self new.
  454. aCollection do: [:each | dict add: each].
  455. ^dict
  456. ! !
  457. HashedCollection subclass: #Dictionary
  458. instanceVariableNames: 'keys values'
  459. package: 'Kernel-Collections'!
  460. !Dictionary methodsFor: 'accessing'!
  461. at: aKey ifAbsent: aBlock
  462. <
  463. var index = self._positionOfKey_(aKey);
  464. return index >>=0 ? self['@values'][index] : aBlock();
  465. >
  466. !
  467. at: aKey put: aValue
  468. <
  469. var index = self._positionOfKey_(aKey);
  470. if(index === -1) {
  471. var keys = self['@keys'];
  472. index = keys.length;
  473. keys.push(aKey);
  474. }
  475. return self['@values'][index] = aValue;
  476. >
  477. !
  478. keyAtValue: anObject
  479. ^ (self associations
  480. detect:[:k :v| v == anObject]
  481. ifNone:[self error: 'Not found']) key
  482. !
  483. keys
  484. ^keys copy
  485. !
  486. values
  487. ^values copy
  488. ! !
  489. !Dictionary methodsFor: 'adding/removing'!
  490. removeKey: aKey ifAbsent: aBlock
  491. <
  492. var index = self._positionOfKey_(aKey);
  493. if(index === -1) {
  494. return aBlock()
  495. } else {
  496. var keys = self['@keys'], values = self['@values'];
  497. var value = values[index], l = keys.length;
  498. keys[index] = keys[l-1];
  499. keys.pop();
  500. values[index] = values[l-1];
  501. values.pop();
  502. return value;
  503. }
  504. >
  505. ! !
  506. !Dictionary methodsFor: 'converting'!
  507. asHashedCollection
  508. ^HashedCollection fromPairs: self associations
  509. !
  510. asJSON
  511. ^self asHashedCollection asJSON
  512. ! !
  513. !Dictionary methodsFor: 'enumerating'!
  514. keysAndValuesDo: aBlock
  515. ^keys with: values do: aBlock
  516. !
  517. keysDo: aBlock
  518. ^keys do: aBlock
  519. !
  520. valuesDo: aBlock
  521. ^values do: aBlock
  522. ! !
  523. !Dictionary methodsFor: 'initialization'!
  524. initialize
  525. super initialize.
  526. keys := #().
  527. values := #()
  528. ! !
  529. !Dictionary methodsFor: 'private'!
  530. positionOfKey: anObject
  531. <
  532. var keys = self['@keys'];
  533. for(var i=0;i<keys.length;i++){
  534. if(keys[i].__eq(anObject)) { return i;}
  535. }
  536. return -1;
  537. >
  538. ! !
  539. !Dictionary methodsFor: 'testing'!
  540. includesKey: aKey
  541. < return self._positionOfKey_(aKey) >>= 0; >
  542. ! !
  543. IndexableCollection subclass: #SequenceableCollection
  544. instanceVariableNames: ''
  545. package: 'Kernel-Collections'!
  546. !SequenceableCollection commentStamp!
  547. A SequencableCollection is an IndexableCollection
  548. with numeric indexes starting with 1.!
  549. !SequenceableCollection methodsFor: 'accessing'!
  550. allButFirst
  551. ^self copyFrom: 2 to: self size
  552. !
  553. allButLast
  554. ^self copyFrom: 1 to: self size - 1
  555. !
  556. atRandom
  557. ^ self at: self size atRandom
  558. !
  559. first
  560. ^self at: 1
  561. !
  562. first: n
  563. "Answer the first n elements of the receiver.
  564. Raise an error if there are not enough elements."
  565. ^ self copyFrom: 1 to: n
  566. !
  567. fourth
  568. ^self at: 4
  569. !
  570. indexOf: anObject
  571. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  572. !
  573. indexOf: anObject ifAbsent: aBlock
  574. <
  575. for(var i=0;i<self.length;i++) {
  576. if(self[i].__eq(anObject)) {return i+1}
  577. };
  578. return aBlock();
  579. >
  580. !
  581. indexOf: anObject startingAt: start
  582. "Answer the index of the first occurence of anElement after start
  583. within the receiver. If the receiver does not contain anElement,
  584. answer 0."
  585. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  586. !
  587. indexOf: anObject startingAt: start ifAbsent: aBlock
  588. <
  589. for(var i=start-1;i<self.length;i++){
  590. if(self[i].__eq(anObject)) {return i+1}
  591. }
  592. return aBlock();
  593. >
  594. !
  595. last
  596. ^self at: self size
  597. !
  598. second
  599. ^self at: 2
  600. !
  601. third
  602. ^self at: 3
  603. ! !
  604. !SequenceableCollection methodsFor: 'adding'!
  605. addLast: anObject
  606. self add: anObject
  607. !
  608. removeLast
  609. self remove: self last
  610. ! !
  611. !SequenceableCollection methodsFor: 'comparing'!
  612. = aCollection
  613. (self class = aCollection class and: [
  614. self size = aCollection size]) ifFalse: [^false].
  615. self withIndexDo: [:each :i |
  616. (aCollection at: i) = each ifFalse: [^false]].
  617. ^true
  618. ! !
  619. !SequenceableCollection methodsFor: 'converting'!
  620. reversed
  621. self subclassResponsibility
  622. ! !
  623. !SequenceableCollection methodsFor: 'copying'!
  624. copyFrom: anIndex to: anotherIndex
  625. | range newCollection |
  626. range := anIndex to: anotherIndex.
  627. newCollection := self class new: range size.
  628. range withIndexDo: [:each :i |
  629. newCollection at: i put: (self at: each)].
  630. ^newCollection
  631. !
  632. deepCopy
  633. | newCollection |
  634. newCollection := self class new: self size.
  635. self withIndexDo: [:each :index |
  636. newCollection at: index put: each deepCopy].
  637. ^newCollection
  638. !
  639. shallowCopy
  640. | newCollection |
  641. newCollection := self class new: self size.
  642. self withIndexDo: [ :each :index |
  643. newCollection at: index put: each].
  644. ^newCollection
  645. ! !
  646. !SequenceableCollection methodsFor: 'enumerating'!
  647. detect: aBlock ifNone: anotherBlock
  648. <
  649. for(var i = 0; i < self.length; i++)
  650. if(aBlock(self[i]))
  651. return self[i];
  652. return anotherBlock();
  653. >
  654. !
  655. do: aBlock
  656. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  657. !
  658. with: anotherCollection do: aBlock
  659. <for(var i=0;i<self.length;i++){aBlock(self[i], anotherCollection[i]);}>
  660. !
  661. withIndexDo: aBlock
  662. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  663. ! !
  664. !SequenceableCollection methodsFor: 'testing'!
  665. includes: anObject
  666. ^(self indexOf: anObject ifAbsent: [nil]) notNil
  667. ! !
  668. SequenceableCollection subclass: #Array
  669. instanceVariableNames: ''
  670. package: 'Kernel-Collections'!
  671. !Array methodsFor: 'accessing'!
  672. at: anIndex ifAbsent: aBlock
  673. <
  674. if((anIndex < 1) || (self.length < anIndex)) {return aBlock()};
  675. return self[anIndex - 1];
  676. >
  677. !
  678. at: anIndex put: anObject
  679. <return self[anIndex - 1] = anObject>
  680. !
  681. size
  682. <return self.length>
  683. ! !
  684. !Array methodsFor: 'adding/removing'!
  685. add: anObject
  686. <self.push(anObject); return anObject;>
  687. !
  688. remove: anObject ifAbsent: aBlock
  689. <
  690. for(var i=0;i<self.length;i++) {
  691. if(self[i] == anObject) {
  692. self.splice(i,1);
  693. return self;
  694. }
  695. };
  696. aBlock._value();
  697. >
  698. !
  699. removeFrom: aNumber to: anotherNumber
  700. <self.splice(aNumber - 1,anotherNumber - 1)>
  701. ! !
  702. !Array methodsFor: 'converting'!
  703. asJavascript
  704. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  705. !
  706. reversed
  707. <return self._copy().reverse()>
  708. ! !
  709. !Array methodsFor: 'enumerating'!
  710. join: aString
  711. <return self.join(aString)>
  712. !
  713. sort
  714. ^self basicPerform: 'sort'
  715. !
  716. sort: aBlock
  717. <
  718. return self.sort(function(a, b) {
  719. if(aBlock(a,b)) {return -1} else {return 1}
  720. })
  721. >
  722. !
  723. sorted
  724. ^self copy sort
  725. !
  726. sorted: aBlock
  727. ^self copy sort: aBlock
  728. ! !
  729. !Array class methodsFor: 'instance creation'!
  730. new: anInteger
  731. <return new Array(anInteger)>
  732. !
  733. with: anObject
  734. ^(self new: 1)
  735. at: 1 put: anObject;
  736. yourself
  737. !
  738. with: anObject with: anObject2
  739. ^(self new: 2)
  740. at: 1 put: anObject;
  741. at: 2 put: anObject2;
  742. yourself
  743. !
  744. with: anObject with: anObject2 with: anObject3
  745. ^(self new: 3)
  746. at: 1 put: anObject;
  747. at: 2 put: anObject2;
  748. at: 3 put: anObject3;
  749. yourself
  750. !
  751. withAll: aCollection
  752. | instance index |
  753. index := 1.
  754. instance := self new: aCollection size.
  755. aCollection do: [:each |
  756. instance at: index put: each.
  757. index := index + 1].
  758. ^instance
  759. ! !
  760. SequenceableCollection subclass: #CharacterArray
  761. instanceVariableNames: ''
  762. package: 'Kernel-Collections'!
  763. !CharacterArray methodsFor: 'accessing'!
  764. at: anIndex put: anObject
  765. self errorReadOnly
  766. ! !
  767. !CharacterArray methodsFor: 'adding'!
  768. add: anObject
  769. self errorReadOnly
  770. !
  771. remove: anObject
  772. self errorReadOnly
  773. ! !
  774. !CharacterArray methodsFor: 'converting'!
  775. asLowercase
  776. ^self class fromString: self asString asLowercase
  777. !
  778. asNumber
  779. ^self asString asNumber
  780. !
  781. asString
  782. ^self subclassResponsibility
  783. !
  784. asSymbol
  785. ^self subclassResponsibility
  786. !
  787. asUppercase
  788. ^self class fromString: self asString asUppercase
  789. ! !
  790. !CharacterArray methodsFor: 'copying'!
  791. , aString
  792. ^self asString, aString asString
  793. ! !
  794. !CharacterArray methodsFor: 'error handling'!
  795. errorReadOnly
  796. self error: 'Object is read-only'
  797. ! !
  798. !CharacterArray methodsFor: 'printing'!
  799. printString
  800. ^self asString printString
  801. ! !
  802. !CharacterArray class methodsFor: 'instance creation'!
  803. fromString: aString
  804. self subclassResponsibility
  805. ! !
  806. CharacterArray subclass: #String
  807. instanceVariableNames: ''
  808. package: 'Kernel-Collections'!
  809. !String methodsFor: 'accessing'!
  810. asciiValue
  811. <return self.charCodeAt(0);>
  812. !
  813. at: anIndex ifAbsent: aBlock
  814. <return String(self).charAt(anIndex - 1) || aBlock()>
  815. !
  816. escaped
  817. <return escape(self)>
  818. !
  819. size
  820. <return self.length>
  821. !
  822. unescaped
  823. <return unescape(self)>
  824. ! !
  825. !String methodsFor: 'comparing'!
  826. < aString
  827. <return String(self) < aString._asString()>
  828. !
  829. <= aString
  830. <return String(self) <= aString._asString()>
  831. !
  832. = aString
  833. <
  834. if(!! aString._isString || !! aString._isString()) {
  835. return false;
  836. }
  837. return String(self) === String(aString)
  838. >
  839. !
  840. == aString
  841. ^self = aString
  842. !
  843. > aString
  844. <return String(self) >> aString._asString()>
  845. !
  846. >= aString
  847. <return String(self) >>= aString._asString()>
  848. ! !
  849. !String methodsFor: 'converting'!
  850. asJSON
  851. ^self
  852. !
  853. asJavaScriptSelector
  854. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  855. !
  856. asJavascript
  857. <
  858. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  859. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  860. else
  861. return "\"" + self + "\"";
  862. >
  863. !
  864. asLowercase
  865. <return self.toLowerCase()>
  866. !
  867. asNumber
  868. <return Number(self)>
  869. !
  870. asRegexp
  871. ^ RegularExpression fromString: self
  872. !
  873. asSelector
  874. <return smalltalk.selector(self)>
  875. !
  876. asString
  877. ^self
  878. !
  879. asSymbol
  880. ^Symbol lookup: self
  881. !
  882. asUppercase
  883. <return self.toUpperCase()>
  884. !
  885. reversed
  886. <return self.split("").reverse().join("")>
  887. !
  888. tokenize: aString
  889. <return self.split(aString)>
  890. ! !
  891. !String methodsFor: 'copying'!
  892. , aString
  893. <return self + aString>
  894. !
  895. copyFrom: anIndex to: anotherIndex
  896. <return self.substring(anIndex - 1, anotherIndex)>
  897. !
  898. deepCopy
  899. ^self shallowCopy
  900. !
  901. shallowCopy
  902. ^self class fromString: self
  903. ! !
  904. !String methodsFor: 'enumerating'!
  905. do: aBlock
  906. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i));}>
  907. !
  908. withIndexDo: aBlock
  909. <for(var i=0;i<self.length;i++){aBlock(self.charAt(i), i+1);}>
  910. ! !
  911. !String methodsFor: 'printing'!
  912. printNl
  913. <console.log(self)>
  914. !
  915. printString
  916. ^'''', self, ''''
  917. ! !
  918. !String methodsFor: 'regular expressions'!
  919. match: aRegexp
  920. <return self.search(aRegexp) !!= -1>
  921. !
  922. matchesOf: aRegularExpression
  923. <return self.match(aRegularExpression)>
  924. !
  925. replace: aString with: anotherString
  926. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  927. !
  928. replaceRegexp: aRegexp with: aString
  929. <return self.replace(aRegexp, aString)>
  930. !
  931. trimBoth
  932. ^self trimBoth: '\s'
  933. !
  934. trimBoth: separators
  935. ^(self trimLeft: separators) trimRight: separators
  936. !
  937. trimLeft
  938. ^self trimLeft: '\s'
  939. !
  940. trimLeft: separators
  941. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  942. !
  943. trimRight
  944. ^self trimRight: '\s'
  945. !
  946. trimRight: separators
  947. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  948. ! !
  949. !String methodsFor: 'split join'!
  950. join: aCollection
  951. ^ String
  952. streamContents: [:stream | aCollection
  953. do: [:each | stream nextPutAll: each asString]
  954. separatedBy: [stream nextPutAll: self]]
  955. !
  956. lineIndicesDo: aBlock
  957. "execute aBlock with 3 arguments for each line:
  958. - start index of line
  959. - end index of line without line delimiter
  960. - end index of line including line delimiter(s) CR, LF or CRLF"
  961. | cr lf start sz nextLF nextCR |
  962. start := 1.
  963. sz := self size.
  964. cr := String cr.
  965. nextCR := self indexOf: cr startingAt: 1.
  966. lf := String lf.
  967. nextLF := self indexOf: lf startingAt: 1.
  968. [ start <= sz ] whileTrue: [
  969. (nextLF = 0 and: [ nextCR = 0 ])
  970. ifTrue: [ "No more CR, nor LF, the string is over"
  971. aBlock value: start value: sz value: sz.
  972. ^self ].
  973. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  974. ifTrue: [ "Found a LF"
  975. aBlock value: start value: nextLF - 1 value: nextLF.
  976. start := 1 + nextLF.
  977. nextLF := self indexOf: lf startingAt: start ]
  978. ifFalse: [ 1 + nextCR = nextLF
  979. ifTrue: [ "Found a CR-LF pair"
  980. aBlock value: start value: nextCR - 1 value: nextLF.
  981. start := 1 + nextLF.
  982. nextCR := self indexOf: cr startingAt: start.
  983. nextLF := self indexOf: lf startingAt: start ]
  984. ifFalse: [ "Found a CR"
  985. aBlock value: start value: nextCR - 1 value: nextCR.
  986. start := 1 + nextCR.
  987. nextCR := self indexOf: cr startingAt: start ]]]
  988. !
  989. lineNumber: anIndex
  990. "Answer a string containing the characters in the given line number."
  991. | lineCount |
  992. lineCount := 0.
  993. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  994. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  995. ^nil
  996. !
  997. lines
  998. "Answer an array of lines composing this receiver without the line ending delimiters."
  999. | lines |
  1000. lines := Array new.
  1001. self linesDo: [:aLine | lines add: aLine].
  1002. ^lines
  1003. !
  1004. linesDo: aBlock
  1005. "Execute aBlock with each line in this string. The terminating line
  1006. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1007. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1008. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1009. ! !
  1010. !String methodsFor: 'testing'!
  1011. includesSubString: subString
  1012. < return self.indexOf(subString) !!= -1 >
  1013. !
  1014. isString
  1015. ^true
  1016. ! !
  1017. !String class methodsFor: 'accessing'!
  1018. cr
  1019. <return '\r'>
  1020. !
  1021. crlf
  1022. <return '\r\n'>
  1023. !
  1024. lf
  1025. <return '\n'>
  1026. !
  1027. space
  1028. <return ' '>
  1029. !
  1030. streamClass
  1031. ^StringStream
  1032. !
  1033. tab
  1034. <return '\t'>
  1035. ! !
  1036. !String class methodsFor: 'instance creation'!
  1037. fromCharCode: anInteger
  1038. <return String.fromCharCode(anInteger)>
  1039. !
  1040. fromString: aString
  1041. <return new self.fn(aString)>
  1042. !
  1043. streamContents: blockWithArg
  1044. |stream|
  1045. stream := (self streamClass on: String new).
  1046. blockWithArg value: stream.
  1047. ^ stream contents
  1048. !
  1049. value: aUTFCharCode
  1050. <return String.fromCharCode(aUTFCharCode);>
  1051. ! !
  1052. CharacterArray subclass: #Symbol
  1053. instanceVariableNames: ''
  1054. package: 'Kernel-Collections'!
  1055. !Symbol methodsFor: 'accessing'!
  1056. at: anIndex ifAbsent: aBlock
  1057. ^self asString at: anIndex ifAbsent: aBlock
  1058. !
  1059. size
  1060. ^self asString size
  1061. ! !
  1062. !Symbol methodsFor: 'comparing'!
  1063. < aSymbol
  1064. ^self asString < aSymbol asString
  1065. !
  1066. <= aSymbol
  1067. ^self asString <= aSymbol asString
  1068. !
  1069. = aSymbol
  1070. aSymbol class = self class ifFalse: [^false].
  1071. ^self asString = aSymbol asString
  1072. !
  1073. > aSymbol
  1074. ^self asString > aSymbol asString
  1075. !
  1076. >= aSymbol
  1077. ^self asString >= aSymbol asString
  1078. ! !
  1079. !Symbol methodsFor: 'converting'!
  1080. asJSON
  1081. ^self asString asJSON
  1082. !
  1083. asJavascript
  1084. ^'smalltalk.symbolFor(', self asString asJavascript, ')'
  1085. !
  1086. asSelector
  1087. ^self asString asSelector
  1088. !
  1089. asString
  1090. <return self.value>
  1091. !
  1092. asSuperSelector
  1093. ^self asString asSuperSelector
  1094. !
  1095. asSymbol
  1096. ^self
  1097. ! !
  1098. !Symbol methodsFor: 'copying'!
  1099. copyFrom: anIndex to: anotherIndex
  1100. ^self class fromString: (self asString copyFrom: anIndex to: anotherIndex)
  1101. !
  1102. deepCopy
  1103. ^self
  1104. !
  1105. shallowCopy
  1106. ^self
  1107. ! !
  1108. !Symbol methodsFor: 'enumerating'!
  1109. collect: aBlock
  1110. ^ (self asString collect: aBlock) asSymbol
  1111. !
  1112. detect: aBlock
  1113. ^ self asString detect: aBlock
  1114. !
  1115. do: aBlock
  1116. self asString do: aBlock
  1117. !
  1118. select: aBlock
  1119. ^ (self asString select: aBlock) asSymbol
  1120. !
  1121. withIndexDo: aBlock
  1122. self asString withIndexDo: aBlock
  1123. ! !
  1124. !Symbol methodsFor: 'evaluating'!
  1125. value: anObject
  1126. ^anObject perform: self
  1127. ! !
  1128. !Symbol methodsFor: 'printing'!
  1129. isSymbol
  1130. ^true
  1131. !
  1132. printString
  1133. ^'#', self asString
  1134. ! !
  1135. !Symbol class methodsFor: 'instance creation'!
  1136. basicNew
  1137. self shouldNotImplement
  1138. !
  1139. fromString: aString
  1140. ^self lookup: aString
  1141. !
  1142. lookup: aString
  1143. <return smalltalk.symbolFor(aString);>
  1144. ! !
  1145. Collection subclass: #Set
  1146. instanceVariableNames: 'elements'
  1147. package: 'Kernel-Collections'!
  1148. !Set methodsFor: 'accessing'!
  1149. size
  1150. ^elements size
  1151. ! !
  1152. !Set methodsFor: 'adding/removing'!
  1153. add: anObject
  1154. <
  1155. var found;
  1156. for(var i=0; i < self['@elements'].length; i++) {
  1157. if(anObject == self['@elements'][i]) {
  1158. found = true;
  1159. break;
  1160. }
  1161. }
  1162. if(!!found) {self['@elements'].push(anObject)}
  1163. >
  1164. !
  1165. remove: anObject
  1166. elements remove: anObject
  1167. ! !
  1168. !Set methodsFor: 'comparing'!
  1169. = aCollection
  1170. self class = aCollection class ifFalse: [ ^ false ].
  1171. self size = aCollection size ifFalse: [ ^ false ].
  1172. self do: [:each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1173. ^ true
  1174. ! !
  1175. !Set methodsFor: 'converting'!
  1176. asArray
  1177. ^elements copy
  1178. ! !
  1179. !Set methodsFor: 'enumerating'!
  1180. collect: aBlock
  1181. ^self class withAll: (elements collect: aBlock)
  1182. !
  1183. detect: aBlock ifNone: anotherBlock
  1184. ^elements detect: aBlock ifNone: anotherBlock
  1185. !
  1186. do: aBlock
  1187. elements do: aBlock
  1188. !
  1189. select: aBlock
  1190. | collection |
  1191. collection := self class new.
  1192. self do: [:each |
  1193. (aBlock value: each) ifTrue: [
  1194. collection add: each]].
  1195. ^collection
  1196. ! !
  1197. !Set methodsFor: 'initialization'!
  1198. initialize
  1199. super initialize.
  1200. elements := #()
  1201. ! !
  1202. !Set methodsFor: 'testing'!
  1203. includes: anObject
  1204. ^elements includes: anObject
  1205. ! !
  1206. Object subclass: #Queue
  1207. instanceVariableNames: 'read readIndex write'
  1208. package: 'Kernel-Collections'!
  1209. !Queue commentStamp!
  1210. A Queue am a one-sided queue.
  1211. A Queue uses two OrderedCollections inside,
  1212. `read` is at the front, is not modified and only read using `readIndex`.
  1213. `write` is at the back and is appended new items.
  1214. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1215. As a consequence, no data moving is done by the Queue; write appending may do data moving
  1216. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1217. !Queue methodsFor: 'accessing'!
  1218. back: anObject
  1219. write add: anObject
  1220. !
  1221. front
  1222. ^self frontIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1223. !
  1224. frontIfAbsent: aBlock
  1225. | result |
  1226. result := read at: readIndex ifAbsent: [
  1227. write isEmpty ifTrue: [
  1228. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1229. ^aBlock value ].
  1230. read := write.
  1231. readIndex := 1.
  1232. write := OrderedCollection new.
  1233. read first ].
  1234. read at: readIndex put: nil.
  1235. readIndex := readIndex + 1.
  1236. ^result
  1237. ! !
  1238. !Queue methodsFor: 'initialization'!
  1239. initialize
  1240. super initialize.
  1241. read := OrderedCollection new.
  1242. write := OrderedCollection new.
  1243. readIndex := 1
  1244. ! !
  1245. Object subclass: #RegularExpression
  1246. instanceVariableNames: ''
  1247. package: 'Kernel-Collections'!
  1248. !RegularExpression methodsFor: 'evaluating'!
  1249. compile: aString
  1250. <return self.compile(aString)>
  1251. !
  1252. exec: aString
  1253. <return self.exec(aString) || nil>
  1254. !
  1255. test: aString
  1256. <return self.test(aString)>
  1257. ! !
  1258. !RegularExpression class methodsFor: 'instance creation'!
  1259. fromString: aString
  1260. ^self fromString: aString flag: ''
  1261. !
  1262. fromString: aString flag: anotherString
  1263. <return new RegExp(aString, anotherString)>
  1264. ! !
  1265. Object subclass: #Stream
  1266. instanceVariableNames: 'collection position streamSize'
  1267. package: 'Kernel-Collections'!
  1268. !Stream methodsFor: 'accessing'!
  1269. collection
  1270. ^collection
  1271. !
  1272. contents
  1273. ^self collection
  1274. copyFrom: 1
  1275. to: self streamSize
  1276. !
  1277. position
  1278. ^position ifNil: [position := 0]
  1279. !
  1280. position: anInteger
  1281. position := anInteger
  1282. !
  1283. setCollection: aCollection
  1284. collection := aCollection
  1285. !
  1286. setStreamSize: anInteger
  1287. streamSize := anInteger
  1288. !
  1289. size
  1290. ^self streamSize
  1291. !
  1292. streamSize
  1293. ^streamSize
  1294. ! !
  1295. !Stream methodsFor: 'actions'!
  1296. close
  1297. !
  1298. flush
  1299. !
  1300. reset
  1301. self position: 0
  1302. !
  1303. resetContents
  1304. self reset.
  1305. self setStreamSize: 0
  1306. ! !
  1307. !Stream methodsFor: 'enumerating'!
  1308. do: aBlock
  1309. [self atEnd] whileFalse: [aBlock value: self next]
  1310. ! !
  1311. !Stream methodsFor: 'positioning'!
  1312. setToEnd
  1313. self position: self size
  1314. !
  1315. skip: anInteger
  1316. self position: ((self position + anInteger) min: self size max: 0)
  1317. ! !
  1318. !Stream methodsFor: 'reading'!
  1319. next
  1320. ^self atEnd
  1321. ifTrue: [nil]
  1322. ifFalse: [
  1323. self position: self position + 1.
  1324. collection at: self position]
  1325. !
  1326. next: anInteger
  1327. | tempCollection |
  1328. tempCollection := self collection class new.
  1329. anInteger timesRepeat: [
  1330. self atEnd ifFalse: [
  1331. tempCollection add: self next]].
  1332. ^tempCollection
  1333. !
  1334. peek
  1335. ^self atEnd ifFalse: [
  1336. self collection at: self position + 1]
  1337. ! !
  1338. !Stream methodsFor: 'testing'!
  1339. atEnd
  1340. ^self position = self size
  1341. !
  1342. atStart
  1343. ^self position = 0
  1344. !
  1345. isEmpty
  1346. ^self size = 0
  1347. ! !
  1348. !Stream methodsFor: 'writing'!
  1349. nextPut: anObject
  1350. self position: self position + 1.
  1351. self collection at: self position put: anObject.
  1352. self setStreamSize: (self streamSize max: self position)
  1353. !
  1354. nextPutAll: aCollection
  1355. aCollection do: [:each |
  1356. self nextPut: each]
  1357. ! !
  1358. !Stream class methodsFor: 'instance creation'!
  1359. on: aCollection
  1360. ^self new
  1361. setCollection: aCollection;
  1362. setStreamSize: aCollection size;
  1363. yourself
  1364. ! !
  1365. Stream subclass: #StringStream
  1366. instanceVariableNames: ''
  1367. package: 'Kernel-Collections'!
  1368. !StringStream methodsFor: 'reading'!
  1369. next: anInteger
  1370. | tempCollection |
  1371. tempCollection := self collection class new.
  1372. anInteger timesRepeat: [
  1373. self atEnd ifFalse: [
  1374. tempCollection := tempCollection, self next]].
  1375. ^tempCollection
  1376. ! !
  1377. !StringStream methodsFor: 'writing'!
  1378. cr
  1379. ^self nextPutAll: String cr
  1380. !
  1381. crlf
  1382. ^self nextPutAll: String crlf
  1383. !
  1384. lf
  1385. ^self nextPutAll: String lf
  1386. !
  1387. nextPut: aString
  1388. self nextPutAll: aString
  1389. !
  1390. nextPutAll: aString
  1391. self setCollection:
  1392. (self collection copyFrom: 1 to: self position),
  1393. aString,
  1394. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1395. self position: self position + aString size.
  1396. self setStreamSize: (self streamSize max: self position)
  1397. !
  1398. space
  1399. self nextPut: ' '
  1400. ! !