"http://p...content-available-to-author-only...h.net/test/read.cgi/tech/1418278634/438"
Object subclass: Likes [
| likes name |
initialize [likes := OrderedCollection new]
allOneLikes [^likes
inject: #()
into: [:arr :who | arr, (Likes members asArray select: who)]
]
definitelyLikes: who [
(who isKindOf: BlockClosure) ifTrue: [
^(Likes members select: who) allSatisfy: [:each | self definitelyLikes: each]
].
(who isKindOf: Collection) ifTrue: [
^who allSatisfy: [:each | self definitelyLikes: each]
].
^likes includes: who
]
likes: who [
(who isKindOf: Collection)
ifTrue: [likes addAll: who] ifFalse: [likes add: who].
^true
]
named: newName [name := newName]
value: who [^self == who]
printOn: aStream [aStream nextPutAll: name]
]
Likes class extend [
| members |
members [^members ifNil: [self members: Set new; members]]
members: set [members := set asSet]
named: str [
| instance |
instance := self new initialize.
instance named: str.
^instance
]
]
Likes subclass: Animals []
Likes subclass: People []
| Kim Robin Sandy Lee cats |
Likes members: {
Kim := People named: #Kim.
Robin := People named: #Robin.
Sandy := People named: #Sandy.
Lee := People named: #Lee.
cats := Animals named: #cats
}.
Kim likes: Robin.
"Lee likes: Kim."
Sandy likes: Kim.
Robin likes: cats.
Sandy likes: [:who |
((who isKindOf: People) or: [who isKindOf: Animals]) and: [who definitelyLikes: cats]].
Kim likes: [:who |
(who isKindOf: People) and: [who definitelyLikes: {Lee. Kim}]].
Sandy likes: Lee.
Likes members do: [:who |
((who isKindOf: People) or: [who isKindOf: Animals]) ifTrue: [who likes: who]].
Likes members do: [:each | (each -> each allOneLikes) printNl]