Kernel-Collections.st 35 KB

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