Kernel-Collections.st 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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. asJavaScriptSource
  635. ^ String streamContents: [ :str |
  636. str nextPut: '{'.
  637. self keysAndValuesDo: [ :key :value |
  638. str nextPutAll: key asJavaScriptSource; nextPut: ':'; nextPutAll: value asJavaScriptSource; nextPut: ',' ].
  639. str skip: -1; nextPut: '}' ]
  640. !
  641. at: aKey ifAbsent: aBlock
  642. ^ (self includesKey: aKey)
  643. ifTrue: [ self basicAt: aKey ]
  644. ifFalse: [ aBlock value ]
  645. !
  646. at: aKey put: aValue
  647. ^ self basicAt: aKey put: aValue
  648. !
  649. keys
  650. <inlineJS: 'return Object.keys(self)'>
  651. !
  652. values
  653. <inlineJS: '
  654. return $self._keys().map(function(key){
  655. return $self._at_(key);
  656. });
  657. '>
  658. ! !
  659. !HashedCollection methodsFor: 'adding/removing'!
  660. removeKey: aKey ifAbsent: aBlock
  661. ^ self
  662. at: aKey
  663. ifPresent: [ :removed | self basicDelete: aKey. removed ]
  664. ifAbsent: [ aBlock value ]
  665. ! !
  666. !HashedCollection methodsFor: 'enumerating'!
  667. keysDo: aBlock
  668. self keys do: aBlock
  669. !
  670. valuesDo: aBlock
  671. self values do: aBlock
  672. ! !
  673. !HashedCollection methodsFor: 'testing'!
  674. includesKey: aKey
  675. <inlineJS: 'return self.hasOwnProperty(aKey)'>
  676. ! !
  677. Collection subclass: #SequenceableCollection
  678. slots: {}
  679. package: 'Kernel-Collections'!
  680. !SequenceableCollection commentStamp!
  681. I am an IndexableCollection
  682. with numeric indexes starting with 1.!
  683. !SequenceableCollection methodsFor: 'accessing'!
  684. allButFirst
  685. ^ self copyFrom: 2 to: self size
  686. !
  687. allButLast
  688. ^ self copyFrom: 1 to: self size - 1
  689. !
  690. anyOne
  691. ^ self at: 1
  692. !
  693. atRandom
  694. ^ self at: self size atRandom
  695. !
  696. first
  697. ^ self at: 1
  698. !
  699. first: aNumber
  700. "Answer the first `aNumber` elements of the receiver.
  701. Raise an error if there are not enough elements in the receiver."
  702. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  703. ^ self copyFrom: 1 to: aNumber
  704. !
  705. fourth
  706. ^ self at: 4
  707. !
  708. indexOf: anObject startingAt: start
  709. "Answer the index of the first occurence of anElement after start
  710. within the receiver. If the receiver does not contain anElement,
  711. answer 0."
  712. ^ self indexOf: anObject startingAt: start ifAbsent: [ 0 ]
  713. !
  714. indexOf: anObject startingAt: start ifAbsent: aBlock
  715. self subclassResponsibility
  716. !
  717. last
  718. ^ self at: self size
  719. !
  720. last: aNumber
  721. "Answer the last aNumber elements of the receiver.
  722. Raise an error if there are not enough elements in the receiver."
  723. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  724. ^ self copyFrom: self size - aNumber + 1 to: self size
  725. !
  726. second
  727. ^ self at: 2
  728. !
  729. third
  730. ^ self at: 3
  731. ! !
  732. !SequenceableCollection methodsFor: 'adding/removing'!
  733. addLast: anObject
  734. self add: anObject
  735. !
  736. removeLast
  737. ^ self remove: self last
  738. ! !
  739. !SequenceableCollection methodsFor: 'comparing'!
  740. = aCollection
  741. (self class = aCollection class and: [
  742. self size = aCollection size ]) ifFalse: [ ^ false ].
  743. self withIndexDo: [ :each :i |
  744. (aCollection at: i) = each ifFalse: [ ^ false ]].
  745. ^ true
  746. ! !
  747. !SequenceableCollection methodsFor: 'converting'!
  748. reversed
  749. self subclassResponsibility
  750. ! !
  751. !SequenceableCollection methodsFor: 'copying'!
  752. copyFrom: anIndex to: anotherIndex
  753. self subclassResponsibility
  754. !
  755. copyWithFirst: anObject
  756. ^ (self class with: anObject) addAll: self; yourself
  757. ! !
  758. !SequenceableCollection methodsFor: 'enumerating'!
  759. reverseDo: aBlock
  760. self reversed do: aBlock
  761. ! !
  762. !SequenceableCollection methodsFor: 'streaming'!
  763. newStream
  764. ^ self streamClass on: self
  765. !
  766. readStream
  767. "For Pharo compatibility"
  768. ^ self stream
  769. !
  770. stream
  771. ^ self newStream
  772. !
  773. streamClass
  774. ^ self class streamClass
  775. !
  776. writeStream
  777. "For Pharo compatibility"
  778. ^ self stream
  779. ! !
  780. !SequenceableCollection methodsFor: 'testing'!
  781. beginsWith: prefix
  782. self size < prefix size ifTrue: [ ^ false ].
  783. ^ (self first: prefix size) = prefix
  784. !
  785. endsWith: suffix
  786. self size < suffix size ifTrue: [ ^ false ].
  787. ^ (self last: suffix size) = suffix
  788. !
  789. includes: anObject
  790. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  791. ! !
  792. !SequenceableCollection class methodsFor: 'accessing'!
  793. streamClass
  794. ^ Stream
  795. ! !
  796. !SequenceableCollection class methodsFor: 'streaming'!
  797. streamContents: aBlock
  798. | stream |
  799. stream := (self streamClass on: self new).
  800. aBlock value: stream.
  801. ^ stream contents
  802. ! !
  803. SequenceableCollection subclass: #Array
  804. slots: {}
  805. package: 'Kernel-Collections'!
  806. !Array commentStamp!
  807. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  808. I am directly mapped to JavaScript Number.
  809. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  810. !Array methodsFor: 'accessing'!
  811. at: anIndex put: anObject
  812. <inlineJS: 'return self[anIndex - 1] = anObject'>
  813. ! !
  814. !Array methodsFor: 'adding/removing'!
  815. add: anObject
  816. <inlineJS: 'self.push(anObject); return anObject;'>
  817. !
  818. addAll: aCollection
  819. <inlineJS: '
  820. if (Array.isArray(aCollection) && aCollection.length < 65000) self.push.apply(self, aCollection);
  821. else $globals.Array.superclass.fn.prototype._addAll_.call($self, aCollection);
  822. return aCollection;
  823. '>
  824. !
  825. addFirst: anObject
  826. <inlineJS: 'self.unshift(anObject); return anObject;'>
  827. !
  828. remove: anObject ifAbsent: aBlock
  829. | index |
  830. index := self indexOf: anObject ifAbsent: [ 0 ].
  831. ^ index = 0
  832. ifFalse: [ self removeIndex: index. anObject ]
  833. ifTrue: [ aBlock value ]
  834. !
  835. removeAll
  836. <inlineJS: 'self.length = 0'>
  837. !
  838. removeFrom: aNumber to: anotherNumber
  839. <inlineJS: 'self.splice(aNumber -1, anotherNumber - aNumber + 1)'>
  840. !
  841. removeIndex: anInteger
  842. <inlineJS: 'self.splice(anInteger - 1, 1)'>
  843. !
  844. removeLast
  845. <inlineJS: 'return self.pop();'>
  846. ! !
  847. !Array methodsFor: 'converting'!
  848. asJavaScriptSource
  849. ^ '[', ((self collect: [:each | each asJavaScriptSource ]) join: ', '), ']'
  850. !
  851. reversed
  852. <inlineJS: 'return self.slice().reverse()'>
  853. ! !
  854. !Array methodsFor: 'copying'!
  855. copyFrom: anIndex to: anotherIndex
  856. <inlineJS: '
  857. if (anIndex >= 1 && anotherIndex <= self.length) {
  858. return self.slice(anIndex - 1, anotherIndex);
  859. } else {
  860. self._at_(anIndex);
  861. self._at_(self.length + 1);
  862. throw new Error("Incorrect indexes in #copyFrom:to: not caught by #at:");
  863. }
  864. '>
  865. !
  866. shallowCopy
  867. <inlineJS: 'return self.slice()'>
  868. ! !
  869. !Array methodsFor: 'enumerating'!
  870. allIn: aBlock
  871. ^ aBlock valueWithPossibleArguments:
  872. "collect to match #in: behaviour"
  873. (self collect: [ :each | each in: [ :x | x ] ])
  874. !
  875. collect: aBlock
  876. "Optimized version"
  877. <inlineJS: 'return self.map(function(each) {return aBlock._value_(each)})'>
  878. !
  879. join: aString
  880. <inlineJS: 'return self.join(aString)'>
  881. !
  882. select: aBlock
  883. "Optimized version"
  884. <inlineJS: 'return self.filter(function(each) {return aBlock._value_(each)})'>
  885. !
  886. sort
  887. ^ self sort: [ :a :b | a < b ]
  888. !
  889. sort: aBlock
  890. <inlineJS: '
  891. return self.sort(function(a, b) {
  892. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  893. })
  894. '>
  895. !
  896. sorted
  897. ^ self copy sort
  898. !
  899. sorted: aBlock
  900. ^ self copy sort: aBlock
  901. ! !
  902. !Array methodsFor: 'printing'!
  903. printOn: aStream
  904. super printOn: aStream.
  905. aStream nextPutAll: ' ('.
  906. self
  907. do: [ :each | each printOn: aStream ]
  908. separatedBy: [ aStream nextPutAll: ' ' ].
  909. aStream nextPutAll: ')'
  910. ! !
  911. !Array class methodsFor: 'instance creation'!
  912. new: anInteger
  913. <inlineJS: 'return new Array(anInteger)'>
  914. !
  915. with: anObject
  916. ^ (self new: 1)
  917. at: 1 put: anObject;
  918. yourself
  919. !
  920. with: anObject with: anObject2
  921. ^ (self new: 2)
  922. at: 1 put: anObject;
  923. at: 2 put: anObject2;
  924. yourself
  925. !
  926. with: anObject with: anObject2 with: anObject3
  927. ^ (self new: 3)
  928. at: 1 put: anObject;
  929. at: 2 put: anObject2;
  930. at: 3 put: anObject3;
  931. yourself
  932. !
  933. withAll: aCollection
  934. | instance index |
  935. index := 1.
  936. instance := self new: aCollection size.
  937. aCollection do: [ :each |
  938. instance at: index put: each.
  939. index := index + 1 ].
  940. ^ instance
  941. ! !
  942. SequenceableCollection subclass: #String
  943. slots: {}
  944. package: 'Kernel-Collections'!
  945. !String commentStamp!
  946. 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.
  947. String inherits many useful methods from its hierarchy, such as
  948. `Collection >> #,`!
  949. !String methodsFor: 'accessing'!
  950. asciiValue
  951. <inlineJS: 'return self.charCodeAt(0);'>
  952. !
  953. at: anIndex ifAbsent: aBlock
  954. <inlineJS: 'return String(self)[anIndex - 1] || aBlock._value()'>
  955. !
  956. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  957. <inlineJS: '
  958. var result = String(self)[anIndex - 1];
  959. return result ? aBlock._value_(result) : anotherBlock._value();
  960. '>
  961. !
  962. at: anIndex put: anObject
  963. self errorReadOnly
  964. !
  965. charCodeAt: anInteger
  966. <inlineJS: 'return self.charCodeAt(anInteger - 1)'>
  967. ! !
  968. !String methodsFor: 'adding/removing'!
  969. add: anObject
  970. self errorReadOnly
  971. !
  972. remove: anObject
  973. self errorReadOnly
  974. !
  975. remove: anObject ifAbsent: aBlock
  976. self errorReadOnly
  977. ! !
  978. !String methodsFor: 'comparing'!
  979. < aString
  980. <inlineJS: 'return String(self) < aString._asString()'>
  981. !
  982. <= aString
  983. <inlineJS: 'return String(self) <= aString._asString()'>
  984. !
  985. = aString
  986. <inlineJS:
  987. 'return aString !!= null && String(self) === (typeof aString === "string" ? aString : aString.valueOf())'>
  988. !
  989. == aString
  990. <inlineJS: '
  991. if (typeof aString === "string") return String(self) === aString;
  992. else if (aString !!= null && typeof aString === "object") return String(self) === aString.valueOf();
  993. else return false;
  994. '>
  995. !
  996. > aString
  997. <inlineJS: 'return String(self) > aString._asString()'>
  998. !
  999. >= aString
  1000. <inlineJS: 'return String(self) >= aString._asString()'>
  1001. ! !
  1002. !String methodsFor: 'converting'!
  1003. asJavaScriptMethodName
  1004. <inlineJS: 'return $core.st2js(self)'>
  1005. !
  1006. asJavaScriptObject
  1007. ^ self
  1008. !
  1009. asJavaScriptSource
  1010. <inlineJS: '
  1011. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1012. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1013. else
  1014. return "\"" + self + "\"";
  1015. '>
  1016. !
  1017. asLowercase
  1018. <inlineJS: 'return self.toLowerCase()'>
  1019. !
  1020. asMutator
  1021. "Answer a setter selector. For example,
  1022. #name asMutator returns #name:"
  1023. self last = ':' ifFalse: [ ^ self, ':' ].
  1024. ^ self
  1025. !
  1026. asNumber
  1027. <inlineJS: 'return Number(self)'>
  1028. !
  1029. asRegexp
  1030. ^ RegularExpression fromString: self
  1031. !
  1032. asString
  1033. ^ self
  1034. !
  1035. asSymbol
  1036. ^ self
  1037. !
  1038. asUppercase
  1039. <inlineJS: 'return self.toUpperCase()'>
  1040. !
  1041. capitalized
  1042. ^ self ifNotEmpty: [ self first asUppercase, self allButFirst ]
  1043. !
  1044. crlfSanitized
  1045. ^ self lines join: String lf
  1046. !
  1047. escaped
  1048. <inlineJS: 'return escape(self)'>
  1049. !
  1050. reversed
  1051. <inlineJS: 'return self.split("").reverse().join("")'>
  1052. !
  1053. unescaped
  1054. <inlineJS: 'return unescape(self)'>
  1055. !
  1056. uriComponentDecoded
  1057. <inlineJS: 'return decodeURIComponent(self)'>
  1058. !
  1059. uriComponentEncoded
  1060. <inlineJS: 'return encodeURIComponent(self)'>
  1061. !
  1062. uriDecoded
  1063. <inlineJS: 'return decodeURI(self)'>
  1064. !
  1065. uriEncoded
  1066. <inlineJS: 'return encodeURI(self)'>
  1067. ! !
  1068. !String methodsFor: 'copying'!
  1069. , aString
  1070. <inlineJS: 'return String(self) + aString'>
  1071. !
  1072. copyFrom: anIndex to: anotherIndex
  1073. <inlineJS: 'return self.substring(anIndex - 1, anotherIndex)'>
  1074. !
  1075. copyWithFirst: anObject
  1076. (anObject isString and: [ anObject size = 1 ]) "character is one-char string in JS"
  1077. ifFalse: [ self error: 'Cannot put ', anObject class name, ' in a String' ].
  1078. ^ anObject, self
  1079. !
  1080. deepCopy
  1081. ^ self shallowCopy
  1082. !
  1083. shallowCopy
  1084. ^ self
  1085. ! !
  1086. !String methodsFor: 'error handling'!
  1087. errorReadOnly
  1088. self error: 'Object is read-only'
  1089. ! !
  1090. !String methodsFor: 'evaluating'!
  1091. value: anObject
  1092. ^ anObject perform: self
  1093. ! !
  1094. !String methodsFor: 'printing'!
  1095. asSymbolPrintOn: aStream
  1096. aStream nextPutAll: '#'.
  1097. self asString isSelector
  1098. ifTrue: [ aStream nextPut: self ]
  1099. ifFalse: [ self printOn: aStream ]
  1100. !
  1101. printNl
  1102. <inlineJS: 'console.log(self)'>
  1103. !
  1104. printOn: aStream
  1105. aStream
  1106. nextPutAll: '''';
  1107. nextPutAll: (self replace: '''' with: '''''');
  1108. nextPutAll: ''''
  1109. !
  1110. symbolPrintString
  1111. ^ String streamContents: [ :str | self asSymbolPrintOn: str ]
  1112. ! !
  1113. !String methodsFor: 'regular expressions'!
  1114. match: aRegexp
  1115. <inlineJS: 'return self.search(aRegexp) !!= -1'>
  1116. !
  1117. matchesOf: aRegularExpression
  1118. <inlineJS: 'return self.match(aRegularExpression)'>
  1119. !
  1120. replace: aString with: anotherString
  1121. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1122. !
  1123. replaceRegexp: aRegexp with: aString
  1124. <inlineJS: 'return self.replace(aRegexp, aString)'>
  1125. !
  1126. trimBoth
  1127. ^ self trimBoth: '\s'
  1128. !
  1129. trimBoth: separators
  1130. ^ (self trimLeft: separators) trimRight: separators
  1131. !
  1132. trimLeft
  1133. ^ self trimLeft: '\s'
  1134. !
  1135. trimLeft: separators
  1136. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1137. !
  1138. trimRight
  1139. ^ self trimRight: '\s'
  1140. !
  1141. trimRight: separators
  1142. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1143. ! !
  1144. !String methodsFor: 'split join'!
  1145. join: aCollection
  1146. ^ String
  1147. streamContents: [ :stream | aCollection
  1148. do: [ :each | stream nextPutAll: each asString ]
  1149. separatedBy: [ stream nextPutAll: self ]]
  1150. !
  1151. lineIndicesDo: aBlock
  1152. "execute aBlock with 3 arguments for each line:
  1153. - start index of line
  1154. - end index of line without line delimiter
  1155. - end index of line including line delimiter(s) CR, LF or CRLF"
  1156. | cr lf start sz nextLF nextCR |
  1157. start := 1.
  1158. sz := self size.
  1159. cr := String cr.
  1160. nextCR := self indexOf: cr startingAt: 1.
  1161. lf := String lf.
  1162. nextLF := self indexOf: lf startingAt: 1.
  1163. [ start <= sz ] whileTrue: [
  1164. (nextLF = 0 and: [ nextCR = 0 ])
  1165. ifTrue: [ "No more CR, nor LF, the string is over"
  1166. aBlock value: start value: sz value: sz.
  1167. ^ self ].
  1168. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1169. ifTrue: [ "Found a LF"
  1170. aBlock value: start value: nextLF - 1 value: nextLF.
  1171. start := 1 + nextLF.
  1172. nextLF := self indexOf: lf startingAt: start ]
  1173. ifFalse: [ 1 + nextCR = nextLF
  1174. ifTrue: [ "Found a CR-LF pair"
  1175. aBlock value: start value: nextCR - 1 value: nextLF.
  1176. start := 1 + nextLF.
  1177. nextCR := self indexOf: cr startingAt: start.
  1178. nextLF := self indexOf: lf startingAt: start ]
  1179. ifFalse: [ "Found a CR"
  1180. aBlock value: start value: nextCR - 1 value: nextCR.
  1181. start := 1 + nextCR.
  1182. nextCR := self indexOf: cr startingAt: start ] ]]
  1183. !
  1184. lineNumber: anIndex
  1185. "Answer a string containing the characters in the given line number."
  1186. | lineCount |
  1187. lineCount := 0.
  1188. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1189. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1190. ^ nil
  1191. !
  1192. lines
  1193. "Answer an array of lines composing this receiver without the line ending delimiters."
  1194. <inlineJS: '
  1195. var result = self.split(/\r\n|\r|\n/);
  1196. if (!!result[result.length-1]) result.pop();
  1197. return result;
  1198. '>
  1199. !
  1200. linesDo: aBlock
  1201. "Execute aBlock with each line in this string. The terminating line
  1202. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1203. self lines do: aBlock
  1204. !
  1205. subStrings: aString
  1206. ^ self tokenize: aString
  1207. !
  1208. tokenize: aString
  1209. <inlineJS: 'return self.split(aString)'>
  1210. ! !
  1211. !String methodsFor: 'streaming'!
  1212. putOn: aStream
  1213. aStream nextPutString: self
  1214. ! !
  1215. !String methodsFor: 'testing'!
  1216. includesSubString: subString
  1217. <inlineJS: 'return self.indexOf(subString) !!= -1'>
  1218. !
  1219. isCapitalized
  1220. ^ self first asUppercase == self first
  1221. !
  1222. isImmutable
  1223. ^ true
  1224. !
  1225. isSelector
  1226. <inlineJS:
  1227. 'return !!!!self.match(/^([a-zA-Z][a-zA-Z0-9]*|[\\+*/=><,@%~|&-]+|([a-zA-Z][a-zA-Z0-9]*\:)+)$/)'
  1228. >
  1229. !
  1230. isString
  1231. ^ true
  1232. !
  1233. isVowel
  1234. "Answer true if the receiver is a one character string containing a voyel"
  1235. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1236. ! !
  1237. !String class methodsFor: 'accessing'!
  1238. cr
  1239. <inlineJS: 'return "\r"'>
  1240. !
  1241. crlf
  1242. <inlineJS: 'return "\r\n"'>
  1243. !
  1244. esc
  1245. ^ self fromCharCode: 27
  1246. !
  1247. lf
  1248. <inlineJS: 'return "\n"'>
  1249. !
  1250. space
  1251. <inlineJS: 'return " "'>
  1252. !
  1253. streamClass
  1254. ^ StringStream
  1255. !
  1256. tab
  1257. <inlineJS: 'return "\t"'>
  1258. ! !
  1259. !String class methodsFor: 'instance creation'!
  1260. fromCharCode: anInteger
  1261. <inlineJS: 'return String.fromCharCode(anInteger)'>
  1262. !
  1263. fromString: aString
  1264. <inlineJS: 'return String(aString)'>
  1265. !
  1266. value: aUTFCharCode
  1267. <inlineJS: 'return String.fromCharCode(aUTFCharCode);'>
  1268. ! !
  1269. !String class methodsFor: 'random'!
  1270. random
  1271. "Returns random alphanumeric string beginning with letter"
  1272. <inlineJS: 'return ((10+22*Math.random())/32).toString(32).slice(2);'>
  1273. !
  1274. randomNotIn: aString
  1275. | result |
  1276. [ result := self random. aString includesSubString: result ] whileTrue.
  1277. ^ result
  1278. ! !
  1279. Collection subclass: #Set
  1280. slots: {#defaultBucket. #slowBucketStores. #fastBuckets. #size}
  1281. package: 'Kernel-Collections'!
  1282. !Set commentStamp!
  1283. I represent an unordered set of objects without duplicates.
  1284. ## Implementation notes
  1285. I put elements into different stores based on their type.
  1286. The goal is to store some elements into native JS object property names to be fast.
  1287. If an unboxed element has typeof 'string', 'boolean' or 'number', or an element is nil, null or undefined,
  1288. I store it as a property name in an empty (== Object.create(null)) JS object, different for each type
  1289. (for simplicity, nil/null/undefined is treated as one and included with the two booleans).
  1290. If element happen to be an object, I try to store them in `ArrayBucketStore`. I have two of them by default,
  1291. one hashed using the Smalltalk class name, the other one using the JS constructor name. It is possible to have more or less
  1292. instances of `ArrayBucketStores`, see `#initializeSlowBucketStores`.
  1293. As a last resort, if none of the `ArrayBucketStore` instances can find a suitable bucket, the `defaultBucket` is used,
  1294. which is an `Array`.!
  1295. !Set methodsFor: 'accessing'!
  1296. size
  1297. ^ size
  1298. ! !
  1299. !Set methodsFor: 'adding/removing'!
  1300. add: anObject
  1301. | bucket |
  1302. bucket := self bucketsOfElement: anObject.
  1303. ^ bucket second
  1304. ifNil: [
  1305. | object slowBucket |
  1306. object := bucket first.
  1307. slowBucket := bucket third.
  1308. slowBucket
  1309. indexOf: object
  1310. ifAbsent: [
  1311. slowBucket add: object.
  1312. size := size + 1 ].
  1313. object ]
  1314. ifNotNil: [ :primitiveBucket |
  1315. self
  1316. add: bucket first
  1317. in: primitiveBucket ]
  1318. !
  1319. remove: anObject ifAbsent: aBlock
  1320. | bucket |
  1321. bucket := self bucketsOfElement: anObject.
  1322. ^ bucket second
  1323. ifNil: [ | obj | obj := bucket first. bucket third remove: obj ifAbsent: [ ^aBlock value ]. size := size - 1. obj ]
  1324. ifNotNil: [ :primitiveBucket | self remove: bucket first in: primitiveBucket ifAbsent: aBlock ]
  1325. !
  1326. removeAll
  1327. <inlineJS: '
  1328. $self.fastBuckets = {
  1329. "boolean": { store: Object.create(null), fn: function (x) { return {"true": true, "false": false, "null": null}[x]; } },
  1330. "number": { store: Object.create(null), fn: Number },
  1331. "string": { store: Object.create(null) }
  1332. };
  1333. $self.slowBucketStores.forEach(function (x) { x._removeAll(); });
  1334. $self.defaultBucket._removeAll();
  1335. $self.size = 0;
  1336. '>
  1337. ! !
  1338. !Set methodsFor: 'comparing'!
  1339. = aCollection
  1340. self class = aCollection class ifFalse: [ ^ false ].
  1341. self size = aCollection size ifFalse: [ ^ false ].
  1342. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1343. ^ true
  1344. ! !
  1345. !Set methodsFor: 'enumerating'!
  1346. collect: aBlock
  1347. | collection |
  1348. collection := self class new.
  1349. self do: [ :each | collection add: (aBlock value: each) ].
  1350. ^ collection
  1351. !
  1352. do: aBlock
  1353. <inlineJS: '
  1354. var el, keys, i;
  1355. el = $self.fastBuckets;
  1356. keys = Object.keys(el);
  1357. for (i = 0; i < keys.length; ++i) {
  1358. var fastBucket = el[keys[i]], fn = fastBucket.fn, store = Object.keys(fastBucket.store);
  1359. if (fn) { for (var j = 0; j < store.length; ++j) { aBlock._value_(fn(store[j])); } }
  1360. else { store._do_(aBlock); }
  1361. }
  1362. el = $self.slowBucketStores;
  1363. for (i = 0; i < el.length; ++i) { el[i]._do_(aBlock); }
  1364. $self.defaultBucket._do_(aBlock);
  1365. '>
  1366. !
  1367. select: aBlock
  1368. | collection |
  1369. collection := self class new.
  1370. self do: [ :each |
  1371. (aBlock value: each) ifTrue: [
  1372. collection add: each ] ].
  1373. ^ collection
  1374. !
  1375. select: selectBlock thenCollect: collectBlock
  1376. | collection |
  1377. collection := self class new.
  1378. self do: [ :each |
  1379. (selectBlock value: each) ifTrue: [
  1380. collection add: (collectBlock value: each) ] ].
  1381. ^ collection
  1382. ! !
  1383. !Set methodsFor: 'initialization'!
  1384. initialize
  1385. super initialize.
  1386. defaultBucket := #().
  1387. self
  1388. initializeSlowBucketStores;
  1389. removeAll
  1390. !
  1391. initializeSlowBucketStores
  1392. slowBucketStores := {
  1393. ArrayBucketStore hashBlock: [ :x | self classNameOf: x ].
  1394. ArrayBucketStore hashBlock: [ :x | self jsConstructorNameOf: x ]
  1395. }
  1396. ! !
  1397. !Set methodsFor: 'printing'!
  1398. printOn: aStream
  1399. super printOn: aStream.
  1400. aStream nextPutAll: ' ('.
  1401. self
  1402. do: [ :each | each printOn: aStream ]
  1403. separatedBy: [ aStream nextPutAll: ' ' ].
  1404. aStream nextPutAll: ')'
  1405. ! !
  1406. !Set methodsFor: 'private'!
  1407. add: anObject in: anotherObject
  1408. <inlineJS: '
  1409. if (anObject in anotherObject.store) { return anObject; }
  1410. $self.size++;
  1411. anotherObject.store[anObject] = true;
  1412. return anObject;
  1413. '>
  1414. !
  1415. bucketsOfElement: anObject
  1416. "Find the appropriate bucket for `anObject`.
  1417. For optimization purposes, directly answer an array with:
  1418. - the object to be store
  1419. - the primitive bucket
  1420. - the slow bucket"
  1421. <inlineJS: '
  1422. // include nil to well-known objects under "boolean" fastBucket
  1423. if (anObject == null || anObject.a$nil) return [ null, $self.fastBuckets.boolean ];
  1424. var prim = anObject.valueOf();
  1425. if (typeof prim === "object" || typeof prim === "function" || !!$self.fastBuckets[typeof prim]) {
  1426. var bucket = null;
  1427. $self.slowBucketStores.some(function (store) {
  1428. return bucket = store._bucketOfElement_(anObject);
  1429. });
  1430. return [ anObject, null, bucket || $self.defaultBucket ];
  1431. }
  1432. return [ prim, $self.fastBuckets[typeof prim] ];
  1433. '>
  1434. !
  1435. classNameOf: anObject
  1436. "Answer the class name of `anObject`, or `undefined`
  1437. if `anObject` is not an Smalltalk object"
  1438. <inlineJS: 'return anObject.a$cls !!= null && anObject.a$cls.name'>
  1439. !
  1440. includes: anObject in: anotherObject
  1441. <inlineJS: 'return anObject in anotherObject.store'>
  1442. !
  1443. jsConstructorNameOf: anObject
  1444. <inlineJS: 'return anObject.constructor && anObject.constructor.name'>
  1445. !
  1446. remove: anObject in: anotherObject ifAbsent: aBlock
  1447. <inlineJS: '
  1448. if (anObject in anotherObject.store) {
  1449. delete anotherObject.store[anObject];
  1450. $self.size--;
  1451. return anObject;
  1452. } else {
  1453. return aBlock._value();
  1454. }'>
  1455. ! !
  1456. !Set methodsFor: 'testing'!
  1457. includes: anObject
  1458. | bucket |
  1459. bucket := self bucketsOfElement: anObject.
  1460. ^ bucket second
  1461. ifNil: [ bucket third includes: bucket first ]
  1462. ifNotNil: [ :primitiveBucket | self includes: bucket first in: primitiveBucket ]
  1463. ! !
  1464. Object subclass: #ProtoStream
  1465. slots: {}
  1466. package: 'Kernel-Collections'!
  1467. !ProtoStream commentStamp!
  1468. I am the abstract base for different accessor for a sequence of objects. This sequence is referred to as my "contents".
  1469. My instances are read/write streams modifying the contents.!
  1470. !ProtoStream methodsFor: 'accessing'!
  1471. contents
  1472. self subclassResponsibility
  1473. ! !
  1474. !ProtoStream methodsFor: 'actions'!
  1475. reset
  1476. self subclassResponsibility
  1477. !
  1478. resetContents
  1479. self subclassResponsibility
  1480. ! !
  1481. !ProtoStream methodsFor: 'enumerating'!
  1482. do: aBlock
  1483. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1484. ! !
  1485. !ProtoStream methodsFor: 'positioning'!
  1486. setToEnd
  1487. self subclassResponsibility
  1488. !
  1489. setToStart
  1490. self reset
  1491. ! !
  1492. !ProtoStream methodsFor: 'reading'!
  1493. next
  1494. ^ self atEnd
  1495. ifTrue: [ nil ]
  1496. ifFalse: [ self subclassResponsibility ]
  1497. !
  1498. peek
  1499. ^ self atEnd
  1500. ifTrue: [ nil ]
  1501. ifFalse: [ self subclassResponsibility ]
  1502. ! !
  1503. !ProtoStream methodsFor: 'testing'!
  1504. atEnd
  1505. self subclassResponsibility
  1506. !
  1507. atStart
  1508. self subclassResponsibility
  1509. !
  1510. isEmpty
  1511. ^ self atStart and: [ self atEnd ]
  1512. ! !
  1513. !ProtoStream methodsFor: 'writing'!
  1514. << anObject
  1515. self write: anObject
  1516. !
  1517. nextPut: anObject
  1518. self subclassResponsibility
  1519. !
  1520. nextPutAll: aCollection
  1521. aCollection do: [ :each |
  1522. self nextPut: each ]
  1523. !
  1524. nextPutString: aString
  1525. self nextPut: aString
  1526. !
  1527. write: anObject
  1528. anObject putOn: self
  1529. ! !
  1530. !ProtoStream class methodsFor: 'instance creation'!
  1531. on: aCollection
  1532. ^ self new
  1533. setCollection: aCollection;
  1534. setStreamSize: aCollection size;
  1535. yourself
  1536. ! !
  1537. ProtoStream subclass: #Stream
  1538. slots: {#collection. #position. #streamSize}
  1539. package: 'Kernel-Collections'!
  1540. !Stream commentStamp!
  1541. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1542. My instances are read/write streams to the contents sequence collection.!
  1543. !Stream methodsFor: 'accessing'!
  1544. collection
  1545. ^ collection
  1546. !
  1547. contents
  1548. ^ self collection
  1549. copyFrom: 1
  1550. to: self streamSize
  1551. !
  1552. position
  1553. ^ position ifNil: [ position := 0 ]
  1554. !
  1555. position: anInteger
  1556. position := anInteger
  1557. !
  1558. setCollection: aCollection
  1559. collection := aCollection
  1560. !
  1561. setStreamSize: anInteger
  1562. streamSize := anInteger
  1563. !
  1564. size
  1565. ^ self streamSize
  1566. !
  1567. streamSize
  1568. ^ streamSize
  1569. ! !
  1570. !Stream methodsFor: 'actions'!
  1571. close
  1572. !
  1573. flush
  1574. !
  1575. reset
  1576. self position: 0
  1577. !
  1578. resetContents
  1579. self reset.
  1580. self setStreamSize: 0
  1581. ! !
  1582. !Stream methodsFor: 'positioning'!
  1583. setToEnd
  1584. self position: self size
  1585. !
  1586. skip: anInteger
  1587. self position: ((self position + anInteger) min: self size max: 0)
  1588. ! !
  1589. !Stream methodsFor: 'reading'!
  1590. next
  1591. ^ self atEnd
  1592. ifTrue: [ nil ]
  1593. ifFalse: [
  1594. self position: self position + 1.
  1595. collection at: self position ]
  1596. !
  1597. next: anInteger
  1598. | tempCollection |
  1599. tempCollection := self collection class new.
  1600. anInteger timesRepeat: [
  1601. self atEnd ifFalse: [
  1602. tempCollection add: self next ]].
  1603. ^ tempCollection
  1604. !
  1605. peek
  1606. ^ self atEnd ifFalse: [
  1607. self collection at: self position + 1 ]
  1608. ! !
  1609. !Stream methodsFor: 'testing'!
  1610. atEnd
  1611. ^ self position = self size
  1612. !
  1613. atStart
  1614. ^ self position = 0
  1615. !
  1616. isEmpty
  1617. ^ self size = 0
  1618. ! !
  1619. !Stream methodsFor: 'writing'!
  1620. nextPut: anObject
  1621. self position: self position + 1.
  1622. self collection at: self position put: anObject.
  1623. self setStreamSize: (self streamSize max: self position)
  1624. ! !
  1625. !Stream class methodsFor: 'instance creation'!
  1626. on: aCollection
  1627. ^ self new
  1628. setCollection: aCollection;
  1629. setStreamSize: aCollection size;
  1630. yourself
  1631. ! !
  1632. Stream subclass: #StringStream
  1633. slots: {}
  1634. package: 'Kernel-Collections'!
  1635. !StringStream commentStamp!
  1636. I am a Stream specific to `String` objects.!
  1637. !StringStream methodsFor: 'reading'!
  1638. next: anInteger
  1639. | tempCollection |
  1640. tempCollection := self collection class new.
  1641. anInteger timesRepeat: [
  1642. self atEnd ifFalse: [
  1643. tempCollection := tempCollection, self next ]].
  1644. ^ tempCollection
  1645. ! !
  1646. !StringStream methodsFor: 'writing'!
  1647. cr
  1648. ^ self nextPutAll: String cr
  1649. !
  1650. crlf
  1651. ^ self nextPutAll: String crlf
  1652. !
  1653. lf
  1654. ^ self nextPutAll: String lf
  1655. !
  1656. nextPut: aString
  1657. self nextPutAll: aString
  1658. !
  1659. nextPutAll: aString
  1660. | pre post |
  1661. self position = self collection size ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1662. pre := self collection copyFrom: 1 to: self position.
  1663. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1664. self setCollection: pre, aString, post
  1665. ].
  1666. self position: self position + aString size.
  1667. self setStreamSize: (self streamSize max: self position)
  1668. !
  1669. nextPutString: aString
  1670. self nextPutAll: aString
  1671. !
  1672. print: anObject
  1673. anObject printOn: self
  1674. !
  1675. printSymbol: anObject
  1676. anObject asSymbolPrintOn: self
  1677. !
  1678. space
  1679. self nextPut: ' '
  1680. !
  1681. tab
  1682. ^ self nextPutAll: String tab
  1683. ! !
  1684. Object subclass: #Queue
  1685. slots: {#read. #readIndex. #write}
  1686. package: 'Kernel-Collections'!
  1687. !Queue commentStamp!
  1688. I am a one-sided queue.
  1689. ## Usage
  1690. Use `#nextPut:` to add items to the queue.
  1691. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1692. ## Implementation notes
  1693. A Queue uses two OrderedCollections inside,
  1694. `read` is at the front, is not modified and only read using `readIndex`.
  1695. `write` is at the back and is appended new items.
  1696. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1697. As a consequence, no data moving is done by me, write appending may do data moving
  1698. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1699. !Queue methodsFor: 'accessing'!
  1700. next
  1701. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1702. !
  1703. nextIfAbsent: aBlock
  1704. | result |
  1705. result := read at: readIndex ifAbsent: [
  1706. write ifEmpty: [
  1707. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1708. ^ aBlock value ].
  1709. read := write.
  1710. readIndex := 1.
  1711. write := OrderedCollection new.
  1712. read first ].
  1713. read at: readIndex put: nil.
  1714. readIndex := readIndex + 1.
  1715. ^ result
  1716. !
  1717. nextPut: anObject
  1718. write add: anObject
  1719. ! !
  1720. !Queue methodsFor: 'initialization'!
  1721. initialize
  1722. super initialize.
  1723. read := OrderedCollection new.
  1724. write := OrderedCollection new.
  1725. readIndex := 1
  1726. ! !
  1727. Object subclass: #RegularExpression
  1728. slots: {}
  1729. package: 'Kernel-Collections'!
  1730. !RegularExpression commentStamp!
  1731. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1732. !RegularExpression methodsFor: 'evaluating'!
  1733. compile: aString
  1734. <inlineJS: 'return self.compile(aString)'>
  1735. !
  1736. exec: aString
  1737. <inlineJS: 'return self.exec(aString) || nil'>
  1738. !
  1739. test: aString
  1740. <inlineJS: 'return self.test(aString)'>
  1741. ! !
  1742. !RegularExpression class methodsFor: 'instance creation'!
  1743. fromString: aString
  1744. ^ self fromString: aString flag: ''
  1745. !
  1746. fromString: aString flag: anotherString
  1747. <inlineJS: 'return new RegExp(aString, anotherString)'>
  1748. ! !
  1749. Trait named: #TKeyValueCollection
  1750. package: 'Kernel-Collections'!
  1751. !TKeyValueCollection methodsFor: 'accessing'!
  1752. at: anIndex
  1753. "Lookup the given index in the receiver.
  1754. If it is present, answer the value stored at anIndex.
  1755. Otherwise, raise an error."
  1756. ^ self at: anIndex ifAbsent: [ self errorNotFound ]
  1757. !
  1758. at: anIndex ifAbsent: aBlock
  1759. "Lookup the given index in the receiver.
  1760. If it is present, answer the value stored at anIndex.
  1761. Otherwise, answer the value of aBlock."
  1762. self subclassResponsibility
  1763. !
  1764. at: aKey ifAbsentPut: aBlock
  1765. ^ self at: aKey ifAbsent: [
  1766. self at: aKey put: aBlock value ]
  1767. !
  1768. at: anIndex ifPresent: aBlock
  1769. "Lookup the given index in the receiver.
  1770. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  1771. Otherwise, answer nil."
  1772. ^ self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  1773. !
  1774. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1775. "Lookup the given index in the receiver.
  1776. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  1777. Otherwise, answer the value of anotherBlock."
  1778. self subclassResponsibility
  1779. !
  1780. at: anIndex put: anObject
  1781. "Store anObject under the given index in the receiver."
  1782. self subclassResponsibility
  1783. !
  1784. indexOf: anObject
  1785. "Lookup index at which anObject is stored in the receiver.
  1786. If not present, raise an error."
  1787. ^ self indexOf: anObject ifAbsent: [ self errorNotFound ]
  1788. !
  1789. indexOf: anObject ifAbsent: aBlock
  1790. "Lookup index at which anObject is stored in the receiver.
  1791. If not present, return value of executing aBlock."
  1792. self subclassResponsibility
  1793. ! !
  1794. !TKeyValueCollection methodsFor: 'enumerating'!
  1795. with: anotherCollection do: aBlock
  1796. "Calls aBlock with every value from self
  1797. and with indetically-indexed value from anotherCollection"
  1798. self withIndexDo: [ :each :index |
  1799. aBlock value: each value: (anotherCollection at: index) ]
  1800. !
  1801. withIndexDo: aBlock
  1802. "Calls aBlock with every value from self
  1803. and with its index as the second argument"
  1804. self subclassResponsibility
  1805. ! !
  1806. Trait named: #TNativeZeroBasedCollection
  1807. package: 'Kernel-Collections'!
  1808. !TNativeZeroBasedCollection methodsFor: 'accessing'!
  1809. at: anIndex ifAbsent: aBlock
  1810. <inlineJS: '
  1811. return anIndex >= 1 && anIndex <= self.length
  1812. ? self[anIndex - 1]
  1813. : aBlock._value()
  1814. '>
  1815. !
  1816. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1817. <inlineJS: '
  1818. return anIndex >= 1 && anIndex <= self.length
  1819. ? aBlock._value_(self[anIndex - 1])
  1820. : anotherBlock._value()
  1821. '>
  1822. !
  1823. indexOf: anObject ifAbsent: aBlock
  1824. <inlineJS: '
  1825. for(var i=0; i < self.length; i++) {
  1826. if($recv(self[i]).__eq(anObject)) {return i+1}
  1827. };
  1828. return aBlock._value();
  1829. '>
  1830. !
  1831. indexOf: anObject startingAt: start ifAbsent: aBlock
  1832. <inlineJS: '
  1833. for(var i=start - 1; i < self.length; i++){
  1834. if($recv(self[i]).__eq(anObject)) {return i+1}
  1835. }
  1836. return aBlock._value();
  1837. '>
  1838. !
  1839. single
  1840. <inlineJS: '
  1841. if (self.length == 0) throw new Error("Collection is empty");
  1842. if (self.length > 1) throw new Error("Collection holds more than one element.");
  1843. return self[0];
  1844. '>
  1845. !
  1846. size
  1847. <inlineJS: 'return self.length'>
  1848. ! !
  1849. !TNativeZeroBasedCollection methodsFor: 'enumerating'!
  1850. detect: aBlock ifNone: anotherBlock
  1851. <inlineJS: '
  1852. for(var i = 0; i < self.length; i++)
  1853. if(aBlock._value_(self[i]))
  1854. return self[i];
  1855. return anotherBlock._value();
  1856. '>
  1857. !
  1858. do: aBlock
  1859. <inlineJS: '
  1860. for(var i=0; i < self.length; i++) {
  1861. aBlock._value_(self[i]);
  1862. }
  1863. '>
  1864. !
  1865. with: anotherCollection do: aBlock
  1866. <inlineJS: '
  1867. $recv(anotherCollection)._first_(0); // #guardSequenceableCollection
  1868. for(var i=0; i<self.length; i++) {
  1869. aBlock._value_value_(self[i], anotherCollection[i]);
  1870. }
  1871. '>
  1872. !
  1873. withIndexDo: aBlock
  1874. <inlineJS: '
  1875. for(var i=0; i < self.length; i++) {
  1876. aBlock._value_value_(self[i], i+1);
  1877. }
  1878. '>
  1879. ! !
  1880. AssociativeCollection setTraitComposition: {TKeyValueCollection} asTraitComposition!
  1881. SequenceableCollection setTraitComposition: {TKeyValueCollection} asTraitComposition!
  1882. Array setTraitComposition: {TNativeZeroBasedCollection} asTraitComposition!
  1883. String setTraitComposition: {TNativeZeroBasedCollection} asTraitComposition!
  1884. ! !