Kernel-Collections.st 38 KB

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