Kernel-Collections.st 39 KB

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