Skip to content
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
speedcom merged 6 commits intomainfrom
refactor/household-initialize-pure-fp
Mar 10, 2026
Merged

Refactor Household.scala to pure FP with opaque types#103
speedcom merged 6 commits intomainfrom
refactor/household-initialize-pure-fp

Conversation

@speedcom
Copy link
Contributor

@speedcom speedcom commented Mar 10, 2026

Summary

  • initialize: replace var counter + nested for-loops with flatMap/take/zipWithIndex, extract sampleHousehold and sampleEducationAndSkill, remove unused nFirms param
  • step: extract buildDistressedSet and sectorMobilityRate, eliminate isInstanceOf/asInstanceOf, merge StepTotals directly into computeAggregates (no more compute-then-overwrite 16 fields)
  • processHousehold: decompose 135-line method into computeMonthlyFlows + resolveBankruptcy + resolveSurvival via typed MonthlyFlows intermediate
  • processConsumerCredit: exhaustive match (no catch-all), named eligibility booleans
  • opaque types: replace redundant .toDouble with PLN.max, PLN * Ratio, Rate + Rate, PLN > PLN throughout

3 files, -44 lines net.

Test plan

  • sbt compile passes
  • sbt test — all 1245 tests pass

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.
@speedcom speedcom merged commit ac613e2 into main Mar 10, 2026
2 checks passed
@speedcom speedcom deleted the refactor/household-initialize-pure-fp branch March 10, 2026 23:13
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant