u/gemelenin r/scala • Recommends
On “Data Structure in Scala for a Recursive data type”
I haven't use them personally, so only speaking from material I've seen: you may want to use recursive types (or schemas, or fixed point types) which are designed to cover the case you have.
There is a recommended lib for that, Matryoshka.
And for great intro, I'd recommend these presentations:
[Pure Functional Database Programming with Fixpoint Types—Rob Norris
]
[Jean Remi Desjardins: A Gentle Introduction to Recursion Schemes - λC 2016
]