Kernel-Collections.st 28 KB

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