Kernel-Collections.st 36 KB

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