Hi guys I need the following behavior and I started to implement it (but Iâm not sure that I implemented in a good way). But may be this method already exist. testDiffs "self run: #testDiffs" self assert: (#(a b c d e f) diff: #(a b z k)) equals: {#(c d e f) . #(z k)}. Stef
On 13.03.2014, at 08:52, Pharo4Stef <pharo4Stef@free.fr> wrote:
Hi guys
I need the following behavior and I started to implement it (but Iâm not sure that I implemented in a good way). But may be this method already exist.
testDiffs "self run: #testDiffs"
self assert: (#(a b c d e f) diff: #(a b z k)) equals: {#(c d e f) . #(z k)}.
isnât that the same as you would get with #difference:?
Stef
On Thu, Mar 13, 2014 at 8:52 AM, Pharo4Stef <pharo4Stef@free.fr> wrote:
#(a b c d e f) diff: #(a b z k)
Use #difference: #(a b c d e f) difference: #(a b z k). ==> #(#f #d #e #c) #(a b z k) difference: #(a b c d e f) ==> #(#k #z) -- Damien Cassou http://damiencassou.seasidehosting.st "Success is the ability to go from one failure to another without losing enthusiasm." Winston Churchill
I was thinking that I could do it in one pass.
On Thu, Mar 13, 2014 at 8:52 AM, Pharo4Stef <pharo4Stef@free.fr> wrote:
#(a b c d e f) diff: #(a b z k)
Use #difference:
#(a b c d e f) difference: #(a b z k). ==> #(#f #d #e #c) #(a b z k) difference: #(a b c d e f) ==> #(#k #z)
-- Damien Cassou http://damiencassou.seasidehosting.st
"Success is the ability to go from one failure to another without losing enthusiasm." Winston Churchill
something like that diffs: aCollection "Answer the set theoretic differences of two collections. The first element of the result is the difference from the perspective of the receiver and the second element the difference from the perspective of the argument." "#(a b c d e f) diffs: #(a b z k) #(#a #b #c #d #e #f) { #(#f #d #e #c) . #(#k #z)} " "{ self difference: aCollection . aCollection difference: self }" | receiver another | receiver := self asSet. another := aCollection asSet. self do: [ :each | (another includes: each) ifTrue: [ receiver remove: each. another remove: each ]]. ^ { receiver asArray . another asArray}
Are the final #asArray conversions always needed ? If not, it would be more efficient not to do them every time, no ? On 14 Mar 2014, at 22:19, Pharo4Stef <pharo4Stef@free.fr> wrote:
something like that
diffs: aCollection "Answer the set theoretic differences of two collections. The first element of the result is the difference from the perspective of the receiver and the second element the difference from the perspective of the argument."
"#(a b c d e f) diffs: #(a b z k) #(#a #b #c #d #e #f) { #(#f #d #e #c) . #(#k #z)} " "{ self difference: aCollection . aCollection difference: self }"
| receiver another | receiver := self asSet. another := aCollection asSet. self do: [ :each | (another includes: each) ifTrue: [ receiver remove: each. another remove: each ]]. ^ { receiver asArray . another asArray}
I have to think if the asSet is needed too. In fact I want in addition intersectionAndDifferences: aColl because I want to be able to define nicely merge: aDict onKeyConflictDoValues: aBlock as follow | d d2 | d := #(1 3 4 5 ) groupedBy: #even. d2 := #(10 31 41 50 ) groupedBy: #even. d merge: d2 onKeyConflictDoValues: [ :aValue :anotherValue | (aValue, anotherValue) asSet asOrderedCollection ] Because manipulating dictionaries whose values are collections is boring. Stef On 14 Mar 2014, at 23:15, Sven Van Caekenberghe <sven@stfx.eu> wrote:
Are the final #asArray conversions always needed ? If not, it would be more efficient not to do them every time, no ?
On 14 Mar 2014, at 22:19, Pharo4Stef <pharo4Stef@free.fr> wrote:
something like that
diffs: aCollection "Answer the set theoretic differences of two collections. The first element of the result is the difference from the perspective of the receiver and the second element the difference from the perspective of the argument."
"#(a b c d e f) diffs: #(a b z k) #(#a #b #c #d #e #f) { #(#f #d #e #c) . #(#k #z)} " "{ self difference: aCollection . aCollection difference: self }"
| receiver another | receiver := self asSet. another := aCollection asSet. self do: [ :each | (another includes: each) ifTrue: [ receiver remove: each. another remove: each ]]. ^ { receiver asArray . another asArray}
Now I can merge dictionaries in a nice way testMergeWithNonOverlappingKeys "self run: #testMerge" | d d2 d3 | d := Dictionary new at: #x put: #(x y z) ; at: #y put: #(e f g ) ; at: #a put: #(a b c); yourself. d2 := Dictionary new at: #x put: #(x y z) ; at: #y put: #( h i j) ; yourself. d3 := d merge: d2 onKeyConflictDoValues: [ :aValue :anotherValue | (aValue, anotherValue) asSet asOrderedCollection ]. self assert: (d3 at: #y) equals: #(e f g h i j). self assert: (d3 at: #a) equals: #(a b c ). self assert: (d3 at: #x) equals: #(x y z).
participants (4)
-
Damien Cassou -
Max Leske -
Pharo4Stef -
Sven Van Caekenberghe