1
0

Kernel-Collections.st 38 KB

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