Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dnaumenko/2bfeedeea09e2c6dc702cc0fded7b004 to your computer and use it in GitHub Desktop.
Save dnaumenko/2bfeedeea09e2c6dc702cc0fded7b004 to your computer and use it in GitHub Desktop.
Shift util for Cats IO
// from https://gitter.im/typelevel/cats-effect?at=5cdae78a5a887e1cd9e8a334
trait Shifter[F[_]] {
def shiftFair[A](fa: F[A]): F[A]
}
def fairShifter(n: Int): F[Shifter[F]] =
Ref.of(0).map {
new Shifter[F] {
def shiftFair(fa: F[A]): F[A] = ref.modify(i => (i+1, i%n == 0).ifM(shift >> fa, fa)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment