Last active
June 7, 2016 19:54
-
-
Save AlecZorab/9fad94ccb1759da60e34873a6a44b144 to your computer and use it in GitHub Desktop.
Deep Modification Magic
This file contains 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
import shapeless._ | |
val smoothTrades: Map[String, K2OverHttp.Trade] = { | |
object continuous extends poly.->((p: OISCompoundingPeriodCommon) => (p match { | |
case c: OISCompoundingPeriod => c.copy(roundingDecimal = None) | |
case c: OISAveragingPeriod => c.copy(roundingDecimal = None) | |
case c: OISAveragingExtPeriod => c.copy(roundingDecimal = None) | |
case c: OISAveragingCustomSchedulePeriod => c.copy(roundingDecimal = None) | |
case c: OISSpreadCompoundingPeriod => c.copy(roundingDecimal = None) | |
case c: OISStraightCompoundingPeriod => c.copy(roundingDecimal = None) | |
}): OISCompoundingPeriodCommon) | |
preCloseTrades.map { case (k, v) => k -> everywhere(continuous)(v) } | |
} |
This file contains 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
import monocle._ | |
import monocle.generic.all._ | |
import monocle.macros.{GenLens, Lenses, PLenses} | |
import monocle.function.all._ | |
import monocle.std.list._ | |
import monocle.std.map._ | |
import monocle.std.tuple2._ | |
import monocle.Traversal | |
val smoothTrades: Map[String, K2OverHttp.Trade] = { | |
def lensFromOptionals[S, A](optionals: List[Optional[S, A]]): Lens[S, A] = | |
Lens[S, A](s => optionals.map(_.getOption(s)).collectFirst { case Some(a) => a }.getOrElse(sys.error("incomplete")))( | |
a => s => optionals.map(_.setOption(a)(s)).collectFirst { case Some(s2) => s2 }.getOrElse(sys.error("incomplete")) | |
) | |
val _oisComp = GenLens[OISCompoundingPeriod](_.roundingDecimal) | |
val _oisAvg = GenLens[OISAveragingPeriod](_.roundingDecimal) | |
val _oisAvgExt = GenLens[OISAveragingExtPeriod](_.roundingDecimal) | |
val _oisAvgCust = GenLens[OISAveragingCustomSchedulePeriod](_.roundingDecimal) | |
val _oisSprComp = GenLens[OISSpreadCompoundingPeriod](_.roundingDecimal) | |
val _oisStrComp = GenLens[OISStraightCompoundingPeriod](_.roundingDecimal) | |
val generic = Generic[OISCompoundingPeriodCommon] | |
type Co = generic.Repr | |
val pris = Prism[OISCompoundingPeriodCommon, Co](_ $ generic.to $ Option.apply)(generic.from) | |
val pOisComp = coProductPrism[Co, OISCompoundingPeriod].composeLens(_oisComp) | |
val pOisAvg = coProductPrism[Co, OISAveragingPeriod].composeLens(_oisAvg) | |
val pOisAvgExt = coProductPrism[Co, OISAveragingExtPeriod].composeLens(_oisAvgExt) | |
val pOisAvgCust = coProductPrism[Co, OISAveragingCustomSchedulePeriod].composeLens(_oisAvgCust) | |
val pOisSprComp = coProductPrism[Co, OISSpreadCompoundingPeriod].composeLens(_oisSprComp) | |
val pOisStrComp = coProductPrism[Co, OISStraightCompoundingPeriod].composeLens(_oisStrComp) | |
val lens: Lens[OISCompoundingPeriodCommon, Option[Int]] = | |
lensFromOptionals(List(pOisComp, pOisAvg, pOisAvgExt, pOisAvgCust, pOisSprComp, pOisStrComp).map(pris composeOptional _)) | |
val simpleOisLegPeriods = GenLens[SimpleOISLeg](_.schedule) | |
val pSimpleOISLeg = Prism[Leg, SimpleOISLeg] { | |
case s: SimpleOISLeg => Some(s) | |
case _ => None | |
} { x => x } | |
val simpleSwapLegs = Lens[SimpleSwap, List[(PayRec, Leg)]](_.legs)(l => s => l.foldLeft(s) { | |
case (s, (Pay, l)) => s.copy(payLeg = l) | |
case (s, (Receive, l)) => s.copy(recLeg = l) | |
}) | |
val pTradeSwap = coProductPrism[K2OverHttp.Trade, SimpleSwap] | |
val l = Lens.id[Map[String, K2OverHttp.Trade]] composeTraversal | |
each composePrism | |
pTradeSwap composeLens | |
simpleSwapLegs composeTraversal | |
each composeLens | |
second composePrism | |
pSimpleOISLeg composeLens | |
simpleOisLegPeriods composeTraversal | |
each composeLens | |
lens | |
// bonus points for symbols! | |
// val l2 = Lens.id[Map[String, K2OverHttp.Trade]] ^|->> | |
// each ^<-? | |
// pTradeSwap ^|-> | |
// simpleSwapLegs ^|->> | |
// each ^|-> | |
// second ^<-? | |
// pSimpleOISLeg ^|-> | |
// simpleOisLegPeriods ^|->> | |
// each ^|-> | |
// lens | |
l.modify(_ => None)(preCloseTrades) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment