Kernel-Collections.st 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097
  1. Smalltalk current createPackage: 'Kernel-Collections'!
  2. Object subclass: #Association
  3. instanceVariableNames: 'key value'
  4. package: 'Kernel-Collections'!
  5. !Association commentStamp!
  6. I represent a pair of associated objects, a key and a value. My instances can serve as entries in a dictionary.
  7. Instances can be created with the class-side method `#key:value:`!
  8. !Association methodsFor: 'accessing'!
  9. key
  10. ^ key
  11. !
  12. key: aKey
  13. key := aKey
  14. !
  15. value
  16. ^ value
  17. !
  18. value: aValue
  19. value := aValue
  20. ! !
  21. !Association methodsFor: 'comparing'!
  22. = anAssociation
  23. ^ self class = anAssociation class and: [
  24. self key = anAssociation key and: [
  25. self value = anAssociation value ]]
  26. ! !
  27. !Association methodsFor: 'printing'!
  28. printOn: aStream
  29. self key printOn: aStream.
  30. aStream nextPutAll: ' -> '.
  31. self value printOn: aStream
  32. ! !
  33. !Association class methodsFor: 'instance creation'!
  34. key: aKey value: aValue
  35. ^ self new
  36. key: aKey;
  37. value: aValue;
  38. yourself
  39. ! !
  40. Object subclass: #Collection
  41. instanceVariableNames: ''
  42. package: 'Kernel-Collections'!
  43. !Collection commentStamp!
  44. I am the abstract superclass of all classes that represent a group of elements.
  45. I provide a set of useful methods to the Collection hierarchy such as enumerating and converting methods.!
  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. size
  55. self subclassResponsibility
  56. ! !
  57. !Collection methodsFor: 'adding/removing'!
  58. add: anObject
  59. self subclassResponsibility
  60. !
  61. addAll: aCollection
  62. aCollection do: [ :each |
  63. self add: each ].
  64. ^ aCollection
  65. !
  66. anyOne
  67. "Answer a representative sample of the receiver. This method can
  68. be helpful when needing to preinfer the nature of the contents of
  69. semi-homogeneous collections."
  70. self ifEmpty: [ self error: 'Collection is empty' ].
  71. self do: [ :each | ^ each ]
  72. !
  73. remove: anObject
  74. ^ self remove: anObject ifAbsent: [ self errorNotFound ]
  75. !
  76. remove: anObject ifAbsent: aBlock
  77. self subclassResponsibility
  78. !
  79. removeAll
  80. self subclassResponsibility
  81. ! !
  82. !Collection methodsFor: 'converting'!
  83. asArray
  84. ^ Array withAll: self
  85. !
  86. asJSON
  87. ^ self asArray collect: [ :each | each asJSON ]
  88. !
  89. asOrderedCollection
  90. ^ self asArray
  91. !
  92. asSet
  93. ^ Set withAll: self
  94. ! !
  95. !Collection methodsFor: 'copying'!
  96. , aCollection
  97. ^ self copy
  98. addAll: aCollection;
  99. yourself
  100. !
  101. copyWith: anObject
  102. ^ self copy add: anObject; yourself
  103. !
  104. copyWithAll: aCollection
  105. ^ self copy addAll: aCollection; yourself
  106. !
  107. copyWithoutAll: aCollection
  108. "Answer a copy of the receiver that does not contain any elements
  109. equal to those in aCollection."
  110. ^ self reject: [ :each | aCollection includes: each ]
  111. ! !
  112. !Collection methodsFor: 'enumerating'!
  113. allSatisfy: aBlock
  114. "Evaluate aBlock with the elements of the receiver.
  115. If aBlock returns false for any element return false.
  116. Otherwise return true."
  117. self do: [ :each | (aBlock value: each) ifFalse: [ ^ false ] ].
  118. ^ true
  119. !
  120. anySatisfy: aBlock
  121. "Evaluate aBlock with the elements of the receiver.
  122. If aBlock returns true for any element return true.
  123. Otherwise return false."
  124. self do: [ :each | (aBlock value: each) ifTrue: [ ^ true ] ].
  125. ^ false
  126. !
  127. collect: aBlock
  128. | stream |
  129. stream := self class new writeStream.
  130. self do: [ :each |
  131. stream nextPut: (aBlock value: each) ].
  132. ^ stream contents
  133. !
  134. detect: aBlock
  135. ^ self detect: aBlock ifNone: [ self errorNotFound ]
  136. !
  137. detect: aBlock ifNone: anotherBlock
  138. self subclassResponsibility
  139. !
  140. do: aBlock
  141. self subclassResponsibility
  142. !
  143. do: aBlock separatedBy: anotherBlock
  144. | actionBeforeElement |
  145. actionBeforeElement := [ actionBeforeElement := anotherBlock ].
  146. self do: [ :each |
  147. actionBeforeElement value.
  148. aBlock value: each ]
  149. !
  150. inject: anObject into: aBlock
  151. | result |
  152. result := anObject.
  153. self do: [ :each |
  154. result := aBlock value: result value: each ].
  155. ^ result
  156. !
  157. intersection: aCollection
  158. "Answer the set theoretic intersection of two collections."
  159. | set outputSet |
  160. set := self asSet.
  161. outputSet := Set new.
  162. aCollection do: [ :each |
  163. ((set includes: each) and: [ (outputSet includes: each) not ])
  164. ifTrue: [
  165. outputSet add: each ]].
  166. ^ self class withAll: outputSet asArray
  167. !
  168. noneSatisfy: aBlock
  169. "Evaluate aBlock with the elements of the receiver.
  170. If aBlock returns false for all elements return true.
  171. Otherwise return false"
  172. self do: [ :item | (aBlock value: item) ifTrue: [ ^ false ] ].
  173. ^ true
  174. !
  175. reject: aBlock
  176. ^ self select: [ :each | (aBlock value: each) = false ]
  177. !
  178. select: aBlock
  179. | stream |
  180. stream := self class new writeStream.
  181. self do: [ :each |
  182. (aBlock value: each) ifTrue: [
  183. stream nextPut: each ] ].
  184. ^ stream contents
  185. !
  186. select: selectBlock thenCollect: collectBlock
  187. | stream |
  188. stream := self class new writeStream.
  189. self do: [ :each |
  190. (selectBlock value: each) ifTrue: [
  191. stream nextPut: (collectBlock value: each) ] ].
  192. ^ stream contents
  193. ! !
  194. !Collection methodsFor: 'error handling'!
  195. errorNotFound
  196. self error: 'Object is not in the collection'
  197. ! !
  198. !Collection methodsFor: 'streaming'!
  199. putOn: aStream
  200. self do: [ :each | each putOn: aStream ]
  201. ! !
  202. !Collection methodsFor: 'testing'!
  203. contains: aBlock
  204. self deprecatedAPI.
  205. ^ self anySatisfy: aBlock
  206. !
  207. ifEmpty: aBlock
  208. "Evaluate the given block with the receiver as argument, answering its value if the receiver is empty, otherwise answer the receiver.
  209. 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:
  210. self classifyMethodAs:
  211. (myProtocol ifEmpty: ['As yet unclassified'])"
  212. ^ self isEmpty
  213. ifTrue: aBlock
  214. ifFalse: [ self ]
  215. !
  216. ifEmpty: aBlock ifNotEmpty: anotherBlock
  217. ^ self isEmpty
  218. ifTrue: aBlock
  219. ifFalse: anotherBlock
  220. !
  221. ifNotEmpty: aBlock
  222. ^ self notEmpty
  223. ifTrue: aBlock
  224. ifFalse: [ self ]
  225. !
  226. ifNotEmpty: aBlock ifEmpty: anotherBlock
  227. ^ self notEmpty
  228. ifTrue: aBlock
  229. ifFalse: anotherBlock
  230. !
  231. includes: anObject
  232. ^ self anySatisfy: [ :each | each = anObject ]
  233. !
  234. isEmpty
  235. ^ self size = 0
  236. !
  237. notEmpty
  238. ^ self isEmpty not
  239. ! !
  240. !Collection class methodsFor: 'helios'!
  241. heliosClass
  242. ^ 'collection'
  243. ! !
  244. !Collection class methodsFor: 'instance creation'!
  245. new: anInteger
  246. ^ self new
  247. !
  248. with: anObject
  249. ^ self new
  250. add: anObject;
  251. yourself
  252. !
  253. with: anObject with: anotherObject
  254. ^ self new
  255. add: anObject;
  256. add: anotherObject;
  257. yourself
  258. !
  259. with: firstObject with: secondObject with: thirdObject
  260. ^ self new
  261. add: firstObject;
  262. add: secondObject;
  263. add: thirdObject;
  264. yourself
  265. !
  266. withAll: aCollection
  267. ^ self new
  268. addAll: aCollection;
  269. yourself
  270. ! !
  271. Collection subclass: #IndexableCollection
  272. instanceVariableNames: ''
  273. package: 'Kernel-Collections'!
  274. !IndexableCollection commentStamp!
  275. I am a key-value store collection, that is,
  276. I store values under indexes.
  277. As a rule of thumb, if a collection has `#at:` and `#at:put:`,
  278. it is an IndexableCollection.!
  279. !IndexableCollection methodsFor: 'accessing'!
  280. at: anIndex
  281. "Lookup the given index in the receiver.
  282. If it is present, answer the value stored at anIndex.
  283. Otherwise, raise an error."
  284. ^ self at: anIndex ifAbsent: [ self errorNotFound ]
  285. !
  286. at: anIndex ifAbsent: aBlock
  287. "Lookup the given index in the receiver.
  288. If it is present, answer the value stored at anIndex.
  289. Otherwise, answer the value of aBlock."
  290. self subclassResponsibility
  291. !
  292. at: anIndex ifPresent: aBlock
  293. "Lookup the given index in the receiver.
  294. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  295. Otherwise, answer nil."
  296. ^ self at: anIndex ifPresent: aBlock ifAbsent: [ nil ]
  297. !
  298. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  299. "Lookup the given index in the receiver.
  300. If it is present, answer the value of evaluating aBlock with the value stored at anIndex.
  301. Otherwise, answer the value of anotherBlock."
  302. self subclassResponsibility
  303. !
  304. at: anIndex put: anObject
  305. "Store anObject under the given index in the receiver."
  306. self subclassResponsibility
  307. !
  308. indexOf: anObject
  309. "Lookup index at which anObject is stored in the receiver.
  310. If not present, raise an error."
  311. ^ self indexOf: anObject ifAbsent: [ self errorNotFound ]
  312. !
  313. indexOf: anObject ifAbsent: aBlock
  314. "Lookup index at which anObject is stored in the receiver.
  315. If not present, return value of executing aBlock."
  316. self subclassResponsibility
  317. ! !
  318. !IndexableCollection methodsFor: 'enumarating'!
  319. with: anotherCollection do: aBlock
  320. "Calls aBlock with every value from self
  321. and with indetically-indexed value from anotherCollection"
  322. self withIndexDo: [ :each :index |
  323. aBlock value: each value: (anotherCollection at: index) ]
  324. !
  325. withIndexDo: aBlock
  326. "Calls aBlock with every value from self
  327. and with its index as the second argument"
  328. self subclassResponsibility
  329. ! !
  330. IndexableCollection subclass: #HashedCollection
  331. instanceVariableNames: ''
  332. package: 'Kernel-Collections'!
  333. !HashedCollection commentStamp!
  334. I am a traditional JavaScript object, or a Smalltalk `Dictionary`.
  335. Unlike a `Dictionary`, I can only have strings as keys.!
  336. !HashedCollection methodsFor: 'accessing'!
  337. associations
  338. | associations |
  339. associations := #().
  340. self associationsDo: [ :each | associations add: each ].
  341. ^ associations
  342. !
  343. at: aKey ifAbsent: aBlock
  344. ^ (self includesKey: aKey)
  345. ifTrue: [ self basicAt: aKey ]
  346. ifFalse: aBlock
  347. !
  348. at: aKey ifAbsentPut: aBlock
  349. ^ self at: aKey ifAbsent: [
  350. self at: aKey put: aBlock value ]
  351. !
  352. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  353. "Lookup the given key in the receiver.
  354. If it is present, answer the value of evaluating the oneArgBlock with the value associated with the key,
  355. otherwise answer the value of absentBlock."
  356. ^ (self includesKey: aKey)
  357. ifTrue: [ aBlock value: (self at: aKey) ]
  358. ifFalse: anotherBlock
  359. !
  360. at: aKey put: aValue
  361. ^ self basicAt: aKey put: aValue
  362. !
  363. indexOf: anObject ifAbsent: aBlock
  364. ^ self keys detect: [ :each | (self at: each) = anObject ] ifNone: aBlock
  365. !
  366. keyAtValue: anObject
  367. ^ self keyAtValue: anObject ifAbsent: [ self errorNotFound ]
  368. !
  369. keyAtValue: anObject ifAbsent: aBlock
  370. ^ self indexOf: anObject ifAbsent: aBlock
  371. !
  372. keys
  373. <return Object.keys(self)>
  374. !
  375. size
  376. ^ self keys size
  377. !
  378. values
  379. <
  380. return self._keys().map(function(key){
  381. return self._at_(key);
  382. });
  383. >
  384. ! !
  385. !HashedCollection methodsFor: 'adding/removing'!
  386. add: anAssociation
  387. self at: anAssociation key put: anAssociation value
  388. !
  389. addAll: aHashedCollection
  390. super addAll: aHashedCollection associations.
  391. ^ aHashedCollection
  392. !
  393. remove: aKey ifAbsent: aBlock
  394. ^ self removeKey: aKey ifAbsent: aBlock
  395. !
  396. removeAll
  397. ^ self keys do: [ :each | self removeKey: each ]
  398. !
  399. removeKey: aKey
  400. ^ self remove: aKey
  401. !
  402. removeKey: aKey ifAbsent: aBlock
  403. ^ (self includesKey: aKey)
  404. ifFalse: [ aBlock value ]
  405. ifTrue: [ self basicDelete: aKey ]
  406. ! !
  407. !HashedCollection methodsFor: 'comparing'!
  408. = aHashedCollection
  409. self class = aHashedCollection class ifFalse: [ ^ false ].
  410. self size = aHashedCollection size ifFalse: [ ^ false ].
  411. ^ self associations = aHashedCollection associations
  412. ! !
  413. !HashedCollection methodsFor: 'converting'!
  414. asDictionary
  415. ^ Dictionary from: self associations
  416. !
  417. asJSON
  418. | c |
  419. c := self class new.
  420. self keysAndValuesDo: [ :key :value |
  421. c at: key put: value asJSON ].
  422. ^ c
  423. ! !
  424. !HashedCollection methodsFor: 'copying'!
  425. , aCollection
  426. self shouldNotImplement
  427. !
  428. deepCopy
  429. | copy |
  430. copy := self class new.
  431. self keysAndValuesDo: [ :key :value |
  432. copy at: key put: value deepCopy ].
  433. ^ copy
  434. !
  435. shallowCopy
  436. | copy |
  437. copy := self class new.
  438. self keysAndValuesDo: [ :key :value |
  439. copy at: key put: value ].
  440. ^ copy
  441. ! !
  442. !HashedCollection methodsFor: 'enumerating'!
  443. associationsDo: aBlock
  444. self keysAndValuesDo: [ :key :value |
  445. aBlock value: (Association key: key value: value) ]
  446. !
  447. collect: aBlock
  448. | newDict |
  449. newDict := self class new.
  450. self keysAndValuesDo: [ :key :value |
  451. newDict at: key put: (aBlock value: value) ].
  452. ^ newDict
  453. !
  454. detect: aBlock ifNone: anotherBlock
  455. ^ self values detect: aBlock ifNone: anotherBlock
  456. !
  457. do: aBlock
  458. self valuesDo: aBlock
  459. !
  460. includes: anObject
  461. ^ self values includes: anObject
  462. !
  463. keysAndValuesDo: aBlock
  464. self keysDo: [ :each |
  465. aBlock value: each value: (self at: each) ]
  466. !
  467. keysDo: aBlock
  468. self keys do: aBlock
  469. !
  470. select: aBlock
  471. | newDict |
  472. newDict := self class new.
  473. self keysAndValuesDo: [ :key :value |
  474. (aBlock value: value) ifTrue: [ newDict at: key put: value ]].
  475. ^ newDict
  476. !
  477. valuesDo: aBlock
  478. self values do: [ :value | aBlock value: value ]
  479. !
  480. withIndexDo: aBlock
  481. self keysAndValuesDo: [ :key :value | aBlock value: value value: key ]
  482. ! !
  483. !HashedCollection methodsFor: 'printing'!
  484. printOn: aStream
  485. super printOn: aStream.
  486. aStream nextPutAll: ' ('.
  487. self associations
  488. do: [ :each | each printOn: aStream ]
  489. separatedBy: [ aStream nextPutAll: ' , ' ].
  490. aStream nextPutAll: ')'
  491. ! !
  492. !HashedCollection methodsFor: 'testing'!
  493. includesKey: aKey
  494. <return self.hasOwnProperty(aKey)>
  495. ! !
  496. !HashedCollection class methodsFor: 'instance creation'!
  497. from: aCollection
  498. | newCollection |
  499. newCollection := self new.
  500. aCollection do: [ :each | newCollection add: each ].
  501. ^ newCollection
  502. !
  503. fromPairs: aCollection
  504. "This message is poorly named and has been replaced by #from:"
  505. ^ self from: aCollection
  506. !
  507. newFromPairs: aCollection
  508. "Accept an array of elements where every two elements form an
  509. association - the odd element being the key, and the even element the value."
  510. | newCollection |
  511. aCollection size even ifFalse: [
  512. self error: '#newFromPairs only accepts arrays of an even length' ].
  513. newCollection := self new.
  514. ( 1 to: aCollection size by: 2 ) do: [ :each |
  515. newCollection at: (aCollection at: each) put: (aCollection at: each + 1) ].
  516. ^ newCollection
  517. ! !
  518. HashedCollection subclass: #Dictionary
  519. instanceVariableNames: 'keys values'
  520. package: 'Kernel-Collections'!
  521. !Dictionary commentStamp!
  522. I represent a set of elements that can be viewed from one of two perspectives: a set of associations,
  523. or a container of values that are externally named where the name can be any object that responds to `=`.
  524. The external name is referred to as the key.!
  525. !Dictionary methodsFor: 'accessing'!
  526. at: aKey ifAbsent: aBlock
  527. <
  528. var index = self._positionOfKey_(aKey);
  529. return index >>=0 ? self['@values'][index] : aBlock._value();
  530. >
  531. !
  532. at: aKey put: aValue
  533. <
  534. var index = self._positionOfKey_(aKey);
  535. if(index === -1) {
  536. var keys = self['@keys'];
  537. index = keys.length;
  538. keys.push(aKey);
  539. }
  540. return self['@values'][index] = aValue;
  541. >
  542. !
  543. indexOf: anObject ifAbsent: aBlock
  544. | index |
  545. index := values
  546. indexOf: anObject
  547. ifAbsent: [ 0 ].
  548. ^ index = 0
  549. ifTrue: [ aBlock value ]
  550. ifFalse: [ keys at: index ]
  551. !
  552. keys
  553. ^ keys copy
  554. !
  555. values
  556. ^ values
  557. ! !
  558. !Dictionary methodsFor: 'adding/removing'!
  559. removeAll
  560. keys removeAll.
  561. values removeAll
  562. !
  563. removeKey: aKey ifAbsent: aBlock
  564. <
  565. var index = self._positionOfKey_(aKey);
  566. if(index === -1) {
  567. return aBlock._value()
  568. } else {
  569. var keys = self['@keys'], values = self['@values'];
  570. var value = values[index], l = keys.length;
  571. keys[index] = keys[l-1];
  572. keys.pop();
  573. values[index] = values[l-1];
  574. values.pop();
  575. return value;
  576. }
  577. >
  578. ! !
  579. !Dictionary methodsFor: 'converting'!
  580. asHashedCollection
  581. ^ HashedCollection from: self associations
  582. !
  583. asJSON
  584. ^ self asHashedCollection asJSON
  585. ! !
  586. !Dictionary methodsFor: 'enumerating'!
  587. keysAndValuesDo: aBlock
  588. ^ keys with: values do: aBlock
  589. !
  590. keysDo: aBlock
  591. ^ keys do: aBlock
  592. !
  593. valuesDo: aBlock
  594. ^ values do: aBlock
  595. ! !
  596. !Dictionary methodsFor: 'initialization'!
  597. initialize
  598. super initialize.
  599. keys := #().
  600. values := #()
  601. ! !
  602. !Dictionary methodsFor: 'private'!
  603. positionOfKey: anObject
  604. <
  605. var keys = self['@keys'];
  606. for(var i=0;i<keys.length;i++){
  607. if(keys[i].__eq(anObject)) { return i;}
  608. }
  609. return -1;
  610. >
  611. ! !
  612. !Dictionary methodsFor: 'testing'!
  613. includesKey: aKey
  614. < return self._positionOfKey_(aKey) >>= 0; >
  615. ! !
  616. IndexableCollection subclass: #SequenceableCollection
  617. instanceVariableNames: ''
  618. package: 'Kernel-Collections'!
  619. !SequenceableCollection commentStamp!
  620. I am an IndexableCollection
  621. with numeric indexes starting with 1.!
  622. !SequenceableCollection methodsFor: 'accessing'!
  623. allButFirst
  624. ^ self copyFrom: 2 to: self size
  625. !
  626. allButLast
  627. ^ self copyFrom: 1 to: self size - 1
  628. !
  629. atRandom
  630. ^ self at: self size atRandom
  631. !
  632. first
  633. ^ self at: 1
  634. !
  635. first: aNumber
  636. "Answer the first `aNumber` elements of the receiver.
  637. Raise an error if there are not enough elements in the receiver."
  638. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  639. ^ self copyFrom: 1 to: aNumber
  640. !
  641. fourth
  642. ^ self at: 4
  643. !
  644. indexOf: anObject ifAbsent: aBlock
  645. <
  646. self = self._numericallyIndexable();
  647. for(var i=0; i < self.length; i++) {
  648. if(self[i].__eq(anObject)) {return i+1}
  649. };
  650. return aBlock._value();
  651. >
  652. !
  653. indexOf: anObject startingAt: start
  654. "Answer the index of the first occurence of anElement after start
  655. within the receiver. If the receiver does not contain anElement,
  656. answer 0."
  657. ^ self indexOf: anObject startingAt: start ifAbsent: [ 0 ]
  658. !
  659. indexOf: anObject startingAt: start ifAbsent: aBlock
  660. <
  661. self = self._numericallyIndexable();
  662. for(var i=start - 1; i < self.length; i++){
  663. if(self[i].__eq(anObject)) {return i+1}
  664. }
  665. return aBlock._value();
  666. >
  667. !
  668. last
  669. ^ self at: self size
  670. !
  671. last: aNumber
  672. "Answer the last aNumber elements of the receiver.
  673. Raise an error if there are not enough elements in the receiver."
  674. self size < aNumber ifTrue: [ self error: 'Invalid number of elements' ].
  675. ^ self copyFrom: self size - aNumber + 1 to: self size
  676. !
  677. second
  678. ^ self at: 2
  679. !
  680. third
  681. ^ self at: 3
  682. ! !
  683. !SequenceableCollection methodsFor: 'adding/removing'!
  684. addLast: anObject
  685. self add: anObject
  686. !
  687. removeLast
  688. ^ self remove: self last
  689. ! !
  690. !SequenceableCollection methodsFor: 'comparing'!
  691. = aCollection
  692. (self class = aCollection class and: [
  693. self size = aCollection size ]) ifFalse: [ ^ false ].
  694. self withIndexDo: [ :each :i |
  695. (aCollection at: i) = each ifFalse: [ ^ false ]].
  696. ^ true
  697. ! !
  698. !SequenceableCollection methodsFor: 'converting'!
  699. reversed
  700. self subclassResponsibility
  701. ! !
  702. !SequenceableCollection methodsFor: 'copying'!
  703. copyFrom: anIndex to: anotherIndex
  704. | range newCollection |
  705. range := anIndex to: anotherIndex.
  706. newCollection := self class new: range size.
  707. range withIndexDo: [ :each :i |
  708. newCollection at: i put: (self at: each) ].
  709. ^ newCollection
  710. !
  711. deepCopy
  712. | newCollection |
  713. newCollection := self class new: self size.
  714. self withIndexDo: [ :each :index |
  715. newCollection at: index put: each deepCopy ].
  716. ^ newCollection
  717. !
  718. shallowCopy
  719. | newCollection |
  720. newCollection := self class new: self size.
  721. self withIndexDo: [ :each :index |
  722. newCollection at: index put: each ].
  723. ^ newCollection
  724. ! !
  725. !SequenceableCollection methodsFor: 'enumerating'!
  726. detect: aBlock ifNone: anotherBlock
  727. <
  728. self = self._numericallyIndexable();
  729. for(var i = 0; i < self.length; i++)
  730. if(aBlock._value_(self[i]))
  731. return self[i];
  732. return anotherBlock._value();
  733. >
  734. !
  735. do: aBlock
  736. <
  737. self = self._numericallyIndexable();
  738. for(var i=0; i < self.length; i++) {
  739. aBlock._value_(self[i]);
  740. }
  741. >
  742. !
  743. with: anotherCollection do: aBlock
  744. <
  745. self = self._numericallyIndexable();
  746. anotherCollection = anotherCollection._numericallyIndexable();
  747. for(var i=0; i<self.length; i++) {
  748. aBlock._value_value_(self[i], anotherCollection[i]);
  749. }
  750. >
  751. !
  752. withIndexDo: aBlock
  753. <
  754. self = self._numericallyIndexable();
  755. for(var i=0; i < self.length; i++) {
  756. aBlock._value_value_(self[i], i+1);
  757. }
  758. >
  759. ! !
  760. !SequenceableCollection methodsFor: 'private'!
  761. numericallyIndexable
  762. "This is an internal converting message.
  763. It answeres a representation of the receiver
  764. that can use foo[i] in JavaScript code.
  765. It fixes IE8, where boxed String is unable
  766. to numerically index its characters,
  767. but primitive string can."
  768. self subclassResponsibility
  769. ! !
  770. !SequenceableCollection methodsFor: 'streaming'!
  771. newStream
  772. ^ self streamClass on: self
  773. !
  774. readStream
  775. "For Pharo compatibility"
  776. ^ self stream
  777. !
  778. stream
  779. ^ self newStream
  780. !
  781. streamClass
  782. ^ self class streamClass
  783. !
  784. writeStream
  785. "For Pharo compatibility"
  786. ^ self stream
  787. ! !
  788. !SequenceableCollection methodsFor: 'testing'!
  789. beginsWith: prefix
  790. self size < prefix size ifTrue: [ ^ false ].
  791. ^ (self first: prefix size) = prefix
  792. !
  793. endsWith: suffix
  794. self size < suffix size ifTrue: [ ^ false ].
  795. ^ (self last: suffix size) = suffix
  796. !
  797. includes: anObject
  798. ^ (self indexOf: anObject ifAbsent: [ nil ]) notNil
  799. ! !
  800. !SequenceableCollection class methodsFor: 'accessing'!
  801. streamClass
  802. ^ Stream
  803. ! !
  804. !SequenceableCollection class methodsFor: 'streaming'!
  805. streamContents: aBlock
  806. | stream |
  807. stream := (self streamClass on: self new).
  808. aBlock value: stream.
  809. ^ stream contents
  810. ! !
  811. SequenceableCollection subclass: #Array
  812. instanceVariableNames: ''
  813. package: 'Kernel-Collections'!
  814. !Array commentStamp!
  815. I represent a collection of objects ordered by the collector. The size of arrays is dynamic.
  816. I am directly mapped to JavaScript Number.
  817. *Note* In Amber, `OrderedCollection` is an alias for `Array`.!
  818. !Array methodsFor: 'accessing'!
  819. at: anIndex ifAbsent: aBlock
  820. <return anIndex >>= 1 && anIndex <= self.length
  821. ? self[anIndex - 1]
  822. : aBlock._value()>
  823. !
  824. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  825. <return anIndex >>= 1 && anIndex <= self.length
  826. ? aBlock._value_(self[anIndex - 1])
  827. : anotherBlock._value()>
  828. !
  829. at: anIndex put: anObject
  830. <return self[anIndex - 1] = anObject>
  831. !
  832. size
  833. <return self.length>
  834. ! !
  835. !Array methodsFor: 'adding/removing'!
  836. add: anObject
  837. <self.push(anObject); return anObject;>
  838. !
  839. addFirst: anObject
  840. <self.unshift(anObject); return anObject;>
  841. !
  842. remove: anObject ifAbsent: aBlock
  843. <
  844. for(var i=0;i<self.length;i++) {
  845. if(_st(self[i]).__eq(anObject)) {
  846. self.splice(i,1);
  847. return self;
  848. }
  849. };
  850. aBlock._value();
  851. >
  852. !
  853. removeAll
  854. <self.length = 0>
  855. !
  856. removeFrom: aNumber to: anotherNumber
  857. <self.splice(aNumber -1, anotherNumber - aNumber + 1)>
  858. !
  859. removeIndex: anInteger
  860. <self.splice(anInteger - 1, 1)>
  861. !
  862. removeLast
  863. <return self.pop();>
  864. ! !
  865. !Array methodsFor: 'converting'!
  866. asJavascript
  867. ^ '[', ((self collect: [:each | each asJavascript ]) join: ', '), ']'
  868. !
  869. reversed
  870. <return self._copy().reverse()>
  871. ! !
  872. !Array methodsFor: 'enumerating'!
  873. collect: aBlock
  874. "Optimized version"
  875. <return self.map(function(each) {return aBlock._value_(each)})>
  876. !
  877. join: aString
  878. <return self.join(aString)>
  879. !
  880. select: aBlock
  881. "Optimized version"
  882. <
  883. var result = self.klass._new();
  884. for(var i=0; i<self.length; i++) {
  885. if(aBlock._value_(self[i])) {
  886. result.push(self[i]);
  887. }
  888. }
  889. return result;
  890. >
  891. !
  892. sort
  893. ^ self basicPerform: 'sort'
  894. !
  895. sort: aBlock
  896. <
  897. return self.sort(function(a, b) {
  898. if(aBlock._value_value_(a,b)) {return -1} else {return 1}
  899. })
  900. >
  901. !
  902. sorted
  903. ^ self copy sort
  904. !
  905. sorted: aBlock
  906. ^ self copy sort: aBlock
  907. ! !
  908. !Array methodsFor: 'printing'!
  909. printOn: aStream
  910. super printOn: aStream.
  911. aStream nextPutAll: ' ('.
  912. self
  913. do: [ :each | each printOn: aStream ]
  914. separatedBy: [ aStream nextPutAll: ' ' ].
  915. aStream nextPutAll: ')'
  916. ! !
  917. !Array methodsFor: 'private'!
  918. numericallyIndexable
  919. ^ self
  920. ! !
  921. !Array class methodsFor: 'instance creation'!
  922. new: anInteger
  923. <return new Array(anInteger)>
  924. !
  925. with: anObject
  926. ^ (self new: 1)
  927. at: 1 put: anObject;
  928. yourself
  929. !
  930. with: anObject with: anObject2
  931. ^ (self new: 2)
  932. at: 1 put: anObject;
  933. at: 2 put: anObject2;
  934. yourself
  935. !
  936. with: anObject with: anObject2 with: anObject3
  937. ^ (self new: 3)
  938. at: 1 put: anObject;
  939. at: 2 put: anObject2;
  940. at: 3 put: anObject3;
  941. yourself
  942. !
  943. withAll: aCollection
  944. | instance index |
  945. index := 1.
  946. instance := self new: aCollection size.
  947. aCollection do: [ :each |
  948. instance at: index put: each.
  949. index := index + 1 ].
  950. ^ instance
  951. ! !
  952. SequenceableCollection subclass: #CharacterArray
  953. instanceVariableNames: ''
  954. package: 'Kernel-Collections'!
  955. !CharacterArray commentStamp!
  956. I am the abstract superclass of string-like collections.!
  957. !CharacterArray methodsFor: 'accessing'!
  958. at: anIndex put: anObject
  959. self errorReadOnly
  960. ! !
  961. !CharacterArray methodsFor: 'adding/removing'!
  962. add: anObject
  963. self errorReadOnly
  964. !
  965. remove: anObject
  966. self errorReadOnly
  967. ! !
  968. !CharacterArray methodsFor: 'converting'!
  969. asLowercase
  970. ^ self class fromString: self asString asLowercase
  971. !
  972. asNumber
  973. ^ self asString asNumber
  974. !
  975. asString
  976. ^ self subclassResponsibility
  977. !
  978. asSymbol
  979. ^ self asString
  980. !
  981. asUppercase
  982. ^ self class fromString: self asString asUppercase
  983. ! !
  984. !CharacterArray methodsFor: 'copying'!
  985. , aString
  986. ^ self asString, aString asString
  987. ! !
  988. !CharacterArray methodsFor: 'error handling'!
  989. errorReadOnly
  990. self error: 'Object is read-only'
  991. ! !
  992. !CharacterArray methodsFor: 'printing'!
  993. printOn: aStream
  994. self asString printOn: aStream
  995. ! !
  996. !CharacterArray methodsFor: 'streaming'!
  997. putOn: aStream
  998. aStream nextPutString: self
  999. ! !
  1000. !CharacterArray class methodsFor: 'instance creation'!
  1001. fromString: aString
  1002. self subclassResponsibility
  1003. ! !
  1004. CharacterArray subclass: #String
  1005. instanceVariableNames: ''
  1006. package: 'Kernel-Collections'!
  1007. !String commentStamp!
  1008. I am an indexed collection of Characters. Unlike most Smalltalk dialects, Amber doesn't provide the Character class. Instead, elements of a String are single character strings.
  1009. String inherits many useful methods from its hierarchy, such as
  1010. `Collection >> #,`!
  1011. !String methodsFor: 'accessing'!
  1012. asciiValue
  1013. <return self.charCodeAt(0);>
  1014. !
  1015. at: anIndex ifAbsent: aBlock
  1016. <return String(self)[anIndex - 1] || aBlock._value()>
  1017. !
  1018. at: anIndex ifPresent: aBlock ifAbsent: anotherBlock
  1019. <
  1020. var result = String(self)[anIndex - 1];
  1021. return result ? aBlock._value_(result) : anotherBlock._value();
  1022. >
  1023. !
  1024. charCodeAt: anInteger
  1025. < return self.charCodeAt(anInteger - 1) >
  1026. !
  1027. identityHash
  1028. ^ self, 's'
  1029. !
  1030. size
  1031. <return self.length>
  1032. ! !
  1033. !String methodsFor: 'comparing'!
  1034. < aString
  1035. <return String(self) < aString._asString()>
  1036. !
  1037. <= aString
  1038. <return String(self) <= aString._asString()>
  1039. !
  1040. = aString
  1041. <
  1042. if(typeof aString === 'undefined') { return false }
  1043. if(!!aString._isString || !! aString._isString()) {
  1044. return false;
  1045. }
  1046. return String(self) === String(aString)
  1047. >
  1048. !
  1049. == aString
  1050. ^ self = aString
  1051. !
  1052. > aString
  1053. <return String(self) >> aString._asString()>
  1054. !
  1055. >= aString
  1056. <return String(self) >>= aString._asString()>
  1057. ! !
  1058. !String methodsFor: 'converting'!
  1059. asJSON
  1060. ^ self
  1061. !
  1062. asJavascript
  1063. <
  1064. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1065. return "\"" + self.replace(/[\x00-\x1f"\\\x7f-\x9f]/g, function(ch){var c=ch.charCodeAt(0);return "\\x"+("0"+c.toString(16)).slice(-2)}) + "\"";
  1066. else
  1067. return "\"" + self + "\"";
  1068. >
  1069. !
  1070. asLowercase
  1071. <return self.toLowerCase()>
  1072. !
  1073. asMutator
  1074. "Answer a setter selector. For example,
  1075. #name asMutator returns #name:"
  1076. self last = ':' ifFalse: [ ^ self, ':' ].
  1077. ^ self
  1078. !
  1079. asNumber
  1080. <return Number(self)>
  1081. !
  1082. asRegexp
  1083. ^ RegularExpression fromString: self
  1084. !
  1085. asSelector
  1086. <return smalltalk.selector(self)>
  1087. !
  1088. asString
  1089. ^ self
  1090. !
  1091. asSymbol
  1092. ^ self
  1093. !
  1094. asUppercase
  1095. <return self.toUpperCase()>
  1096. !
  1097. capitalized
  1098. ^ self isEmpty
  1099. ifTrue: [ self ]
  1100. ifFalse: [ self first asUppercase, self allButFirst ]
  1101. !
  1102. crlfSanitized
  1103. ^ self lines join: String lf
  1104. !
  1105. escaped
  1106. <return escape(self)>
  1107. !
  1108. reversed
  1109. <return self.split("").reverse().join("")>
  1110. !
  1111. unescaped
  1112. <return unescape(self)>
  1113. !
  1114. uriComponentDecoded
  1115. <return decodeURIComponent(self)>
  1116. !
  1117. uriComponentEncoded
  1118. <return encodeURIComponent(self)>
  1119. !
  1120. uriDecoded
  1121. <return decodeURI(self)>
  1122. !
  1123. uriEncoded
  1124. <return encodeURI(self)>
  1125. ! !
  1126. !String methodsFor: 'copying'!
  1127. , aString
  1128. <return String(self) + aString>
  1129. !
  1130. copyFrom: anIndex to: anotherIndex
  1131. <return self.substring(anIndex - 1, anotherIndex)>
  1132. !
  1133. deepCopy
  1134. ^ self shallowCopy
  1135. !
  1136. shallowCopy
  1137. ^ self class fromString: self
  1138. ! !
  1139. !String methodsFor: 'printing'!
  1140. printNl
  1141. <console.log(self)>
  1142. !
  1143. printOn: aStream
  1144. aStream
  1145. nextPutAll: '''';
  1146. nextPutAll: self;
  1147. nextPutAll: ''''
  1148. ! !
  1149. !String methodsFor: 'private'!
  1150. numericallyIndexable
  1151. <return String(self)>
  1152. ! !
  1153. !String methodsFor: 'regular expressions'!
  1154. match: aRegexp
  1155. <return self.search(aRegexp) !!= -1>
  1156. !
  1157. matchesOf: aRegularExpression
  1158. <return self.match(aRegularExpression)>
  1159. !
  1160. replace: aString with: anotherString
  1161. ^ self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1162. !
  1163. replaceRegexp: aRegexp with: aString
  1164. <return self.replace(aRegexp, aString)>
  1165. !
  1166. trimBoth
  1167. ^ self trimBoth: '\s'
  1168. !
  1169. trimBoth: separators
  1170. ^ (self trimLeft: separators) trimRight: separators
  1171. !
  1172. trimLeft
  1173. ^ self trimLeft: '\s'
  1174. !
  1175. trimLeft: separators
  1176. ^ self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1177. !
  1178. trimRight
  1179. ^ self trimRight: '\s'
  1180. !
  1181. trimRight: separators
  1182. ^ self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1183. ! !
  1184. !String methodsFor: 'split join'!
  1185. join: aCollection
  1186. ^ String
  1187. streamContents: [ :stream | aCollection
  1188. do: [ :each | stream nextPutAll: each asString ]
  1189. separatedBy: [ stream nextPutAll: self ]]
  1190. !
  1191. lineIndicesDo: aBlock
  1192. "execute aBlock with 3 arguments for each line:
  1193. - start index of line
  1194. - end index of line without line delimiter
  1195. - end index of line including line delimiter(s) CR, LF or CRLF"
  1196. | cr lf start sz nextLF nextCR |
  1197. start := 1.
  1198. sz := self size.
  1199. cr := String cr.
  1200. nextCR := self indexOf: cr startingAt: 1.
  1201. lf := String lf.
  1202. nextLF := self indexOf: lf startingAt: 1.
  1203. [ start <= sz ] whileTrue: [
  1204. (nextLF = 0 and: [ nextCR = 0 ])
  1205. ifTrue: [ "No more CR, nor LF, the string is over"
  1206. aBlock value: start value: sz value: sz.
  1207. ^ self ].
  1208. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1209. ifTrue: [ "Found a LF"
  1210. aBlock value: start value: nextLF - 1 value: nextLF.
  1211. start := 1 + nextLF.
  1212. nextLF := self indexOf: lf startingAt: start ]
  1213. ifFalse: [ 1 + nextCR = nextLF
  1214. ifTrue: [ "Found a CR-LF pair"
  1215. aBlock value: start value: nextCR - 1 value: nextLF.
  1216. start := 1 + nextLF.
  1217. nextCR := self indexOf: cr startingAt: start.
  1218. nextLF := self indexOf: lf startingAt: start ]
  1219. ifFalse: [ "Found a CR"
  1220. aBlock value: start value: nextCR - 1 value: nextCR.
  1221. start := 1 + nextCR.
  1222. nextCR := self indexOf: cr startingAt: start ] ]]
  1223. !
  1224. lineNumber: anIndex
  1225. "Answer a string containing the characters in the given line number."
  1226. | lineCount |
  1227. lineCount := 0.
  1228. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1229. (lineCount := lineCount + 1) = anIndex ifTrue: [ ^ self copyFrom: start to: endWithoutDelimiters ]].
  1230. ^ nil
  1231. !
  1232. lines
  1233. "Answer an array of lines composing this receiver without the line ending delimiters."
  1234. | lines |
  1235. lines := Array new.
  1236. self linesDo: [ :aLine | lines add: aLine ].
  1237. ^ lines
  1238. !
  1239. linesDo: aBlock
  1240. "Execute aBlock with each line in this string. The terminating line
  1241. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1242. self lineIndicesDo: [ :start :endWithoutDelimiters :end |
  1243. aBlock value: (self copyFrom: start to: endWithoutDelimiters) ]
  1244. !
  1245. subStrings: aString
  1246. ^ self tokenize: aString
  1247. !
  1248. tokenize: aString
  1249. <return self.split(aString)>
  1250. ! !
  1251. !String methodsFor: 'testing'!
  1252. includesSubString: subString
  1253. < return self.indexOf(subString) !!= -1 >
  1254. !
  1255. isCapitalized
  1256. ^ self first asUppercase == self first
  1257. !
  1258. isImmutable
  1259. ^ true
  1260. !
  1261. isString
  1262. ^ true
  1263. !
  1264. isVowel
  1265. "Answer true if the receiver is a one character string containing a voyel"
  1266. ^ self size = 1 and: [ 'aeiou' includes: self asLowercase ]
  1267. ! !
  1268. !String class methodsFor: 'accessing'!
  1269. cr
  1270. <return '\r'>
  1271. !
  1272. crlf
  1273. <return '\r\n'>
  1274. !
  1275. esc
  1276. ^ self fromCharCode: 27
  1277. !
  1278. lf
  1279. <return '\n'>
  1280. !
  1281. space
  1282. <return ' '>
  1283. !
  1284. streamClass
  1285. ^ StringStream
  1286. !
  1287. tab
  1288. <return '\t'>
  1289. ! !
  1290. !String class methodsFor: 'instance creation'!
  1291. fromCharCode: anInteger
  1292. <return String.fromCharCode(anInteger)>
  1293. !
  1294. fromString: aString
  1295. <return String(aString)>
  1296. !
  1297. value: aUTFCharCode
  1298. <return String.fromCharCode(aUTFCharCode);>
  1299. ! !
  1300. !String class methodsFor: 'random'!
  1301. random
  1302. "Returns random alphanumeric string beginning with letter"
  1303. <return (Math.random()*(22/32)+(10/32)).toString(32).slice(2);>
  1304. !
  1305. randomNotIn: aString
  1306. | result |
  1307. [ result := self random. aString includesSubString: result ] whileTrue.
  1308. ^ result
  1309. ! !
  1310. Collection subclass: #Set
  1311. instanceVariableNames: 'elements'
  1312. package: 'Kernel-Collections'!
  1313. !Set commentStamp!
  1314. I represent an unordered set of objects without duplicates.!
  1315. !Set methodsFor: 'accessing'!
  1316. size
  1317. ^ elements size
  1318. ! !
  1319. !Set methodsFor: 'adding/removing'!
  1320. add: anObject
  1321. <
  1322. var found;
  1323. for(var i=0; i < self['@elements'].length; i++) {
  1324. if(_st(anObject).__eq(self['@elements'][i])) {
  1325. found = true;
  1326. break;
  1327. }
  1328. }
  1329. if(!!found) {self['@elements'].push(anObject)}
  1330. >
  1331. !
  1332. remove: anObject
  1333. elements remove: anObject
  1334. !
  1335. remove: anObject ifAbsent: aBlock
  1336. elements remove: anObject ifAbsent: aBlock
  1337. !
  1338. removeAll
  1339. elements removeAll
  1340. ! !
  1341. !Set methodsFor: 'comparing'!
  1342. = aCollection
  1343. self class = aCollection class ifFalse: [ ^ false ].
  1344. self size = aCollection size ifFalse: [ ^ false ].
  1345. self do: [ :each | (aCollection includes: each) ifFalse: [ ^ false ] ].
  1346. ^ true
  1347. ! !
  1348. !Set methodsFor: 'converting'!
  1349. asArray
  1350. ^ elements copy
  1351. ! !
  1352. !Set methodsFor: 'enumerating'!
  1353. collect: aBlock
  1354. ^ self class withAll: (elements collect: aBlock)
  1355. !
  1356. detect: aBlock ifNone: anotherBlock
  1357. ^ elements detect: aBlock ifNone: anotherBlock
  1358. !
  1359. do: aBlock
  1360. elements do: aBlock
  1361. !
  1362. select: aBlock
  1363. | collection |
  1364. collection := self class new.
  1365. self do: [ :each |
  1366. (aBlock value: each) ifTrue: [
  1367. collection add: each ]].
  1368. ^ collection
  1369. ! !
  1370. !Set methodsFor: 'initialization'!
  1371. initialize
  1372. super initialize.
  1373. elements := #()
  1374. ! !
  1375. !Set methodsFor: 'printing'!
  1376. printOn: aStream
  1377. super printOn: aStream.
  1378. aStream nextPutAll: ' ('.
  1379. self
  1380. do: [ :each | each printOn: aStream ]
  1381. separatedBy: [ aStream nextPutAll: ' ' ].
  1382. aStream nextPutAll: ')'
  1383. ! !
  1384. !Set methodsFor: 'testing'!
  1385. includes: anObject
  1386. ^ elements includes: anObject
  1387. ! !
  1388. Object subclass: #Queue
  1389. instanceVariableNames: 'read readIndex write'
  1390. package: 'Kernel-Collections'!
  1391. !Queue commentStamp!
  1392. I am a one-sided queue.
  1393. ## Usage
  1394. Use `#nextPut:` to add items to the queue.
  1395. Use `#next` or `#nextIfAbsent:` to get (and remove) the next item in the queue.
  1396. ## Implementation notes
  1397. A Queue uses two OrderedCollections inside,
  1398. `read` is at the front, is not modified and only read using `readIndex`.
  1399. `write` is at the back and is appended new items.
  1400. When `read` is exhausted, `write` is promoted to `read` and new `write` is created.
  1401. As a consequence, no data moving is done by me, write appending may do data moving
  1402. when growing `write`, but this is left to engine to implement as good as it chooses to.!
  1403. !Queue methodsFor: 'accessing'!
  1404. next
  1405. ^ self nextIfAbsent: [ self error: 'Cannot read from empty Queue.' ]
  1406. !
  1407. nextIfAbsent: aBlock
  1408. | result |
  1409. result := read at: readIndex ifAbsent: [
  1410. write isEmpty ifTrue: [
  1411. readIndex > 1 ifTrue: [ read := #(). readIndex := 1 ].
  1412. ^ aBlock value ].
  1413. read := write.
  1414. readIndex := 1.
  1415. write := OrderedCollection new.
  1416. read first ].
  1417. read at: readIndex put: nil.
  1418. readIndex := readIndex + 1.
  1419. ^ result
  1420. !
  1421. nextPut: anObject
  1422. write add: anObject
  1423. ! !
  1424. !Queue methodsFor: 'initialization'!
  1425. initialize
  1426. super initialize.
  1427. read := OrderedCollection new.
  1428. write := OrderedCollection new.
  1429. readIndex := 1
  1430. ! !
  1431. Object subclass: #RegularExpression
  1432. instanceVariableNames: ''
  1433. package: 'Kernel-Collections'!
  1434. !RegularExpression commentStamp!
  1435. I represent a regular expression object. My instances are JavaScript `RegExp` object.!
  1436. !RegularExpression methodsFor: 'evaluating'!
  1437. compile: aString
  1438. <return self.compile(aString)>
  1439. !
  1440. exec: aString
  1441. <return self.exec(aString) || nil>
  1442. !
  1443. test: aString
  1444. <return self.test(aString)>
  1445. ! !
  1446. !RegularExpression class methodsFor: 'instance creation'!
  1447. fromString: aString
  1448. ^ self fromString: aString flag: ''
  1449. !
  1450. fromString: aString flag: anotherString
  1451. <return new RegExp(aString, anotherString)>
  1452. ! !
  1453. Object subclass: #Stream
  1454. instanceVariableNames: 'collection position streamSize'
  1455. package: 'Kernel-Collections'!
  1456. !Stream commentStamp!
  1457. I represent an accessor for a sequence of objects. This sequence is referred to as my "contents".
  1458. My instances are read/write streams to the contents sequence collection.!
  1459. !Stream methodsFor: 'accessing'!
  1460. collection
  1461. ^ collection
  1462. !
  1463. contents
  1464. ^ self collection
  1465. copyFrom: 1
  1466. to: self streamSize
  1467. !
  1468. position
  1469. ^ position ifNil: [ position := 0 ]
  1470. !
  1471. position: anInteger
  1472. position := anInteger
  1473. !
  1474. setCollection: aCollection
  1475. collection := aCollection
  1476. !
  1477. setStreamSize: anInteger
  1478. streamSize := anInteger
  1479. !
  1480. size
  1481. ^ self streamSize
  1482. !
  1483. streamSize
  1484. ^ streamSize
  1485. ! !
  1486. !Stream methodsFor: 'actions'!
  1487. close
  1488. !
  1489. flush
  1490. !
  1491. reset
  1492. self position: 0
  1493. !
  1494. resetContents
  1495. self reset.
  1496. self setStreamSize: 0
  1497. ! !
  1498. !Stream methodsFor: 'enumerating'!
  1499. do: aBlock
  1500. [ self atEnd ] whileFalse: [ aBlock value: self next ]
  1501. ! !
  1502. !Stream methodsFor: 'positioning'!
  1503. setToEnd
  1504. self position: self size
  1505. !
  1506. skip: anInteger
  1507. self position: ((self position + anInteger) min: self size max: 0)
  1508. ! !
  1509. !Stream methodsFor: 'reading'!
  1510. next
  1511. ^ self atEnd
  1512. ifTrue: [ nil ]
  1513. ifFalse: [
  1514. self position: self position + 1.
  1515. collection at: self position ]
  1516. !
  1517. next: anInteger
  1518. | tempCollection |
  1519. tempCollection := self collection class new.
  1520. anInteger timesRepeat: [
  1521. self atEnd ifFalse: [
  1522. tempCollection add: self next ]].
  1523. ^ tempCollection
  1524. !
  1525. peek
  1526. ^ self atEnd ifFalse: [
  1527. self collection at: self position + 1 ]
  1528. ! !
  1529. !Stream methodsFor: 'testing'!
  1530. atEnd
  1531. ^ self position = self size
  1532. !
  1533. atStart
  1534. ^ self position = 0
  1535. !
  1536. isEmpty
  1537. ^ self size = 0
  1538. ! !
  1539. !Stream methodsFor: 'writing'!
  1540. << anObject
  1541. self write: anObject
  1542. !
  1543. nextPut: anObject
  1544. self position: self position + 1.
  1545. self collection at: self position put: anObject.
  1546. self setStreamSize: (self streamSize max: self position)
  1547. !
  1548. nextPutAll: aCollection
  1549. aCollection do: [ :each |
  1550. self nextPut: each ]
  1551. !
  1552. nextPutString: aString
  1553. self nextPut: aString
  1554. !
  1555. write: anObject
  1556. anObject putOn: self
  1557. ! !
  1558. !Stream class methodsFor: 'instance creation'!
  1559. on: aCollection
  1560. ^ self new
  1561. setCollection: aCollection;
  1562. setStreamSize: aCollection size;
  1563. yourself
  1564. ! !
  1565. Stream subclass: #StringStream
  1566. instanceVariableNames: ''
  1567. package: 'Kernel-Collections'!
  1568. !StringStream commentStamp!
  1569. I am a Stream specific to `String` objects.!
  1570. !StringStream methodsFor: 'reading'!
  1571. next: anInteger
  1572. | tempCollection |
  1573. tempCollection := self collection class new.
  1574. anInteger timesRepeat: [
  1575. self atEnd ifFalse: [
  1576. tempCollection := tempCollection, self next ]].
  1577. ^ tempCollection
  1578. ! !
  1579. !StringStream methodsFor: 'writing'!
  1580. cr
  1581. ^ self nextPutAll: String cr
  1582. !
  1583. crlf
  1584. ^ self nextPutAll: String crlf
  1585. !
  1586. lf
  1587. ^ self nextPutAll: String lf
  1588. !
  1589. nextPut: aString
  1590. self nextPutAll: aString
  1591. !
  1592. nextPutAll: aString
  1593. | pre post |
  1594. self atEnd ifTrue: [ self setCollection: self collection, aString ] ifFalse: [
  1595. pre := self collection copyFrom: 1 to: self position.
  1596. post := self collection copyFrom: (self position + 1 + aString size) to: self collection size.
  1597. self setCollection: pre, aString, post
  1598. ].
  1599. self position: self position + aString size.
  1600. self setStreamSize: (self streamSize max: self position)
  1601. !
  1602. nextPutString: aString
  1603. self nextPutAll: aString
  1604. !
  1605. space
  1606. self nextPut: ' '
  1607. !
  1608. tab
  1609. ^ self nextPutAll: String tab
  1610. ! !