Created
June 9, 2016 03:20
-
-
Save MasseGuillaume/debc570253c9206777db1405602d4fc2 to your computer and use it in GitHub Desktop.
gooooooogle.scala
This file contains 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
case class Paginator(previous: Option[Int], selections: List[Int], current: Int, next: Option[Int]) | |
def pagination(current: Int, total: Int, window: Int = 10): Paginator = { | |
val prev = | |
if(current == 1) None | |
else Some(current -1) | |
val next = | |
if(current == total) None | |
else Some(current + 1) | |
val delta = (window - 1) / 2 | |
val (start, end) = | |
if(current + delta <= total) { | |
if(current - delta >= 1) (current - delta, current + delta) | |
else (1, window + 1) | |
} else (total - window, total) | |
val sels = (start to end).toList | |
Paginator(prev, sels, current, next) | |
} | |
def renderP(paginator: Paginator): String = { | |
import paginator._ | |
val p = previous.map(_ => "<").getOrElse(" ") | |
val s = selections.map(s => if(s == current) s"*$s*" else s" $s " ).mkString("") | |
val n = next.map(_ => ">").getOrElse("") | |
s"$p $s $n" | |
} | |
List( | |
pagination( 1, 12), | |
pagination( 6, 12), | |
pagination( 7, 12), | |
pagination( 8, 12), | |
pagination(11, 12), | |
pagination(12, 12) | |
).map(renderP).mkString(System.lineSeparator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment