Kernel-Collections.st 29 KB

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