1
0

Kernel-Collections.st 30 KB

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