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

stan shepherd squeak414 at free.fr
Mon Jul 21 15:57:43 UTC 2008


Quoting cdrick <cdrick65 at gmail.com>:
> 
> testMaleMeiosis2
> | resultArray tesSet |
> testSet := strand testRun.
> resultArray := Forecaster testMale meiose
> 	inject: #(0 0)
>         into: [:array :val |
>              array at: 1 put: (array at: 1) + testSet maternalCount.
> 	     array at: 2 put: (array at: 2) + testSet paternalCount.
> 	     array]
> 
>> self should: [ resultArray = #(100 100)].
> 

thanks Cédrick. Yes, that's what I was trying to do earlier, but without
success. Perhaps a missing explicit return somewhere.

Quoting "Randal L. Schwartz" <merlyn at stonehenge.com>:
> 
> | testSets mTotal pTotal |
> 
> testSets := (Forecaster testMale meiose) collect: [:strand | strand
> testRun
> ].
> mTotal := (testSets collect: [:each | each maternalCount]) sum.
> pTotal := (testSets collect: [:each | each paternalCount]) sum.
> 

Randal, much more succinct than my effort, thanks.

Quoting Zulq Alam <me at zulq.net>:

> Hi Stan,
> 
> How about using #detectSum:?
>
>testMaleMeiosis2                
>        pTotal := Forecaster testMale meiose
>                detectSum: [:strand | strand testRun paternalCount].
>        mTotal := Forecaster testMale meiose
>                detectSum: [:strand | strand testRun maternalCount].        
>       self should: [mTotal = 100 and: [pTotal = 100]]  
> 
> 

detectSum:  neatens things up nicely, Zulq. I dont think I can call meiose
twice in this case, because it returns different strands each time,
intentionally. It randomly swaps genes between strands as in genetic
recombination, and that's what I'm trying to test.

So until the next cunning suggestion, at the moment it looks like:

testMaleMeiosis3
	| testSets mTotal pTotal |
	testSets := Forecaster testMale meiose
				collect: [:strand | strand testRun].
	mTotal := testSets
				detectSum: [:each | each maternalCount].
	pTotal := testSets
				detectSum: [:each | each paternalCount].
	self
		should: [mTotal = 100
				and: [pTotal = 100]] 

I appreciate all your replies; very illuminating.

...Stan
-- 
View this message in context: http://www.nabble.com/How-to-shorten-a-method-%28using-inject%3A-into%3A--%29-tp18567292p18571731.html
Sent from the Squeak - Beginners mailing list archive at Nabble.com.



More information about the Beginners mailing list