Kernel-Collections.st 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. | newCollection |
  113. newCollection := self class new.
  114. self do: [:each |
  115. newCollection add: (aBlock value: each)].
  116. ^newCollection
  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 |
  679. instance := self new: aCollection size.
  680. aCollection withIndexDo: [:each :index |
  681. instance at: index put: each].
  682. ^instance
  683. ! !
  684. SequenceableCollection subclass: #CharacterArray
  685. instanceVariableNames: ''
  686. package: 'Kernel-Collections'!
  687. !CharacterArray methodsFor: 'accessing'!
  688. at: anIndex put: anObject
  689. self errorReadOnly
  690. ! !
  691. !CharacterArray methodsFor: 'adding'!
  692. add: anObject
  693. self errorReadOnly
  694. !
  695. remove: anObject
  696. self errorReadOnly
  697. ! !
  698. !CharacterArray methodsFor: 'converting'!
  699. asLowercase
  700. ^self class fromString: self asString asLowercase
  701. !
  702. asNumber
  703. ^self asString asNumber
  704. !
  705. asString
  706. ^self subclassResponsibility
  707. !
  708. asSymbol
  709. ^self subclassResponsibility
  710. !
  711. asUppercase
  712. ^self class fromString: self asString asUppercase
  713. ! !
  714. !CharacterArray methodsFor: 'copying'!
  715. , aString
  716. ^self asString, aString asString
  717. ! !
  718. !CharacterArray methodsFor: 'error handling'!
  719. errorReadOnly
  720. self error: 'Object is read-only'
  721. ! !
  722. !CharacterArray methodsFor: 'printing'!
  723. printString
  724. ^self asString printString
  725. ! !
  726. !CharacterArray class methodsFor: 'instance creation'!
  727. fromString: aString
  728. self subclassResponsibility
  729. ! !
  730. CharacterArray subclass: #String
  731. instanceVariableNames: ''
  732. package: 'Kernel-Collections'!
  733. !String methodsFor: 'accessing'!
  734. asciiValue
  735. <return self.charCodeAt(0);>
  736. !
  737. at: anIndex ifAbsent: aBlock
  738. <return String(self)[anIndex - 1] || aBlock()>
  739. !
  740. escaped
  741. <return escape(self)>
  742. !
  743. size
  744. <return self.length>
  745. !
  746. unescaped
  747. <return unescape(self)>
  748. ! !
  749. !String methodsFor: 'comparing'!
  750. < aString
  751. <return String(self) < aString._asString()>
  752. !
  753. <= aString
  754. <return String(self) <= aString._asString()>
  755. !
  756. = aString
  757. aString class = self class ifFalse: [^false].
  758. <return String(self) === String(aString)>
  759. !
  760. == aString
  761. ^self = aString
  762. !
  763. > aString
  764. <return String(self) >> aString._asString()>
  765. !
  766. >= aString
  767. <return String(self) >>= aString._asString()>
  768. ! !
  769. !String methodsFor: 'converting'!
  770. asJSON
  771. ^self
  772. !
  773. asJavaScriptSelector
  774. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  775. !
  776. asJavascript
  777. <
  778. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  779. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  780. else
  781. return "\"" + self + "\"";
  782. >
  783. !
  784. asLowercase
  785. <return self.toLowerCase()>
  786. !
  787. asNumber
  788. <return Number(self)>
  789. !
  790. asSelector
  791. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  792. | selector |
  793. selector := '_', self.
  794. selector := selector replace: ':' with: '_'.
  795. selector := selector replace: '[+]' with: '_plus'.
  796. selector := selector replace: '-' with: '_minus'.
  797. selector := selector replace: '[*]' with: '_star'.
  798. selector := selector replace: '[/]' with: '_slash'.
  799. selector := selector replace: '>' with: '_gt'.
  800. selector := selector replace: '<' with: '_lt'.
  801. selector := selector replace: '=' with: '_eq'.
  802. selector := selector replace: ',' with: '_comma'.
  803. selector := selector replace: '[@]' with: '_at'.
  804. ^selector
  805. !
  806. asString
  807. ^self
  808. !
  809. asSymbol
  810. ^Symbol lookup: self
  811. !
  812. asUppercase
  813. <return self.toUpperCase()>
  814. !
  815. reversed
  816. <return self.split("").reverse().join("")>
  817. !
  818. tokenize: aString
  819. <return self.split(aString)>
  820. ! !
  821. !String methodsFor: 'copying'!
  822. , aString
  823. <return self + aString>
  824. !
  825. copyFrom: anIndex to: anotherIndex
  826. <return self.substring(anIndex - 1, anotherIndex)>
  827. !
  828. deepCopy
  829. ^self shallowCopy
  830. !
  831. shallowCopy
  832. ^self class fromString: self
  833. ! !
  834. !String methodsFor: 'printing'!
  835. printNl
  836. <console.log(self)>
  837. !
  838. printString
  839. ^'''', self, ''''
  840. ! !
  841. !String methodsFor: 'regular expressions'!
  842. match: aRegexp
  843. <return self.search(aRegexp) !!= -1>
  844. !
  845. matchesOf: aRegularExpression
  846. <return self.match(aRegularExpression)>
  847. !
  848. replace: aString with: anotherString
  849. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  850. !
  851. replaceRegexp: aRegexp with: aString
  852. <return self.replace(aRegexp, aString)>
  853. !
  854. trimBoth
  855. ^self trimBoth: '\s'
  856. !
  857. trimBoth: separators
  858. ^(self trimLeft: separators) trimRight: separators
  859. !
  860. trimLeft
  861. ^self trimLeft: '\s'
  862. !
  863. trimLeft: separators
  864. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  865. !
  866. trimRight
  867. ^self trimRight: '\s'
  868. !
  869. trimRight: separators
  870. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  871. ! !
  872. !String methodsFor: 'split join'!
  873. join: aCollection
  874. ^ String
  875. streamContents: [:stream | aCollection
  876. do: [:each | stream nextPutAll: each asString]
  877. separatedBy: [stream nextPutAll: self]]
  878. !
  879. lineIndicesDo: aBlock
  880. "execute aBlock with 3 arguments for each line:
  881. - start index of line
  882. - end index of line without line delimiter
  883. - end index of line including line delimiter(s) CR, LF or CRLF"
  884. | cr lf start sz nextLF nextCR |
  885. start := 1.
  886. sz := self size.
  887. cr := String cr.
  888. nextCR := self indexOf: cr startingAt: 1.
  889. lf := String lf.
  890. nextLF := self indexOf: lf startingAt: 1.
  891. [ start <= sz ] whileTrue: [
  892. (nextLF = 0 and: [ nextCR = 0 ])
  893. ifTrue: [ "No more CR, nor LF, the string is over"
  894. aBlock value: start value: sz value: sz.
  895. ^self ].
  896. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  897. ifTrue: [ "Found a LF"
  898. aBlock value: start value: nextLF - 1 value: nextLF.
  899. start := 1 + nextLF.
  900. nextLF := self indexOf: lf startingAt: start ]
  901. ifFalse: [ 1 + nextCR = nextLF
  902. ifTrue: [ "Found a CR-LF pair"
  903. aBlock value: start value: nextCR - 1 value: nextLF.
  904. start := 1 + nextLF.
  905. nextCR := self indexOf: cr startingAt: start.
  906. nextLF := self indexOf: lf startingAt: start ]
  907. ifFalse: [ "Found a CR"
  908. aBlock value: start value: nextCR - 1 value: nextCR.
  909. start := 1 + nextCR.
  910. nextCR := self indexOf: cr startingAt: start ]]]
  911. !
  912. lineNumber: anIndex
  913. "Answer a string containing the characters in the given line number."
  914. | lineCount |
  915. lineCount := 0.
  916. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  917. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  918. ^nil
  919. !
  920. lines
  921. "Answer an array of lines composing this receiver without the line ending delimiters."
  922. | lines |
  923. lines := Array new.
  924. self linesDo: [:aLine | lines add: aLine].
  925. ^lines
  926. !
  927. linesDo: aBlock
  928. "Execute aBlock with each line in this string. The terminating line
  929. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  930. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  931. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  932. ! !
  933. !String methodsFor: 'testing'!
  934. includesSubString: subString
  935. < return self.indexOf(subString) !!= -1 >
  936. !
  937. isString
  938. ^true
  939. ! !
  940. !String class methodsFor: 'accessing'!
  941. cr
  942. <return '\r'>
  943. !
  944. crlf
  945. <return '\r\n'>
  946. !
  947. lf
  948. <return '\n'>
  949. !
  950. space
  951. <return ' '>
  952. !
  953. streamClass
  954. ^StringStream
  955. !
  956. tab
  957. <return '\t'>
  958. ! !
  959. !String class methodsFor: 'instance creation'!
  960. fromString: aString
  961. <return new self.fn(aString)>
  962. !
  963. streamContents: blockWithArg
  964. |stream|
  965. stream := (self streamClass on: String new).
  966. blockWithArg value: stream.
  967. ^ stream contents
  968. !
  969. value: aUTFCharCode
  970. <return String.fromCharCode(aUTFCharCode);>
  971. ! !
  972. CharacterArray subclass: #Symbol
  973. instanceVariableNames: ''
  974. package: 'Kernel-Collections'!
  975. !Symbol methodsFor: 'accessing'!
  976. at: anIndex ifAbsent: aBlock
  977. ^self asString at: anIndex ifAbsent: aBlock
  978. !
  979. size
  980. ^self asString size
  981. ! !
  982. !Symbol methodsFor: 'comparing'!
  983. < aSymbol
  984. ^self asString < aSymbol asString
  985. !
  986. <= aSymbol
  987. ^self asString <= aSymbol asString
  988. !
  989. = aSymbol
  990. aSymbol class = self class ifFalse: [^false].
  991. ^self asString = aSymbol asString
  992. !
  993. > aSymbol
  994. ^self asString > aSymbol asString
  995. !
  996. >= aSymbol
  997. ^self asString >= aSymbol asString
  998. ! !
  999. !Symbol methodsFor: 'converting'!
  1000. asJSON
  1001. ^self asString asJSON
  1002. !
  1003. asJavascript
  1004. ^'smalltalk.symbolFor("', self asString, '")'
  1005. !
  1006. asSelector
  1007. ^self asString asSelector
  1008. !
  1009. asString
  1010. <return self.value>
  1011. !
  1012. asSymbol
  1013. ^self
  1014. ! !
  1015. !Symbol methodsFor: 'copying'!
  1016. copyFrom: anIndex to: anotherIndex
  1017. ^self class fromString: (self asString copyFrom: anIndex to: anotherIndex)
  1018. !
  1019. deepCopy
  1020. ^self
  1021. !
  1022. shallowCopy
  1023. ^self
  1024. ! !
  1025. !Symbol methodsFor: 'printing'!
  1026. isSymbol
  1027. ^true
  1028. !
  1029. printString
  1030. ^'#', self asString
  1031. ! !
  1032. !Symbol class methodsFor: 'instance creation'!
  1033. basicNew
  1034. self shouldNotImplement
  1035. !
  1036. fromString: aString
  1037. ^self lookup: aString
  1038. !
  1039. lookup: aString
  1040. <return smalltalk.symbolFor(aString);>
  1041. ! !
  1042. Collection subclass: #Set
  1043. instanceVariableNames: 'elements'
  1044. package: 'Kernel-Collections'!
  1045. !Set methodsFor: 'accessing'!
  1046. size
  1047. ^elements size
  1048. ! !
  1049. !Set methodsFor: 'adding/removing'!
  1050. add: anObject
  1051. <
  1052. var found;
  1053. for(var i=0; i < self['@elements'].length; i++) {
  1054. if(anObject == self['@elements'][i]) {
  1055. found = true;
  1056. break;
  1057. }
  1058. }
  1059. if(!!found) {self['@elements'].push(anObject)}
  1060. >
  1061. !
  1062. remove: anObject
  1063. elements remove: anObject
  1064. ! !
  1065. !Set methodsFor: 'comparing'!
  1066. = aCollection
  1067. ^self class = aCollection class and: [
  1068. elements = aCollection asArray]
  1069. ! !
  1070. !Set methodsFor: 'converting'!
  1071. asArray
  1072. ^elements copy
  1073. ! !
  1074. !Set methodsFor: 'enumerating'!
  1075. detect: aBlock ifNone: anotherBlock
  1076. ^elements detect: aBlock ifNone: anotherBlock
  1077. !
  1078. do: aBlock
  1079. elements do: aBlock
  1080. !
  1081. select: aBlock
  1082. | collection |
  1083. collection := self class new.
  1084. self do: [:each |
  1085. (aBlock value: each) ifTrue: [
  1086. collection add: each]].
  1087. ^collection
  1088. ! !
  1089. !Set methodsFor: 'initialization'!
  1090. initialize
  1091. super initialize.
  1092. elements := #()
  1093. ! !
  1094. !Set methodsFor: 'printing'!
  1095. printString
  1096. "print the contents of the Set into a string and return it"
  1097. ^String streamContents: [:aStream |
  1098. aStream
  1099. nextPutAll: super printString, ' ('.
  1100. self do: [:each | aStream nextPutAll: each printString]
  1101. separatedBy: [aStream nextPutAll: ' '].
  1102. aStream nextPutAll: ')'.]
  1103. ! !
  1104. !Set methodsFor: 'testing'!
  1105. includes: anObject
  1106. ^elements includes: anObject
  1107. ! !
  1108. Object subclass: #RegularExpression
  1109. instanceVariableNames: ''
  1110. package: 'Kernel-Collections'!
  1111. !RegularExpression methodsFor: 'evaluating'!
  1112. compile: aString
  1113. <return self.compile(aString)>
  1114. !
  1115. exec: aString
  1116. <return self.exec(aString) || nil>
  1117. !
  1118. test: aString
  1119. <return self.test(aString)>
  1120. ! !
  1121. !RegularExpression class methodsFor: 'instance creation'!
  1122. fromString: aString
  1123. ^self fromString: aString flag: ''
  1124. !
  1125. fromString: aString flag: anotherString
  1126. <return new RegExp(aString, anotherString)>
  1127. ! !
  1128. Object subclass: #Stream
  1129. instanceVariableNames: 'collection position streamSize'
  1130. package: 'Kernel-Collections'!
  1131. !Stream methodsFor: 'accessing'!
  1132. collection
  1133. ^collection
  1134. !
  1135. contents
  1136. ^self collection
  1137. copyFrom: 1
  1138. to: self streamSize
  1139. !
  1140. position
  1141. ^position ifNil: [position := 0]
  1142. !
  1143. position: anInteger
  1144. position := anInteger
  1145. !
  1146. setCollection: aCollection
  1147. collection := aCollection
  1148. !
  1149. setStreamSize: anInteger
  1150. streamSize := anInteger
  1151. !
  1152. size
  1153. ^self streamSize
  1154. !
  1155. streamSize
  1156. ^streamSize
  1157. ! !
  1158. !Stream methodsFor: 'actions'!
  1159. close
  1160. !
  1161. flush
  1162. !
  1163. reset
  1164. self position: 0
  1165. !
  1166. resetContents
  1167. self reset.
  1168. self setStreamSize: 0
  1169. ! !
  1170. !Stream methodsFor: 'enumerating'!
  1171. do: aBlock
  1172. [self atEnd] whileFalse: [aBlock value: self next]
  1173. ! !
  1174. !Stream methodsFor: 'positioning'!
  1175. setToEnd
  1176. self position: self size
  1177. !
  1178. skip: anInteger
  1179. self position: ((self position + anInteger) min: self size max: 0)
  1180. ! !
  1181. !Stream methodsFor: 'reading'!
  1182. next
  1183. ^self atEnd
  1184. ifTrue: [nil]
  1185. ifFalse: [
  1186. self position: self position + 1.
  1187. collection at: self position]
  1188. !
  1189. next: anInteger
  1190. | tempCollection |
  1191. tempCollection := self collection class new.
  1192. anInteger timesRepeat: [
  1193. self atEnd ifFalse: [
  1194. tempCollection add: self next]].
  1195. ^tempCollection
  1196. !
  1197. peek
  1198. ^self atEnd ifFalse: [
  1199. self collection at: self position + 1]
  1200. ! !
  1201. !Stream methodsFor: 'testing'!
  1202. atEnd
  1203. ^self position = self size
  1204. !
  1205. atStart
  1206. ^self position = 0
  1207. !
  1208. isEmpty
  1209. ^self size = 0
  1210. ! !
  1211. !Stream methodsFor: 'writing'!
  1212. nextPut: anObject
  1213. self position: self position + 1.
  1214. self collection at: self position put: anObject.
  1215. self setStreamSize: (self streamSize max: self position)
  1216. !
  1217. nextPutAll: aCollection
  1218. aCollection do: [:each |
  1219. self nextPut: each]
  1220. ! !
  1221. !Stream class methodsFor: 'instance creation'!
  1222. on: aCollection
  1223. ^self new
  1224. setCollection: aCollection;
  1225. setStreamSize: aCollection size;
  1226. yourself
  1227. ! !
  1228. Stream subclass: #StringStream
  1229. instanceVariableNames: ''
  1230. package: 'Kernel-Collections'!
  1231. !StringStream methodsFor: 'reading'!
  1232. next: anInteger
  1233. | tempCollection |
  1234. tempCollection := self collection class new.
  1235. anInteger timesRepeat: [
  1236. self atEnd ifFalse: [
  1237. tempCollection := tempCollection, self next]].
  1238. ^tempCollection
  1239. ! !
  1240. !StringStream methodsFor: 'writing'!
  1241. cr
  1242. ^self nextPutAll: String cr
  1243. !
  1244. crlf
  1245. ^self nextPutAll: String crlf
  1246. !
  1247. lf
  1248. ^self nextPutAll: String lf
  1249. !
  1250. nextPut: aString
  1251. self nextPutAll: aString
  1252. !
  1253. nextPutAll: aString
  1254. self setCollection:
  1255. (self collection copyFrom: 1 to: self position),
  1256. aString,
  1257. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1258. self position: self position + aString size.
  1259. self setStreamSize: (self streamSize max: self position)
  1260. !
  1261. space
  1262. self nextPut: ' '
  1263. ! !