[Newbies] Re: How to shorten a method (using inject: into: ?)

Randal L. Schwartz merlyn at stonehenge.com
Mon Jul 21 15:45:05 UTC 2008


>>>>> "Zulq" == Zulq Alam <me at zulq.net> writes:

Zulq> Hi Stan,
Zulq> How about using #detectSum:?

Zulq> testMaleMeiosis2		
Zulq> 	pTotal := Forecaster testMale meiose
Zulq> 		detectSum: [:strand | strand testRun paternalCount].
Zulq> 	mTotal := Forecaster testMale meiose
Zulq> 		detectSum: [:strand | strand testRun maternalCount].	
Zulq> 	self should: [mTotal = 100 and: [pTotal = 100]]		

Huh?  Why the heck is that called #detectSum: instead of just #sum:?
I was trying to find that.  That'd shorten my previous one even more:

testRuns := Forecaster testMale meiose collect: [:e | e testRun].
pTotal := testRuns detectSum: [:e | e paternalCount].
mTotal := testRuns detectSum: [:e | e maternalCount].

No redundant calculations, although we're building an array in the middle that
will be a throwaway.

/me makes a note to look under #detectFoo: next time

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


More information about the Beginners mailing list