Skip to content

Instantly share code, notes, and snippets.

@suls
Last active January 17, 2016 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suls/4feb7669d7c928050719 to your computer and use it in GitHub Desktop.
Save suls/4feb7669d7c928050719 to your computer and use it in GitHub Desktop.
pagination
object Pagination {
def pagedRequest[F[_], A] (
f: Offset => F[Page[A]],
offset: Offset
) : Process[F, A] = {
Process
.eval(f(offset))
.flatMap { response: Page[A] =>
Process.emitAll(response.results) ++
response.offset.map { o =>
pagedRequest(f, Option(o))
}.getOrElse(Process.empty[F, A])
}
}
}
class Test extends Specification with ScalaCheck {
implicit val IdIsCatchable : Catchable[Id] = new Catchable[Id] {
override def attempt[A](f: Id.Id[A]): Id.Id[\/[Throwable, A]] =
\/-(f)
override def fail[A](err: Throwable): Id.Id[A] = throw err
}
"pagination is flattening" >>
prop { (is: List[List[Int]]) =>
val f: (Offset) => Page[Int] =
(o:Offset) => {
val length = is.size
val fs = is.zipWithIndex.map { case (i: List[Int], idx: Int) =>
idx -> Page(i, if (idx < length-1) Some(idx+1) else None)
}.toMap
o.map(fs(_)).get
}
is.flatten must_== pagedRequest[Id, Int](f, Some(0)).runLog
}.setGen(Gen.nonEmptyListOf(Gen.listOf(Gen.posNum[Int])))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment