I am trying to learn Shapeless. I have created a very simple case class:
case class Holding(ticker: String, assetClass: String, units: Double, mktValue: Double)
I would like to be able to group on any arbitrary field, i.e. ticker or assetClass on a list of Holdings and sum on units and mktValue.
This works:
val map = list.groupBy(lens[Holding] >> 'assetClass)
So, does this:
val mapped = Map( 'assetClass -> (lens[Holding] >> 'assetClass), 'ticker -> (lens[Holding] >> 'ticker))
def makeLens(sym: Symbol) = {
mapped.get(sym).get
}
val map = list.groupBy(makeLens('assetClass).get)
Creating a map to hold the lenses does not seem to follow the spirit of the 'scrap your bolierplate' approach. Is there any way to do this:
val sym = 'assetClass
val map = list.groupBy(lens[Holding] >> sym)
I need to be able to define a method as generically as possible that takes an arbitrary symbol and groups the list. Is that possible?