2
0

Kernel-Collections.st 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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 subclassResponsibility
  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 subclassResponsibility
  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 subclassResponsibility
  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: aNumber
  636. "Answer the first `aNumber` elements of the receiver.
  637. Raise an error if there are not enough elements in the receiver."
  638. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  639. ^ self copyFrom: 1 to: aNumber
  640. !
  641. fourth
  642. ^ self at: 4
  643. !
  644. indexOf: anObject ifAbsent: aBlock
  645. <
  646. self = self._numericallyIndexable();
  647. for(var i=0; i < self.length; i++) {
  648. if(self[i].__eq(anObject)) {return i+1}
  649. };
  650. return aBlock._value();
  651. >
  652. !
  653. indexOf: anObject startingAt: start
  654. "Answer the index of the first occurence of anElement after start
  655. within the receiver. If the receiver does not contain anElement,
  656. answer 0."
  657. ^ self indexOf: anObject startingAt: start ifAbsent: [ 0 ]
  658. !
  659. indexOf: anObject startingAt: start ifAbsent: aBlock
  660. <
  661. self = self._numericallyIndexable();
  662. for(var i=start - 1; i < self.length; i++){
  663. if(self[i].__eq(anObject)) {return i+1}
  664. }
  665. return aBlock._value();
  666. >
  667. !
  668. last
  669. ^ self at: self size
  670. !
  671. last: aNumber
  672. "Answer the last aNumber elements of the receiver.
  673. Raise an error if there are not enough elements in the receiver."
  674. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  675. ^ self copyFrom: self size - aNumber + 1 to: self size
  676. !
  677. second
  678. ^ self at: 2
  679. !
  680. third
  681. ^ self at: 3
  682. ! !
  683. !SequenceableCollection methodsFor: 'adding/removing'!
  684. addLast: anObject
  685. self add: anObject
  686. !
  687. removeLast
  688. ^ self remove: self last
  689. ! !
  690. !SequenceableCollection methodsFor: 'comparing'!
  691. = aCollection
  692. (self class = aCollection class and: [
  693. self size = aCollection size ]) ifFalse: [ ^ false ].
  694. self withIndexDo: [ :each :i |
  695. (aCollection at: i) = each ifFalse: [ ^ false ]].
  696. ^ true
  697. ! !
  698. !SequenceableCollection methodsFor: 'converting'!
  699. reversed
  700. self subclassResponsibility
  701. ! !
  702. !SequenceableCollection methodsFor: 'copying'!
  703. copyFrom: anIndex to: anotherIndex
  704. | range newCollection |
  705. range := anIndex to: anotherIndex.
  706. newCollection := self class new: range size.
  707. range withIndexDo: [ :each :i |
  708. newCollection at: i put: (self at: each) ].
  709. ^ newCollection
  710. !
  711. deepCopy
  712. | newCollection |
  713. newCollection := self class new: self size.
  714. self withIndexDo: [ :each :index |
  715. newCollection at: index put: each deepCopy ].
  716. ^ newCollection
  717. !
  718. shallowCopy
  719. | newCollection |
  720. newCollection := self class new: self size.
  721. self withIndexDo: [ :each :index |
  722. newCollection at: index put: each ].
  723. ^ newCollection
  724. ! !
  725. !SequenceableCollection methodsFor: 'enumerating'!
  726. detect: aBlock ifNone: anotherBlock
  727. <
  728. self = self._numericallyIndexable();
  729. for(var i = 0; i < self.length; i++)
  730. if(aBlock._value_(self[i]))
  731. return self[i];
  732. return anotherBlock._value();
  733. >
  734. !
  735. do: aBlock
  736. <
  737. self = self._numericallyIndexable();
  738. for(var i=0; i < self.length; i++) {
  739. aBlock._value_(self[i]);
  740. }
  741. >
  742. !
  743. with: anotherCollection do: aBlock
  744. <
  745. self = self._numericallyIndexable();
  746. anotherCollection = anotherCollection._numericallyIndexable();
  747. for(var i=0; i<self.length; i++) {
  748. aBlock._value_value_(self[i], anotherCollection[i]);
  749. }
  750. >
  751. !
  752. withIndexDo: aBlock
  753. <
  754. self = self._numericallyIndexable();
  755. for(var i=0; i < self.length; i++) {
  756. aBlock._value_value_(self[i], i+1);
  757. }
  758. >
  759. ! !
  760. !SequenceableCollection methodsFor: 'private'!
  761. numericallyIndexable
  762. "This is an internal converting message.
  763. It answeres a representation of the receiver
  764. that can use foo[i] in JavaScript code.
  765. It fixes IE8, where boxed String is unable
  766. to numerically index its characters,
  767. but primitive string can."
  768. self subclassResponsibility
  769. ! !
  770. !SequenceableCollection methodsFor: 'streaming'!
  771. newStream
  772. ^ self streamClass on: self
  773. !
  774. readStream
  775. "For Pharo compatibility"
  776. ^ self stream
  777. !
  778. stream
  779. ^ self newStream
  780. !
  781. streamClass
  782. ^ self class streamClass
  783. !
  784. writeStream
  785. "For Pharo compatibility"
  786. ^ self stream
  787. ! !
  788. !SequenceableCollection methodsFor: 'testing'!
  789. beginsWith: prefix
  790. self size < prefix size ifTrue: [ ^ false ].
  791. ^ (self first: prefix size) = prefix
  792. !
  793. endsWith: suffix
  794. self size < suffix size ifTrue: [ ^ false ].
  795. ^ (self last: suffix size) = suffix
  796. !
  797. includes: anObject
  798. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  799. ! !
  800. !SequenceableCollection class methodsFor: 'accessing'!
  801. streamClass
  802. ^ Stream
  803. ! !
  804. !SequenceableCollection class methodsFor: 'streaming'!
  805. streamContents: aBlock
  806. | stream |
  807. stream := (self streamClass on: self new).
  808. aBlock value: stream.
  809. ^ stream contents
  810. ! !
  811. SequenceableCollection subclass: #Array
  812. instanceVariableNames: ''
  813. package: 'Kernel-Collections'!
  814. !Array commentStamp!
  815. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  816. I am directly mapped to JavaScript Number.
  817. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  818. !Array methodsFor: 'accessing'!
  819. at: anIndex ifAbsent: aBlock
  820. <
  821. return anIndex >>= 1 && anIndex <= self.length
  822. ? self[anIndex - 1]
  823. : aBlock._value()
  824. >
  825. !
  826. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  827. <
  828. return anIndex >>= 1 && anIndex <= self.length
  829. ? aBlock._value_(self[anIndex - 1])
  830. : anotherBlock._value()
  831. >
  832. !
  833. at: anIndex put: anObject
  834. <return self[anIndex - 1] = anObject>
  835. !
  836. size
  837. <return self.length>
  838. ! !
  839. !Array methodsFor: 'adding/removing'!
  840. add: anObject
  841. <self.push(anObject); return anObject;>
  842. !
  843. addFirst: anObject
  844. <self.unshift(anObject); return anObject;>
  845. !
  846. remove: anObject ifAbsent: aBlock
  847. <
  848. for(var i=0;i<self.length;i++) {
  849. if(_st(self[i]).__eq(anObject)) {
  850. self.splice(i,1);
  851. return self;
  852. }
  853. };
  854. aBlock._value();
  855. >
  856. !
  857. removeAll
  858. <self.length = 0>
  859. !
  860. removeFrom: aNumber to: anotherNumber
  861. <self.splice(aNumber -1, anotherNumber - aNumber + 1)>
  862. !
  863. removeIndex: anInteger
  864. <self.splice(anInteger - 1, 1)>
  865. !
  866. removeLast
  867. <return self.pop();>
  868. ! !
  869. !Array methodsFor: 'converting'!
  870. asJavascript
  871. ^ '[', ((self collect: [:each | each asJavascript ]) join: ', '), ']'
  872. !
  873. reversed
  874. <return self._copy().reverse()>
  875. ! !
  876. !Array methodsFor: 'enumerating'!
  877. collect: aBlock
  878. "Optimized version"
  879. <return self.map(function(each) {return aBlock._value_(each)})>
  880. !
  881. join: aString
  882. <return self.join(aString)>
  883. !
  884. select: aBlock
  885. "Optimized version"
  886. <
  887. var result = self.klass._new();
  888. for(var i=0; i<self.length; i++) {
  889. if(aBlock._value_(self[i])) {
  890. result.push(self[i]);
  891. }
  892. }
  893. return result;
  894. >
  895. !
  896. sort
  897. ^ self basicPerform: 'sort'
  898. !
  899. sort: aBlock
  900. <
  901. return self.sort(function(a, b) {
  902. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  903. })
  904. >
  905. !
  906. sorted
  907. ^ self copy sort
  908. !
  909. sorted: aBlock
  910. ^ self copy sort: aBlock
  911. ! !
  912. !Array methodsFor: 'printing'!
  913. printOn: aStream
  914. super printOn: aStream.
  915. aStream nextPutAll: ' ('.
  916. self
  917. do: [ :each | each printOn: aStream ]
  918. separatedBy: [ aStream nextPutAll: ' ' ].
  919. aStream nextPutAll: ')'
  920. ! !
  921. !Array methodsFor: 'private'!
  922. numericallyIndexable
  923. ^ self
  924. ! !
  925. !Array class methodsFor: 'instance creation'!
  926. new: anInteger
  927. <return new Array(anInteger)>
  928. !
  929. with: anObject
  930. ^ (self new: 1)
  931. at: 1 put: anObject;
  932. yourself
  933. !
  934. with: anObject with: anObject2
  935. ^ (self new: 2)
  936. at: 1 put: anObject;
  937. at: 2 put: anObject2;
  938. yourself
  939. !
  940. with: anObject with: anObject2 with: anObject3
  941. ^ (self new: 3)
  942. at: 1 put: anObject;
  943. at: 2 put: anObject2;
  944. at: 3 put: anObject3;
  945. yourself
  946. !
  947. withAll: aCollection
  948. | instance index |
  949. index := 1.
  950. instance := self new: aCollection size.
  951. aCollection do: [ :each |
  952. instance at: index put: each.
  953. index := index + 1 ].
  954. ^ instance
  955. ! !
  956. SequenceableCollection subclass: #CharacterArray
  957. instanceVariableNames: ''
  958. package: 'Kernel-Collections'!
  959. !CharacterArray commentStamp!
  960. I am the abstract superclass of string-like collections.!
  961. !CharacterArray methodsFor: 'accessing'!
  962. at: anIndex put: anObject
  963. self errorReadOnly
  964. ! !
  965. !CharacterArray methodsFor: 'adding/removing'!
  966. add: anObject
  967. self errorReadOnly
  968. !
  969. remove: anObject
  970. self errorReadOnly
  971. ! !
  972. !CharacterArray methodsFor: 'converting'!
  973. asLowercase
  974. ^ self class fromString: self asString asLowercase
  975. !
  976. asNumber
  977. ^ self asString asNumber
  978. !
  979. asString
  980. ^ self subclassResponsibility
  981. !
  982. asSymbol
  983. ^ self asString
  984. !
  985. asUppercase
  986. ^ self class fromString: self asString asUppercase
  987. ! !
  988. !CharacterArray methodsFor: 'copying'!
  989. , aString
  990. ^ self asString, aString asString
  991. ! !
  992. !CharacterArray methodsFor: 'error handling'!
  993. errorReadOnly
  994. self error: 'Object is read-only'
  995. ! !
  996. !CharacterArray methodsFor: 'printing'!
  997. printOn: aStream
  998. self asString printOn: aStream
  999. ! !
  1000. !CharacterArray methodsFor: 'streaming'!
  1001. putOn: aStream
  1002. aStream nextPutString: self
  1003. ! !
  1004. !CharacterArray class methodsFor: 'instance creation'!
  1005. fromString: aString
  1006. self subclassResponsibility
  1007. ! !
  1008. CharacterArray subclass: #String
  1009. instanceVariableNames: ''
  1010. package: 'Kernel-Collections'!
  1011. !String commentStamp!
  1012. 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.
  1013. String inherits many useful methods from its hierarchy, such as
  1014. `Collection >> #,`!
  1015. !String methodsFor: 'accessing'!
  1016. asciiValue
  1017. <return self.charCodeAt(0);>
  1018. !
  1019. at: anIndex ifAbsent: aBlock
  1020. <return String(self)[anIndex - 1] || aBlock._value()>
  1021. !
  1022. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1023. <
  1024. var result = String(self)[anIndex - 1];
  1025. return result ? aBlock._value_(result) : anotherBlock._value();
  1026. >
  1027. !
  1028. charCodeAt: anInteger
  1029. <return self.charCodeAt(anInteger - 1)>
  1030. !
  1031. identityHash
  1032. ^ self, 's'
  1033. !
  1034. size
  1035. <return self.length>
  1036. ! !
  1037. !String methodsFor: 'comparing'!
  1038. < aString
  1039. <return String(self) < aString._asString()>
  1040. !
  1041. <= aString
  1042. <return String(self) <= aString._asString()>
  1043. !
  1044. = aString
  1045. <
  1046. if(typeof aString === 'undefined') { return false }
  1047. if(!!aString._isString || !! aString._isString()) {
  1048. return false;
  1049. }
  1050. return String(self) === String(aString)
  1051. >
  1052. !
  1053. == aString
  1054. ^ self = aString
  1055. !
  1056. > aString
  1057. <return String(self) >> aString._asString()>
  1058. !
  1059. >= aString
  1060. <return String(self) >>= aString._asString()>
  1061. ! !
  1062. !String methodsFor: 'converting'!
  1063. asJSON
  1064. ^ self
  1065. !
  1066. asJavascript
  1067. <
  1068. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1069. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1070. else
  1071. return "\"" + self + "\"";
  1072. >
  1073. !
  1074. asLowercase
  1075. <return self.toLowerCase()>
  1076. !
  1077. asMutator
  1078. "Answer a setter selector. For example,
  1079. #name asMutator returns #name:"
  1080. self last = ':' ifFalse: [ ^ self, ':' ].
  1081. ^ self
  1082. !
  1083. asNumber
  1084. <return Number(self)>
  1085. !
  1086. asRegexp
  1087. ^ RegularExpression fromString: self
  1088. !
  1089. asSelector
  1090. <return smalltalk.selector(self)>
  1091. !
  1092. asString
  1093. ^ self
  1094. !
  1095. asSymbol
  1096. ^ self
  1097. !
  1098. asUppercase
  1099. <return self.toUpperCase()>
  1100. !
  1101. capitalized
  1102. ^ self isEmpty
  1103. ifTrue: [ self ]
  1104. ifFalse: [ self first asUppercase, self allButFirst ]
  1105. !
  1106. crlfSanitized
  1107. ^ self lines join: String lf
  1108. !
  1109. escaped
  1110. <return escape(self)>
  1111. !
  1112. reversed
  1113. <return self.split("").reverse().join("")>
  1114. !
  1115. unescaped
  1116. <return unescape(self)>
  1117. !
  1118. uriComponentDecoded
  1119. <return decodeURIComponent(self)>
  1120. !
  1121. uriComponentEncoded
  1122. <return encodeURIComponent(self)>
  1123. !
  1124. uriDecoded
  1125. <return decodeURI(self)>
  1126. !
  1127. uriEncoded
  1128. <return encodeURI(self)>
  1129. ! !
  1130. !String methodsFor: 'copying'!
  1131. , aString
  1132. <return String(self) + aString>
  1133. !
  1134. copyFrom: anIndex to: anotherIndex
  1135. <return self.substring(anIndex - 1, anotherIndex)>
  1136. !
  1137. deepCopy
  1138. ^ self shallowCopy
  1139. !
  1140. shallowCopy
  1141. ^ self class fromString: self
  1142. ! !
  1143. !String methodsFor: 'printing'!
  1144. printNl
  1145. <console.log(self)>
  1146. !
  1147. printOn: aStream
  1148. aStream
  1149. nextPutAll: '''';
  1150. nextPutAll: self;
  1151. nextPutAll: ''''
  1152. ! !
  1153. !String methodsFor: 'private'!
  1154. numericallyIndexable
  1155. <return String(self)>
  1156. ! !
  1157. !String methodsFor: 'regular expressions'!
  1158. match: aRegexp
  1159. <return self.search(aRegexp) !!= -1>
  1160. !
  1161. matchesOf: aRegularExpression
  1162. <return self.match(aRegularExpression)>
  1163. !
  1164. replace: aString with: anotherString
  1165. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1166. !
  1167. replaceRegexp: aRegexp with: aString
  1168. <return self.replace(aRegexp, aString)>
  1169. !
  1170. trimBoth
  1171. ^ self trimBoth: '\s'
  1172. !
  1173. trimBoth: separators
  1174. ^ (self trimLeft: separators) trimRight: separators
  1175. !
  1176. trimLeft
  1177. ^ self trimLeft: '\s'
  1178. !
  1179. trimLeft: separators
  1180. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1181. !
  1182. trimRight
  1183. ^ self trimRight: '\s'
  1184. !
  1185. trimRight: separators
  1186. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1187. ! !
  1188. !String methodsFor: 'split join'!
  1189. join: aCollection
  1190. ^ String
  1191. streamContents: [ :stream | aCollection
  1192. do: [ :each | stream nextPutAll: each asString ]
  1193. separatedBy: [ stream nextPutAll: self ]]
  1194. !
  1195. lineIndicesDo: aBlock
  1196. "execute aBlock with 3 arguments for each line:
  1197. - start index of line
  1198. - end index of line without line delimiter
  1199. - end index of line including line delimiter(s) CR, LF or CRLF"
  1200. | cr lf start sz nextLF nextCR |
  1201. start := 1.
  1202. sz := self size.
  1203. cr := String cr.
  1204. nextCR := self indexOf: cr startingAt: 1.
  1205. lf := String lf.
  1206. nextLF := self indexOf: lf startingAt: 1.
  1207. [ start <= sz ] whileTrue: [
  1208. (nextLF = 0 and: [ nextCR = 0 ])
  1209. ifTrue: [ "No more CR, nor LF, the string is over"
  1210. aBlock value: start value: sz value: sz.
  1211. ^ self ].
  1212. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1213. ifTrue: [ "Found a LF"
  1214. aBlock value: start value: nextLF - 1 value: nextLF.
  1215. start := 1 + nextLF.
  1216. nextLF := self indexOf: lf startingAt: start ]
  1217. ifFalse: [ 1 + nextCR = nextLF
  1218. ifTrue: [ "Found a CR-LF pair"
  1219. aBlock value: start value: nextCR - 1 value: nextLF.
  1220. start := 1 + nextLF.
  1221. nextCR := self indexOf: cr startingAt: start.
  1222. nextLF := self indexOf: lf startingAt: start ]
  1223. ifFalse: [ "Found a CR"
  1224. aBlock value: start value: nextCR - 1 value: nextCR.
  1225. start := 1 + nextCR.
  1226. nextCR := self indexOf: cr startingAt: start ] ]]
  1227. !
  1228. lineNumber: anIndex
  1229. "Answer a string containing the characters in the given line number."
  1230. | lineCount |
  1231. lineCount := 0.
  1232. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1233. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1234. ^ nil
  1235. !
  1236. lines
  1237. "Answer an array of lines composing this receiver without the line ending delimiters."
  1238. | lines |
  1239. lines := Array new.
  1240. self linesDo: [ :aLine | lines add: aLine ].
  1241. ^ lines
  1242. !
  1243. linesDo: aBlock
  1244. "Execute aBlock with each line in this string. The terminating line
  1245. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1246. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1247. aBlock value: (self copyFrom: start to: endWithoutDelimiters) ]
  1248. !
  1249. subStrings: aString
  1250. ^ self tokenize: aString
  1251. !
  1252. tokenize: aString
  1253. <return self.split(aString)>
  1254. ! !
  1255. !String methodsFor: 'testing'!
  1256. includesSubString: subString
  1257. <return self.indexOf(subString) !!= -1>
  1258. !
  1259. isCapitalized
  1260. ^ self first asUppercase == self first
  1261. !
  1262. isImmutable
  1263. ^ true
  1264. !
  1265. isString
  1266. ^ true
  1267. !
  1268. isVowel
  1269. "Answer true if the receiver is a one character string containing a voyel"
  1270. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1271. ! !
  1272. !String class methodsFor: 'accessing'!
  1273. cr
  1274. <return '\r'>
  1275. !
  1276. crlf
  1277. <return '\r\n'>
  1278. !
  1279. esc
  1280. ^ self fromCharCode: 27
  1281. !
  1282. lf
  1283. <return '\n'>
  1284. !
  1285. space
  1286. <return ' '>
  1287. !
  1288. streamClass
  1289. ^ StringStream
  1290. !
  1291. tab
  1292. <return '\t'>
  1293. ! !
  1294. !String class methodsFor: 'instance creation'!
  1295. fromCharCode: anInteger
  1296. <return String.fromCharCode(anInteger)>
  1297. !
  1298. fromString: aString
  1299. <return String(aString)>
  1300. !
  1301. value: aUTFCharCode
  1302. <return String.fromCharCode(aUTFCharCode);>
  1303. ! !
  1304. !String class methodsFor: 'random'!
  1305. random
  1306. "Returns random alphanumeric string beginning with letter"
  1307. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1308. !
  1309. randomNotIn: aString
  1310. | result |
  1311. [ result := self random. aString includesSubString: result ] whileTrue.
  1312. ^ result
  1313. ! !
  1314. Collection subclass: #Set
  1315. instanceVariableNames: 'elements'
  1316. package: 'Kernel-Collections'!
  1317. !Set commentStamp!
  1318. I represent an unordered set of objects without duplicates.!
  1319. !Set methodsFor: 'accessing'!
  1320. size
  1321. ^ elements size
  1322. ! !
  1323. !Set methodsFor: 'adding/removing'!
  1324. add: anObject
  1325. <
  1326. var found;
  1327. for(var i=0; i < self['@elements'].length; i++) {
  1328. if(_st(anObject).__eq(self['@elements'][i])) {
  1329. found = true;
  1330. break;
  1331. }
  1332. }
  1333. if(!!found) {self['@elements'].push(anObject)}
  1334. >
  1335. !
  1336. remove: anObject
  1337. elements remove: anObject
  1338. !
  1339. remove: anObject ifAbsent: aBlock
  1340. elements remove: anObject ifAbsent: aBlock
  1341. !
  1342. removeAll
  1343. elements removeAll
  1344. ! !
  1345. !Set methodsFor: 'comparing'!
  1346. = aCollection
  1347. self class = aCollection class ifFalse: [ ^ false ].
  1348. self size = aCollection size ifFalse: [ ^ false ].
  1349. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1350. ^ true
  1351. ! !
  1352. !Set methodsFor: 'converting'!
  1353. asArray
  1354. ^ elements copy
  1355. ! !
  1356. !Set methodsFor: 'enumerating'!
  1357. collect: aBlock
  1358. ^ self class withAll: (elements collect: aBlock)
  1359. !
  1360. detect: aBlock ifNone: anotherBlock
  1361. ^ elements detect: aBlock ifNone: anotherBlock
  1362. !
  1363. do: aBlock
  1364. elements do: aBlock
  1365. !
  1366. select: aBlock
  1367. | collection |
  1368. collection := self class new.
  1369. self do: [ :each |
  1370. (aBlock value: each) ifTrue: [
  1371. collection add: each ]].
  1372. ^ collection
  1373. ! !
  1374. !Set methodsFor: 'initialization'!
  1375. initialize
  1376. super initialize.
  1377. elements := #()
  1378. ! !
  1379. !Set methodsFor: 'printing'!
  1380. printOn: aStream
  1381. super printOn: aStream.
  1382. aStream nextPutAll: ' ('.
  1383. self
  1384. do: [ :each | each printOn: aStream ]
  1385. separatedBy: [ aStream nextPutAll: ' ' ].
  1386. aStream nextPutAll: ')'
  1387. ! !
  1388. !Set methodsFor: 'testing'!
  1389. includes: anObject
  1390. ^ elements includes: anObject
  1391. ! !
  1392. Object subclass: #Queue
  1393. instanceVariableNames: 'read readIndex write'
  1394. package: 'Kernel-Collections'!
  1395. !Queue commentStamp!
  1396. I am a one-sided queue.
  1397. ## Usage
  1398. Use `#nextPut:` to add items to the queue.
  1399. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1400. ## Implementation notes
  1401. A Queue uses two OrderedCollections inside,
  1402. `read` is at the front, is not modified and only read using `readIndex`.
  1403. `write` is at the back and is appended new items.
  1404. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1405. As a consequence, no data moving is done by me, write appending may do data moving
  1406. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1407. !Queue methodsFor: 'accessing'!
  1408. next
  1409. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1410. !
  1411. nextIfAbsent: aBlock
  1412. | result |
  1413. result := read at: readIndex ifAbsent: [
  1414. write isEmpty ifTrue: [
  1415. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1416. ^ aBlock value ].
  1417. read := write.
  1418. readIndex := 1.
  1419. write := OrderedCollection new.
  1420. read first ].
  1421. read at: readIndex put: nil.
  1422. readIndex := readIndex + 1.
  1423. ^ result
  1424. !
  1425. nextPut: anObject
  1426. write add: anObject
  1427. ! !
  1428. !Queue methodsFor: 'initialization'!
  1429. initialize
  1430. super initialize.
  1431. read := OrderedCollection new.
  1432. write := OrderedCollection new.
  1433. readIndex := 1
  1434. ! !
  1435. Object subclass: #RegularExpression
  1436. instanceVariableNames: ''
  1437. package: 'Kernel-Collections'!
  1438. !RegularExpression commentStamp!
  1439. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1440. !RegularExpression methodsFor: 'evaluating'!
  1441. compile: aString
  1442. <return self.compile(aString)>
  1443. !
  1444. exec: aString
  1445. <return self.exec(aString) || nil>
  1446. !
  1447. test: aString
  1448. <return self.test(aString)>
  1449. ! !
  1450. !RegularExpression class methodsFor: 'instance creation'!
  1451. fromString: aString
  1452. ^ self fromString: aString flag: ''
  1453. !
  1454. fromString: aString flag: anotherString
  1455. <return new RegExp(aString, anotherString)>
  1456. ! !
  1457. Object subclass: #Stream
  1458. instanceVariableNames: 'collection position streamSize'
  1459. package: 'Kernel-Collections'!
  1460. !Stream commentStamp!
  1461. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1462. My instances are read/write streams to the contents sequence collection.!
  1463. !Stream methodsFor: 'accessing'!
  1464. collection
  1465. ^ collection
  1466. !
  1467. contents
  1468. ^ self collection
  1469. copyFrom: 1
  1470. to: self streamSize
  1471. !
  1472. position
  1473. ^ position ifNil: [ position := 0 ]
  1474. !
  1475. position: anInteger
  1476. position := anInteger
  1477. !
  1478. setCollection: aCollection
  1479. collection := aCollection
  1480. !
  1481. setStreamSize: anInteger
  1482. streamSize := anInteger
  1483. !
  1484. size
  1485. ^ self streamSize
  1486. !
  1487. streamSize
  1488. ^ streamSize
  1489. ! !
  1490. !Stream methodsFor: 'actions'!
  1491. close
  1492. !
  1493. flush
  1494. !
  1495. reset
  1496. self position: 0
  1497. !
  1498. resetContents
  1499. self reset.
  1500. self setStreamSize: 0
  1501. ! !
  1502. !Stream methodsFor: 'enumerating'!
  1503. do: aBlock
  1504. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1505. ! !
  1506. !Stream methodsFor: 'positioning'!
  1507. setToEnd
  1508. self position: self size
  1509. !
  1510. skip: anInteger
  1511. self position: ((self position + anInteger) min: self size max: 0)
  1512. ! !
  1513. !Stream methodsFor: 'reading'!
  1514. next
  1515. ^ self atEnd
  1516. ifTrue: [ nil ]
  1517. ifFalse: [
  1518. self position: self position + 1.
  1519. collection at: self position ]
  1520. !
  1521. next: anInteger
  1522. | tempCollection |
  1523. tempCollection := self collection class new.
  1524. anInteger timesRepeat: [
  1525. self atEnd ifFalse: [
  1526. tempCollection add: self next ]].
  1527. ^ tempCollection
  1528. !
  1529. peek
  1530. ^ self atEnd ifFalse: [
  1531. self collection at: self position + 1 ]
  1532. ! !
  1533. !Stream methodsFor: 'testing'!
  1534. atEnd
  1535. ^ self position = self size
  1536. !
  1537. atStart
  1538. ^ self position = 0
  1539. !
  1540. isEmpty
  1541. ^ self size = 0
  1542. ! !
  1543. !Stream methodsFor: 'writing'!
  1544. << anObject
  1545. self write: anObject
  1546. !
  1547. nextPut: anObject
  1548. self position: self position + 1.
  1549. self collection at: self position put: anObject.
  1550. self setStreamSize: (self streamSize max: self position)
  1551. !
  1552. nextPutAll: aCollection
  1553. aCollection do: [ :each |
  1554. self nextPut: each ]
  1555. !
  1556. nextPutString: aString
  1557. self nextPut: aString
  1558. !
  1559. write: anObject
  1560. anObject putOn: self
  1561. ! !
  1562. !Stream class methodsFor: 'instance creation'!
  1563. on: aCollection
  1564. ^ self new
  1565. setCollection: aCollection;
  1566. setStreamSize: aCollection size;
  1567. yourself
  1568. ! !
  1569. Stream subclass: #StringStream
  1570. instanceVariableNames: ''
  1571. package: 'Kernel-Collections'!
  1572. !StringStream commentStamp!
  1573. I am a Stream specific to `String` objects.!
  1574. !StringStream methodsFor: 'reading'!
  1575. next: anInteger
  1576. | tempCollection |
  1577. tempCollection := self collection class new.
  1578. anInteger timesRepeat: [
  1579. self atEnd ifFalse: [
  1580. tempCollection := tempCollection, self next ]].
  1581. ^ tempCollection
  1582. ! !
  1583. !StringStream methodsFor: 'writing'!
  1584. cr
  1585. ^ self nextPutAll: String cr
  1586. !
  1587. crlf
  1588. ^ self nextPutAll: String crlf
  1589. !
  1590. lf
  1591. ^ self nextPutAll: String lf
  1592. !
  1593. nextPut: aString
  1594. self nextPutAll: aString
  1595. !
  1596. nextPutAll: aString
  1597. | pre post |
  1598. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1599. pre := self collection copyFrom: 1 to: self position.
  1600. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1601. self setCollection: pre, aString, post
  1602. ].
  1603. self position: self position + aString size.
  1604. self setStreamSize: (self streamSize max: self position)
  1605. !
  1606. nextPutString: aString
  1607. self nextPutAll: aString
  1608. !
  1609. space
  1610. self nextPut: ' '
  1611. !
  1612. tab
  1613. ^ self nextPutAll: String tab
  1614. ! !