Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
The reason for the error  is that the method `#flatCollect:as:` inserts elements into the collection type given as second argument. String is immutable, so that is not possible.
  • Loading branch information
kasperosterbye committed Sep 13, 2019
1 parent 10d18f2 commit 13a9eca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Collections-Abstract/Collection.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -695,11 +695,11 @@ Collection >> flatCollect: aBlock as: aCollectionClass [
"Evaluate aBlock for each of the receiver's elements and answer the
list of all resulting values flatten one level. Assumes that aBlock returns some kind
of collection for each element. Equivalent to the lisp's mapcan"

| col |
col := aCollectionClass new: self size.
self do: [ :each |
col addAll: (aBlock value: each) ].
^col
col := OrderedCollection new: self size.
self do: [ :each | col addAll: (aBlock value: each) ].
^ aCollectionClass withAll: col
]

{ #category : #enumerating }
Expand Down
8 changes: 7 additions & 1 deletion src/Collections-Tests/TEnumeratingTest.trait.st
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ TEnumeratingTest >> testFlatCollectAs [

self
assert: (self simpleCollection flatCollect: [ :x | { x }, { x } ] as: IdentitySet)
equals: self simpleCollection asIdentitySet
equals: self simpleCollection asIdentitySet.
self
assert: (#(foo bar baz) flatCollect: #yourself as: String)
equals: 'foobarbaz'.
self
assert: (#(#(1 2) #(4 3)) flatCollect: #yourself as: Array)
equals: #(1 2 4 3)
]

{ #category : #'tests - enumerating' }
Expand Down

0 comments on commit 13a9eca

Please sign in to comment.