[Newbies] How to shorten a method (using inject: into: ?)
cdrick65 at gmail.com
Tue Jul 22 16:03:53 UTC 2008
> Zulq Alam-2 wrote:
>> Using #inject:into and Point objects to store the totals:
>> | totals |
>> totals := Forecaster testMale meiose inject: 0 at 0 into:
>> [:subtotals :strand |
>> subtotals + (strand maternalCount @ strand maternalCount)].
>> self should: [totals = 100 at 100]
>> The point notation makes it look very clean (to me).
> I agree it's very tidy looking.
> Is there a style issue with introducing an object type which is not part of
> the problem? Is point often used like this- it's being treated as a
> shorthand form of an array of 2 elements which has some handy methods, is it
to me same problem as earlier with the array even if a bit more
readable. Actually it's more elgant and shorter thant the array
solution but the intention is even more hidden (to me ;) ).
That's cool we can do that but not to use except for optimization (I'm
not sure though this will be quicker).
Always choose the most readable (before optimizing) and to me, the
following is much more readable:
mTotal := (testSets collect: [:each | each maternalCount]) sum.
pTotal := (testSets collect: [:each | each paternalCount]) sum.
my 2 cents
More information about the Beginners