stripe / rainier

Bayesian inference in Scala.
https://rainier.fit
Apache License 2.0
433 stars 51 forks source link

add contramap implementation to ToReal #377

Closed sritchie closed 5 years ago

sritchie commented 5 years ago

This PR adds a contramap implementation; this lets me get instances of ToReal[B] for types where I can extract something numeric, say, but for which I can't actually define a Numeric[B] instance.

I'm using this with algebird's AveragedValue. I'd like to be able to have the ToReal implicit handle the conversion rather than explicitly taking a function from T => Double, in this case. With this PR I can write:

  implicit val avToReal: ToReal[AveragedValue] =
    implicitly[ToReal[Double]].contramap(_.value)
codecov-io commented 5 years ago

Codecov Report

Merging #377 into develop will increase coverage by 0.03%. The diff coverage is 0%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #377      +/-   ##
===========================================
+ Coverage    45.84%   45.87%   +0.03%     
===========================================
  Files           86       86              
  Lines         3067     3069       +2     
  Branches       141      135       -6     
===========================================
+ Hits          1406     1408       +2     
  Misses        1661     1661
Impacted Files Coverage Δ
...main/scala/com/stripe/rainier/compute/ToReal.scala 73.33% <0%> (-11.29%) :arrow_down:
...ain/scala/com/stripe/rainier/compute/RealOps.scala 78.81% <0%> (+1.69%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 967bc86...2795b2e. Read the comment docs.

avibryant commented 5 years ago

LGTM