[Pharo-users] Re: BlockClosure folding

2022-03-16 Thread Richard O'Keefe
Pattern?  I'd call it an antipattern myself.
Note that we can already construct
  { block1
  . block2
  . block3
  }
and write
  { block1
  . block2
  . block3
  } allSatisfy: [:each | each value].
so we don't have to abuse the #, selector
or write a single new method to get the "flattened"
structure you're after.

Collection
  methods for: 'collections of blocks'
allSatisfied
  ^self allSatisfy: [:each | each value]
anySatisfied
  ^self anySatisfy: [:each | each value]
noneSatisfied
  ^self noneSatisfy: [:each | each value]

if you do feel like adding a few methods, whereupon
  { block1
  . block2
  . block3
  } allSatisfied

Didn't VW recently implement {} syntax natively?
Or was that VAST?

In all seriousness, when is it better to use
ComplexCondition than to rewrite?



On Wed, 16 Mar 2022 at 14:38, Hernán Morales Durand <
hernan.mora...@gmail.com> wrote:

> This is an interesting pattern.
> Thank you for sharing.
>
> Hernán
>
> El mar, 15 mar 2022 a las 4:43, Julián Maestri ()
> escribió:
>
>> Not satisfying the equality, but you can use polymorphism.
>>
>> Block >> , aBlock
>> ^ BlockCompositor andAll: OrderedCollection with: self with: aBlock
>>
>> BlockCompositor >> #, aBlock
>> conditions add: aBlock.
>>
>> BlockCompositor >> value: anObject
>> ^ conditions allSatisfy: [:e | e value: anObject ]
>>
>> I don't really like the name BlockCompositor, but can't think of a better
>> name at the moment.
>>
>> You could also implement logic for #or: using #+ and: #anySatisfy: for
>> example, but need to safeguard against mixing both conditions.
>>
>> On Mon, 14 Mar 2022 at 22:29, Hernán Morales Durand <
>> hernan.mora...@gmail.com> wrote:
>>
>>> I think I saw a coding pattern a while ago that allows you to do the
>>> following:
>>>
>>> cond1 , cond2 , cond3 , cond4
>>>
>>> And providing a kind of folding selector condition #and: you would get:
>>>
>>> [ cond1 and: [ cond2 and: [ cond3 and: [ cond4 ] ] ] ].
>>>
>>> for example:
>>>
>>> conditions := [ : each | each firstName = 'Boca' ] ,
>>> [ : each | each lastName = 'Baret' ] ,
>>> [ : each | each fullName = 'Virgasia' ].
>>>
>>> such that the following assert is met:
>>>
>>> self assert: conditions equals: [ : each | each firstName = 'Boca' and:
>>> [ each lastName = 'Baret' and: [ each fullName = 'Virgasia' ] ] ].
>>>
>>> Any ideas or pointers?
>>>
>>> Cheers,
>>>
>>> Hernán
>>>
>>>


[Pharo-users] Re: BlockClosure folding

2022-03-16 Thread Richard O'Keefe
The motivating example can be written

aHasDot := a includes: $..
bHasDot := b includes: $..
^aHasDot = bHasDot
   ifFalse: [bHasDot]
   ifTrue:  [aHasDot
   ifTrue:  [a < b]
   ifFalse: [a > b]]

(Dijkstra was right.  People *do* forget that '=' is
one of the standard Boolean operations, and it matters.)
This is absolutely straightforward, and without wasting
any time wondering what the three-headed IF monster is,
we can go straight to the big puzzle: "I can see at a
glance what this DOES, but what the heck does it MEAN?"
When would this be useful?  What would be a good name for
this combination, however expressed?

I think the moral here is "when you have code whose
structure is so complex that you are tempted to construct
special control flow objects to make it LOOK simpler,
rewrite it so that it IS simpler."

I once wrote a class called Distributor based on Boost
'signal's.  It was an interesting exercise, and I am
still finding mistakes in it.  It was not entirely unlike
ComplexCondition.  What I *haven't* done in the 10 years
since I wrote it is found any use for it.


On Wed, 16 Mar 2022 at 07:15,  wrote:

> ComplexCondition as described in Andres Valloud, "A Mentoring Course on
> Smalltalk"
> 
> is available at http://www.squeaksource.com/ComplexCondition.html
>
> With it, you can do something like:
>
> ^[a includes: $.], [b includes: $.]
> ifAllTrue: [a < b]
> ifAnyTrue: [b includes: $.]
> otherwise: [a > b]
>
>


[Pharo-users] Re: BlockClosure folding

2022-03-15 Thread Hernán Morales Durand
This is an interesting pattern.
Thank you for sharing.

Hernán

El mar, 15 mar 2022 a las 4:43, Julián Maestri ()
escribió:

> Not satisfying the equality, but you can use polymorphism.
>
> Block >> , aBlock
> ^ BlockCompositor andAll: OrderedCollection with: self with: aBlock
>
> BlockCompositor >> #, aBlock
> conditions add: aBlock.
>
> BlockCompositor >> value: anObject
> ^ conditions allSatisfy: [:e | e value: anObject ]
>
> I don't really like the name BlockCompositor, but can't think of a better
> name at the moment.
>
> You could also implement logic for #or: using #+ and: #anySatisfy: for
> example, but need to safeguard against mixing both conditions.
>
> On Mon, 14 Mar 2022 at 22:29, Hernán Morales Durand <
> hernan.mora...@gmail.com> wrote:
>
>> I think I saw a coding pattern a while ago that allows you to do the
>> following:
>>
>> cond1 , cond2 , cond3 , cond4
>>
>> And providing a kind of folding selector condition #and: you would get:
>>
>> [ cond1 and: [ cond2 and: [ cond3 and: [ cond4 ] ] ] ].
>>
>> for example:
>>
>> conditions := [ : each | each firstName = 'Boca' ] ,
>> [ : each | each lastName = 'Baret' ] ,
>> [ : each | each fullName = 'Virgasia' ].
>>
>> such that the following assert is met:
>>
>> self assert: conditions equals: [ : each | each firstName = 'Boca' and: [
>> each lastName = 'Baret' and: [ each fullName = 'Virgasia' ] ] ].
>>
>> Any ideas or pointers?
>>
>> Cheers,
>>
>> Hernán
>>
>>


[Pharo-users] Re: BlockClosure folding

2022-03-15 Thread Hernán Morales Durand
Exactly. This was what I was looking for.
Thank you Sean.

Hernán


El mar, 15 mar 2022 a las 19:15,  escribió:

> ComplexCondition as described in Andres Valloud, "A Mentoring Course on
> Smalltalk"
> 
> is available at http://www.squeaksource.com/ComplexCondition.html
>
> With it, you can do something like:
>
> ^[a includes: $.], [b includes: $.]
> ifAllTrue: [a < b]
> ifAnyTrue: [b includes: $.]
> otherwise: [a > b]
>
>


[Pharo-users] Re: BlockClosure folding

2022-03-15 Thread sean
ComplexCondition as described in [Andres Valloud, "A Mentoring Course on 
Smalltalk"](http://www.lulu.com/product/paperback/a-mentoring-course-on-smalltalk/3788890)
 is available at http://www.squeaksource.com/ComplexCondition.html

With it, you can do something like:

```
^[a includes: $.], [b includes: $.]
ifAllTrue: [a < b]
ifAnyTrue: [b includes: $.]
otherwise: [a > b]
```


[Pharo-users] Re: BlockClosure folding

2022-03-14 Thread Julián Maestri
Not satisfying the equality, but you can use polymorphism.

Block >> , aBlock
^ BlockCompositor andAll: OrderedCollection with: self with: aBlock

BlockCompositor >> #, aBlock
conditions add: aBlock.

BlockCompositor >> value: anObject
^ conditions allSatisfy: [:e | e value: anObject ]

I don't really like the name BlockCompositor, but can't think of a better
name at the moment.

You could also implement logic for #or: using #+ and: #anySatisfy: for
example, but need to safeguard against mixing both conditions.

On Mon, 14 Mar 2022 at 22:29, Hernán Morales Durand <
hernan.mora...@gmail.com> wrote:

> I think I saw a coding pattern a while ago that allows you to do the
> following:
>
> cond1 , cond2 , cond3 , cond4
>
> And providing a kind of folding selector condition #and: you would get:
>
> [ cond1 and: [ cond2 and: [ cond3 and: [ cond4 ] ] ] ].
>
> for example:
>
> conditions := [ : each | each firstName = 'Boca' ] ,
> [ : each | each lastName = 'Baret' ] ,
> [ : each | each fullName = 'Virgasia' ].
>
> such that the following assert is met:
>
> self assert: conditions equals: [ : each | each firstName = 'Boca' and: [
> each lastName = 'Baret' and: [ each fullName = 'Virgasia' ] ] ].
>
> Any ideas or pointers?
>
> Cheers,
>
> Hernán
>
>