What does reduce: get used for?

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

What does reduce: get used for?

Rick Flower
I'm not completely following what its purpose is?  Below is the code  
from the Magritte, living in the SequenceableCollection object:

reduce: aBlock
        | result |
        self isEmpty
                ifTrue: [ ^ nil ].
        result := self first.
        2 to: self size do: [ :index |
                result := aBlock
                        value: result
                        value: (self at: index) ].
        ^ result

In my case, it is causing problems on VW when it calls aBlock (near  
the end) and passes in a pair of Characters which are processed by  
MAComponentRenderer>>classFor: which then tries to do a string append  
(#,) which gives a DNU since VW's characters (and I think Squeak from  
what I can tell) do not have concat methods (#,).. Am I missing  
something?  Below is the code that calls reduce:

MAComponentRenderer>>classFor: aDescription

        | classes item |

        item := (component description = aDescription
                ifTrue: [ component ]
                ifFalse: [ self childAt: aDescription ]) ifNotNilDo: [ :comp | comp  
model ].
       
        classes := aDescription cssClassesForItem: item.

        (self hasError: aDescription) ifTrue: [ classes add: 'error' ].

        ^ classes reduce: [ :a :b | a , ' ' , b ]




_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: What does reduce: get used for?

Lukas Renggli-2
> I'm not completely following what its purpose is?

It is a left-fold:

   http://en.wikipedia.org/wiki/Fold_(higher-order_function)

> In my case, it is causing problems on VW when it calls aBlock (near
> the end) and passes in a pair of Characters which are processed by
> MAComponentRenderer>>classFor: which then tries to do a string append
> (#,) which gives a DNU since VW's characters (and I think Squeak from
> what I can tell) do not have concat methods (#,).. Am I missing
> something?

The idea is that when you do

   #( 'foo' 'bar' 'zork' ) reduce: [ :a :b | a , ' ' , b ]

you get

   'foo bar zork'

I don't know how your collection 'classes' ends up with characters?  
These should be strings. Maybe there is a bug in #cssClassesForItem:?

I don't have such a method in Squeak though and the original  
implementation of #classFor: looks quite different:

MAComponentRenderer>>classFor: aDescription
        | classes |
        classes := OrderedCollection withAll: aDescription cssClasses.
        aDescription isReadonly
                ifTrue: [ classes add: 'readonly' ].
        aDescription isRequired
                ifTrue: [ classes add: 'required' ].
        (self hasError: aDescription)
                ifTrue: [ classes add: 'error' ].
        ^ classes reduce: [ :a :b | a , ' ' , b ]

Cheers,
Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch


_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: What does reduce: get used for?

keith1y
Lukas Renggli wrote:

>> I'm not completely following what its purpose is?
>>    
>
> It is a left-fold:
>
>    http://en.wikipedia.org/wiki/Fold_(higher-order_function)
>
>  
>> In my case, it is causing problems on VW when it calls aBlock (near
>> the end) and passes in a pair of Characters which are processed by
>> MAComponentRenderer>>classFor: which then tries to do a string append
>> (#,) which gives a DNU since VW's characters (and I think Squeak from
>> what I can tell) do not have concat methods (#,).. Am I missing
>> something?
>>    
>
> The idea is that when you do
>
>    #( 'foo' 'bar' 'zork' ) reduce: [ :a :b | a , ' ' , b ]
>
> you get
>
>    'foo bar zork'
>
> I don't know how your collection 'classes' ends up with characters?  
> These should be strings. Maybe there is a bug in #cssClassesForItem:?
>
> I don't have such a method in Squeak though and the original  
> implementation of #classFor: looks quite different:
>
> MAComponentRenderer>>classFor: aDescription
> | classes |
> classes := OrderedCollection withAll: aDescription cssClasses.
> aDescription isReadonly
> ifTrue: [ classes add: 'readonly' ].
> aDescription isRequired
> ifTrue: [ classes add: 'required' ].
> (self hasError: aDescription)
> ifTrue: [ classes add: 'error' ].
> ^ classes reduce: [ :a :b | a , ' ' , b ]
>
> Cheers,
> Lukas
>
>  
If looks like you have put a string into the cssClasses property, where
an Array/Collection of strings is expected.

Keith


_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki
Reply | Threaded
Open this post in threaded view
|

Re: What does reduce: get used for?

Rick Flower
Thanks Keith & Lukas.. Keith was right.. I was passing in a string  
instead of an array of strings.. Oh well.. Live and learn..
It's working MUCH better now!

-- Rick

On Aug 18, 2008, at 8:28 AM, Keith Hodges wrote:

> Lukas Renggli wrote:
>>> I'm not completely following what its purpose is?
>>>
>>
>> It is a left-fold:
>>
>>   http://en.wikipedia.org/wiki/Fold_(higher-order_function)
>>
>>
>>> In my case, it is causing problems on VW when it calls aBlock (near
>>> the end) and passes in a pair of Characters which are processed by
>>> MAComponentRenderer>>classFor: which then tries to do a string  
>>> append
>>> (#,) which gives a DNU since VW's characters (and I think Squeak  
>>> from
>>> what I can tell) do not have concat methods (#,).. Am I missing
>>> something?
>>>
>>
>> The idea is that when you do
>>
>>   #( 'foo' 'bar' 'zork' ) reduce: [ :a :b | a , ' ' , b ]
>>
>> you get
>>
>>   'foo bar zork'
>>
>> I don't know how your collection 'classes' ends up with characters?
>> These should be strings. Maybe there is a bug in #cssClassesForItem:?
>>
>> I don't have such a method in Squeak though and the original
>> implementation of #classFor: looks quite different:
>>
>> MAComponentRenderer>>classFor: aDescription
>> | classes |
>> classes := OrderedCollection withAll: aDescription cssClasses.
>> aDescription isReadonly
>> ifTrue: [ classes add: 'readonly' ].
>> aDescription isRequired
>> ifTrue: [ classes add: 'required' ].
>> (self hasError: aDescription)
>> ifTrue: [ classes add: 'error' ].
>> ^ classes reduce: [ :a :b | a , ' ' , b ]
>>
>> Cheers,
>> Lukas
>>
>>
> If looks like you have put a string into the cssClasses property,  
> where
> an Array/Collection of strings is expected.
>
> Keith
>
>
> _______________________________________________
> SmallWiki, Magritte, Pier and Related Tools ...
> https://www.iam.unibe.ch/mailman/listinfo/smallwiki
>


_______________________________________________
SmallWiki, Magritte, Pier and Related Tools ...
https://www.iam.unibe.ch/mailman/listinfo/smallwiki