2
0

Kernel-Collections.st 35 KB

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