Set on Attribute

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Set on Attribute

Sean P. DeNigris
Administrator
Say I have a collection like #('ab' 'ac' 'ba' bc') and I want to condense it so that a certain attribute is unique. In this example, say the first character, so I want one object where the first character is $a and one $b, but I don't care which object i.e. 'ab' or 'ac' for $a, but not both.

Is there an elegant way to do that?
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Set on Attribute

Sven Van Caekenberghe-2
PluggableSet ?

> On 26 Aug 2016, at 01:42, Sean P. DeNigris <[hidden email]> wrote:
>
> Say I have a collection like #('ab' 'ac' 'ba' bc') and I want to condense it
> so that a certain attribute is unique. In this example, say the first
> character, so I want one object where the first character is $a and one $b,
> but I don't care which object i.e. 'ab' or 'ac' for $a, but not both.
>
> Is there an elegant way to do that?
>
>
>
> -----
> Cheers,
> Sean
> --
> View this message in context: http://forum.world.st/Set-on-Attribute-tp4912672.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: Set on Attribute

monty-3
In reply to this post by Sean P. DeNigris
Other than PluggableSet, something like Unix's sort and uniq combo would work:

condenseOnFirst: aCollection
| lastSelected |

^ aCollection sorted select: [:each |
        (lastSelected isNil
                or: [lastSelected first ~= each first])
                ifTrue: [
                        lastSelected := each.
                        true]
                ifFalse: [false]]



> Sent: Thursday, August 25, 2016 at 7:42 PM
> From: "Sean P. DeNigris" <[hidden email]>
> To: [hidden email]
> Subject: [Pharo-users] Set on Attribute
>
> Say I have a collection like #('ab' 'ac' 'ba' bc') and I want to condense it
> so that a certain attribute is unique. In this example, say the first
> character, so I want one object where the first character is $a and one $b,
> but I don't care which object i.e. 'ab' or 'ac' for $a, but not both.
>
> Is there an elegant way to do that?
>
>
>
> -----
> Cheers,
> Sean
> --
> View this message in context: http://forum.world.st/Set-on-Attribute-tp4912672.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Set on Attribute

Sean P. DeNigris
Administrator
In reply to this post by Sven Van Caekenberghe-2
Sven Van Caekenberghe-2 wrote
PluggableSet ?
Perfect! Thanks :)

My actual usage, in case anyone finds it helpful, was removing duplicate files:
        uniqueFiles := PluggableSet new
                equalBlock: [ :a :b | a isMD5Equal: b ];
                hashBlock: [ :f | f binaryReadStreamDo: [ :str | (MD5 hashStream: str) hex hash ] ]
                yourself.
        allFiles := folder filesMatching: '*.eml'.
        uniqueFiles addAll: allFiles.
        (allFiles difference: uniqueFiles) do: #delete.
Cheers,
Sean