"cdrick" == cdrick cdrick65@gmail.com writes:
cdrick> Hi Stan, cdrick> 2008/7/21 stan shepherd squeak414@free.fr:
Hi, I have the following method, that code critics flags as long:
testMaleMeiosis2 | testSet mCount pCount mTotal pTotal | pTotal := 0. mTotal := 0. Forecaster testMale meiose do: [:strand | testSet := strand testRun. mCount := testSet maternalCount. pCount := testSet paternalCount. mTotal := mTotal + mCount. pTotal := pTotal + pCount]. self should: [mTotal = 100 and: [pTotal = 100]]
I'm trying to shorten it, including various attempts with inject: into: , but with no success. Any tips please?
cdrick> So somethink like that should work:
cdrick> testMaleMeiosis2 cdrick> | resultArray tesSet | cdrick> testSet := strand testRun. cdrick> resultArray := Forecaster testMale meiose cdrick> inject: #(0 0) cdrick> into: [:array :val | cdrick> array at: 1 put: (array at: 1) + testSet maternalCount. cdrick> array at: 2 put: (array at: 2) + testSet paternalCount. cdrick> array]
cdrick> self should: [ resultArray = #(100 100)].
This looks even uglier. How about first gathering the testSets, then getting what you need from those:
| 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.
Unless you're talking about 5000 elements in testSets, this is likely in the same ballpark for speed, and that's probably dwarfed by the cost of #testRun anyway.
Sometimes an obsession with #inject:into: as a shiny object leads you down a dark path. Especially with sums. Consider #sum first.