Last active
January 17, 2016 05:02
-
-
Save suls/4feb7669d7c928050719 to your computer and use it in GitHub Desktop.
pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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