Kernel-Collections.st 28 KB

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