This repository was archived by the owner on Mar 12, 2026. It is now read-only.
Refactor Household.scala to pure FP with opaque types#103
Merged
Conversation
Replace var counter + nested for-loops + builder with flatMap/take/ zipWithIndex. Extract sampleHousehold and sampleEducationAndSkill as pure functions. Remove unused nFirms parameter.
- Extract buildDistressedSet and sectorMobilityRate as named functions - Replace isInstanceOf/asInstanceOf with pattern match in mobility calc - Merge StepTotals directly into computeAggregates — single Aggregates construction instead of compute + copy overwriting 16 fields - Keep public computeAggregates overload for BankUpdateStep and tests
Extract MonthlyFlows, computeMonthlyFlows, resolveBankruptcy, resolveSurvival from 135-line processHousehold. Make consumer credit match exhaustive (no catch-all) with named eligibility booleans.
Replace PLN(Math.max(0.0, x.toDouble)) with x.max(PLN.Zero), use PLN * Ratio / PLN * Rate directly, Rate + Rate for consumer rate, PLN < PLN for bankruptcy threshold.
PLN > PLN for savings vs retrainingCost, pass retrainingCost directly (already PLN), Ratio * Ratio for success probability.
Type locals as PLN directly, use PLN.max for rent floor, PLN * Double for wage, eliminate redundant PLN(...) wrapping in State construction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sampleHouseholdandsampleEducationAndSkill, remove unusednFirmsparambuildDistressedSetandsectorMobilityRate, eliminateisInstanceOf/asInstanceOf, mergeStepTotalsdirectly intocomputeAggregates(no more compute-then-overwrite 16 fields)computeMonthlyFlows+resolveBankruptcy+resolveSurvivalvia typedMonthlyFlowsintermediate.toDoublewithPLN.max,PLN * Ratio,Rate + Rate,PLN > PLNthroughout3 files, -44 lines net.
Test plan
sbt compilepassessbt test— all 1245 tests pass