Kernel-Collections.st 48 KB

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