Kernel-Collections.st 48 KB

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