Kernel-Collections.st 48 KB

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