Kernel-Collections.st 37 KB

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