Kernel-Collections.st 38 KB

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