2
0

Kernel-Collections.st 39 KB

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