Skip to content

Instantly share code, notes, and snippets.

@Sciss
Created February 2, 2018 16:13
Show Gist options
  • Save Sciss/3139e5c0008dfc75f60846bc069e86d5 to your computer and use it in GitHub Desktop.
Save Sciss/3139e5c0008dfc75f60846bc069e86d5 to your computer and use it in GitHub Desktop.
def modUp[A](a: A, b: A)(implicit num: Integral[A]): A = {
import num._
val am = a - one
val z = if (gteq(am, zero)) am % b else {
val c = -am % b
if (c == zero) zero else b - c
}
z + one
}
Seq.tabulate(10)(i => modUp(i, 4)) // List(4, 1, 2, 3, 4, 1, 2, 3, 4, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment