Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Invariant[Numeric] #3220

Closed
kubukoz opened this issue Dec 19, 2019 · 2 comments · Fixed by #3773
Closed

Add Invariant[Numeric] #3220

kubukoz opened this issue Dec 19, 2019 · 2 comments · Fixed by #3773
Milestone

Comments

@kubukoz
Copy link
Member

kubukoz commented Dec 19, 2019

Here's a ready instance one can use:

implicit val invariantNumeric: Invariant[Numeric] = new Invariant[Numeric] {

  def imap[A, B](fa: Numeric[A])(f: A => B)(g: B => A): Numeric[B] = new Numeric[B] {
    def compare(x: B, y: B): Int = fa.compare(g(x), g(y))
    def plus(x: B, y: B): B = f(fa.plus(g(x), g(y)))
    def minus(x: B, y: B): B = f(fa.minus(g(x), g(y)))
    def times(x: B, y: B): B = f(fa.times(g(x), g(y)))
    def negate(x: B): B = f(fa.negate(g(x)))
    def fromInt(x: Int): B = f(fa.fromInt(x))
    def toInt(x: B): Int = fa.toInt(g(x))
    def toLong(x: B): Long = fa.toLong(g(x))
    def toFloat(x: B): Float = fa.toFloat(g(x))
    def toDouble(x: B): Double = fa.toDouble(g(x))
  }
}

Feel free to grab it if you have time before I do.

@kubukoz
Copy link
Member Author

kubukoz commented Dec 21, 2019

I'll do it now!

@tmccarthy
Copy link
Contributor

Would there be any issues doing this for scala.math.Integral and scala.math.Fractional as well? These inherit from Numeric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants