1
0

Kernel-Collections.st 28 KB

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