Kernel-Collections.st 27 KB

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