Kernel-Collections.st 28 KB

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