Kernel-Collections.st 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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 position: self position + 1.
  844. ^collection at: self position
  845. !
  846. next: anInteger
  847. | tempCollection |
  848. tempCollection := self collection class new.
  849. anInteger timesRepeat: [
  850. self atEnd ifFalse: [
  851. tempCollection add: self next]].
  852. ^tempCollection
  853. !
  854. peek
  855. ^self atEnd ifFalse: [
  856. self collection at: self position + 1]
  857. ! !
  858. !Stream methodsFor: 'testing'!
  859. atEnd
  860. ^self position = self size
  861. !
  862. atStart
  863. ^self position = 0
  864. !
  865. isEmpty
  866. ^self size = 0
  867. ! !
  868. !Stream methodsFor: 'writing'!
  869. nextPut: anObject
  870. self position: self position + 1.
  871. self collection at: self position put: anObject.
  872. self setStreamSize: (self streamSize max: self position)
  873. !
  874. nextPutAll: aCollection
  875. aCollection do: [:each |
  876. self nextPut: each]
  877. ! !
  878. !Stream class methodsFor: 'instance creation'!
  879. on: aCollection
  880. ^self new
  881. setCollection: aCollection;
  882. setStreamSize: aCollection size;
  883. yourself
  884. ! !
  885. Stream subclass: #StringStream
  886. instanceVariableNames: ''
  887. category: 'Kernel-Collections'!
  888. !StringStream methodsFor: 'reading'!
  889. next: anInteger
  890. | tempCollection |
  891. tempCollection := self collection class new.
  892. anInteger timesRepeat: [
  893. self atEnd ifFalse: [
  894. tempCollection := tempCollection, self next]].
  895. ^tempCollection
  896. ! !
  897. !StringStream methodsFor: 'writing'!
  898. nextPut: aString
  899. self nextPutAll: aString
  900. !
  901. nextPutAll: aString
  902. self setCollection:
  903. (self collection copyFrom: 1 to: self position),
  904. aString,
  905. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  906. self position: self position + aString size.
  907. self setStreamSize: (self streamSize max: self position)
  908. !
  909. cr
  910. ^self nextPutAll: String cr
  911. !
  912. crlf
  913. ^self nextPutAll: String crlf
  914. !
  915. lf
  916. ^self nextPutAll: String lf
  917. !
  918. space
  919. self nextPut: ' '
  920. ! !
  921. Collection subclass: #Set
  922. instanceVariableNames: 'elements'
  923. category: 'Kernel-Collections'!
  924. !Set methodsFor: 'accessing'!
  925. size
  926. ^elements size
  927. ! !
  928. !Set methodsFor: 'adding/removing'!
  929. add: anObject
  930. <
  931. var found;
  932. for(var i in self['@elements']) {
  933. if(anObject == self['@elements'][i]) {
  934. found = true;
  935. break;
  936. }
  937. }
  938. if(!!found) {self['@elements'].push(anObject)}
  939. >
  940. !
  941. remove: anObject
  942. elements remove: anObject
  943. ! !
  944. !Set methodsFor: 'comparing'!
  945. = aCollection
  946. ^self class = aCollection class and: [
  947. elements = aCollection asArray]
  948. ! !
  949. !Set methodsFor: 'converting'!
  950. asArray
  951. ^elements copy
  952. ! !
  953. !Set methodsFor: 'enumerating'!
  954. detect: aBlock ifNone: anotherBlock
  955. ^elements detect: aBlock ifNone: anotherBlock
  956. !
  957. do: aBlock
  958. elements do: aBlock
  959. !
  960. select: aBlock
  961. | collection |
  962. collection := self class new.
  963. self do: [:each |
  964. (aBlock value: each) ifTrue: [
  965. collection add: each]].
  966. ^collection
  967. ! !
  968. !Set methodsFor: 'initialization'!
  969. initialize
  970. super initialize.
  971. elements := #()
  972. ! !
  973. !Set methodsFor: 'testing'!
  974. includes: anObject
  975. ^elements includes: anObject
  976. ! !
  977. Collection subclass: #HashedCollection
  978. instanceVariableNames: ''
  979. category: 'Kernel-Collections'!
  980. !HashedCollection commentStamp!
  981. A HashedCollection is a traditional JavaScript object, or a Smalltalk Dictionary.
  982. Unlike a Dictionary, it can only have strings as keys.!
  983. !HashedCollection methodsFor: 'accessing'!
  984. size
  985. ^self keys size
  986. !
  987. associations
  988. | associations |
  989. associations := #().
  990. self keys do: [:each |
  991. associations add: (Association key: each value: (self at: each))].
  992. ^associations
  993. !
  994. keys
  995. <
  996. var keys = [];
  997. for(var i in self) {
  998. if(self.hasOwnProperty(i)) {
  999. keys.push(i);
  1000. }
  1001. };
  1002. return keys;
  1003. >
  1004. !
  1005. values
  1006. ^self keys collect: [:each | self at: each]
  1007. !
  1008. at: aKey put: aValue
  1009. ^self basicAt: aKey put: aValue
  1010. !
  1011. at: aKey ifAbsent: aBlock
  1012. ^(self includesKey: aKey)
  1013. ifTrue: [self basicAt: aKey]
  1014. ifFalse: aBlock
  1015. !
  1016. at: aKey ifAbsentPut: aBlock
  1017. ^self at: aKey ifAbsent: [
  1018. self at: aKey put: aBlock value]
  1019. !
  1020. at: aKey ifPresent: aBlock
  1021. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1022. !
  1023. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1024. ^(self basicAt: aKey)
  1025. ifNil: anotherBlock
  1026. ifNotNil: [aBlock value: (self at: aKey)]
  1027. !
  1028. at: aKey
  1029. ^self at: aKey ifAbsent: [self errorNotFound]
  1030. ! !
  1031. !HashedCollection methodsFor: 'adding/removing'!
  1032. add: anAssociation
  1033. self at: anAssociation key put: anAssociation value
  1034. !
  1035. addAll: aHashedCollection
  1036. super addAll: aHashedCollection associations.
  1037. ^aHashedCollection
  1038. !
  1039. removeKey: aKey
  1040. self remove: aKey
  1041. !
  1042. remove: aKey ifAbsent: aBlock
  1043. ^self removeKey: aKey ifAbsent: aBlock
  1044. !
  1045. removeKey: aKey ifAbsent: aBlock
  1046. ^(self includesKey: aKey)
  1047. ifFalse: [aBlock value]
  1048. ifTrue: [self basicDelete: aKey]
  1049. ! !
  1050. !HashedCollection methodsFor: 'comparing'!
  1051. = aHashedCollection
  1052. self class = aHashedCollection class ifFalse: [^false].
  1053. self size = aHashedCollection size ifFalse: [^false].
  1054. ^self associations = aHashedCollection associations
  1055. ! !
  1056. !HashedCollection methodsFor: 'converting'!
  1057. asDictionary
  1058. ^Dictionary fromPairs: self associations
  1059. ! !
  1060. !HashedCollection methodsFor: 'copying'!
  1061. shallowCopy
  1062. | copy |
  1063. copy := self class new.
  1064. self associationsDo: [:each |
  1065. copy at: each key put: each value].
  1066. ^copy
  1067. !
  1068. , aCollection
  1069. self shouldNotImplement
  1070. !
  1071. copyFrom: anIndex to: anotherIndex
  1072. self shouldNotImplement
  1073. !
  1074. deepCopy
  1075. | copy |
  1076. copy := self class new.
  1077. self associationsDo: [:each |
  1078. copy at: each key put: each value deepCopy].
  1079. ^copy
  1080. ! !
  1081. !HashedCollection methodsFor: 'enumerating'!
  1082. associationsDo: aBlock
  1083. self associations do: aBlock
  1084. !
  1085. keysAndValuesDo: aBlock
  1086. self associationsDo: [:each |
  1087. aBlock value: each key value: each value]
  1088. !
  1089. do: aBlock
  1090. self values do: aBlock
  1091. !
  1092. select: aBlock
  1093. | newDict |
  1094. newDict := self class new.
  1095. self keysAndValuesDo: [:key :value |
  1096. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1097. ^newDict
  1098. !
  1099. collect: aBlock
  1100. | newDict |
  1101. newDict := self class new.
  1102. self keysAndValuesDo: [:key :value |
  1103. newDict at: key put: (aBlock value: value)].
  1104. ^newDict
  1105. !
  1106. detect: aBlock ifNone: anotherBlock
  1107. ^self values detect: aBlock ifNone: anotherBlock
  1108. !
  1109. includes: anObject
  1110. ^self values includes: anObject
  1111. ! !
  1112. !HashedCollection methodsFor: 'printing'!
  1113. printString
  1114. ^String streamContents: [:aStream|
  1115. aStream
  1116. nextPutAll: super printString;
  1117. nextPutAll: '('.
  1118. self associations
  1119. do: [:anAssociation|
  1120. aStream
  1121. nextPutAll: anAssociation key printString;
  1122. nextPutAll: ' -> ';
  1123. nextPutAll: anAssociation value printString]
  1124. separatedBy: [aStream nextPutAll: ' , '].
  1125. aStream nextPutAll: ')']
  1126. !
  1127. storeOn: aStream
  1128. aStream nextPutAll: '#{'.
  1129. self associations
  1130. do: [:each | each storeOn: aStream]
  1131. separatedBy: [ aStream nextPutAll: '. '].
  1132. aStream nextPutAll: '}'
  1133. ! !
  1134. !HashedCollection methodsFor: 'testing'!
  1135. includesKey: aKey
  1136. <return self.hasOwnProperty(aKey)>
  1137. ! !
  1138. !HashedCollection class methodsFor: 'instance creation'!
  1139. fromPairs: aCollection
  1140. | dict |
  1141. dict := self new.
  1142. aCollection do: [:each | dict add: each].
  1143. ^dict
  1144. ! !
  1145. HashedCollection subclass: #Dictionary
  1146. instanceVariableNames: 'keys values'
  1147. category: 'Kernel-Collections'!
  1148. !Dictionary methodsFor: 'accessing'!
  1149. at: aKey ifAbsent: aBlock
  1150. <
  1151. var index;
  1152. for(var i=0;i<self['@keys'].length;i++){
  1153. if(self['@keys'][i].__eq(aKey)) {index = i;}
  1154. };
  1155. if(typeof index === 'undefined') {
  1156. return aBlock();
  1157. } else {
  1158. return self['@values'][index];
  1159. }
  1160. >
  1161. !
  1162. keys
  1163. ^keys copy
  1164. !
  1165. values
  1166. ^values copy
  1167. !
  1168. at: aKey put: aValue
  1169. <
  1170. var index = self['@keys'].indexOf(aKey);
  1171. if(index === -1) {
  1172. self['@values'].push(aValue);
  1173. self['@keys'].push(aKey);
  1174. } else {
  1175. self['@values'][index] = aValue;
  1176. };
  1177. return aValue;
  1178. >
  1179. ! !
  1180. !Dictionary methodsFor: 'adding/removing'!
  1181. removeKey: aKey ifAbsent: aBlock
  1182. <
  1183. var index = self['@keys'].indexOf(aKey);
  1184. if(index === -1) {
  1185. return aBlock()
  1186. } else {
  1187. self['@keys'].splice(i, 1);
  1188. self['@values'].splice(i, 1);
  1189. return aKey
  1190. };
  1191. >
  1192. ! !
  1193. !Dictionary methodsFor: 'converting'!
  1194. asHashedCollection
  1195. ^HashedCollection fromPairs: self associations
  1196. !
  1197. asJSONString
  1198. ^self asHashedCollection asJSONString
  1199. ! !
  1200. !Dictionary methodsFor: 'initialization'!
  1201. initialize
  1202. super initialize.
  1203. keys := #().
  1204. values := #()
  1205. ! !
  1206. !Dictionary methodsFor: 'testing'!
  1207. includesKey: aKey
  1208. ^keys includes: aKey
  1209. ! !
  1210. SequenceableCollection subclass: #OrderedCollection
  1211. instanceVariableNames: 'elements'
  1212. category: 'Kernel-Collections'!
  1213. !OrderedCollection methodsFor: 'accessing'!
  1214. size
  1215. ^elements size
  1216. !
  1217. at: anIndex put: anObject
  1218. <return self['@elements'][anIndex - 1] = anObject>
  1219. !
  1220. at: anIndex ifAbsent: aBlock
  1221. ^elements at: anIndex ifAbsent: aBlock
  1222. ! !
  1223. !OrderedCollection methodsFor: 'adding/removing'!
  1224. add: anObject
  1225. <self['@elements'].push(anObject); return anObject;>
  1226. !
  1227. remove: anObject
  1228. <
  1229. for(var i=0;i<self['@elements'].length;i++) {
  1230. if(self['@elements'][i] == anObject) {
  1231. self['@elements'].splice(i,1);
  1232. break;
  1233. }
  1234. }
  1235. >
  1236. !
  1237. removeFrom: aNumber to: anotherNumber
  1238. <self['@elements'].splice(aNumber - 1,anotherNumber - 1)>
  1239. ! !
  1240. !OrderedCollection methodsFor: 'converting'!
  1241. reversed
  1242. ^self asArray reversed asOrderedCollection
  1243. !
  1244. asOrderedCollection
  1245. ^self
  1246. !
  1247. asArray
  1248. ^elements copy
  1249. ! !
  1250. !OrderedCollection methodsFor: 'enumerating'!
  1251. join: aString
  1252. ^elements join: aString
  1253. !
  1254. sort
  1255. elements sort.
  1256. ^self
  1257. !
  1258. sort: aBlock
  1259. elements sort: aBlock.
  1260. ^self
  1261. !
  1262. sorted
  1263. ^self copy sort
  1264. !
  1265. sorted: aBlock
  1266. ^self copy sort: aBlock
  1267. !
  1268. withIndexDo: aBlock
  1269. elements withIndexDo: aBlock
  1270. !
  1271. detect: aBlock ifNone: anotherBlock
  1272. ^elements detect: aBlock ifNone: anotherBlock
  1273. !
  1274. do: aBlock
  1275. elements do: aBlock
  1276. ! !
  1277. !OrderedCollection methodsFor: 'initialization'!
  1278. initialize
  1279. super initialize.
  1280. elements := #()
  1281. ! !