Kernel-Collections.st 27 KB

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