Skip to content

Instantly share code, notes, and snippets.

@torholm
Last active June 13, 2016 12:31
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 torholm/d8dcf8bd73330b6377b96bf52a0a156a to your computer and use it in GitHub Desktop.
Save torholm/d8dcf8bd73330b6377b96bf52a0a156a to your computer and use it in GitHub Desktop.
def paginate(min: Int, max: Int, selected: Int): String = {
require(selected <= max, "selected can't be bigger than max")
require(selected >= min, "selected can't be less than max")
require(max >= min, "max can't be bigger than min")
val winMax = Math.min(Math.max(min + 9, selected + 4), max)
val winMin = Math.max(Math.min(max - 8, selected - 5), min)
val prev = if (selected > min) "< " else ""
val next = if (selected < max) " >" else ""
val pages = (winMin to winMax).map {
case `selected` => "*" + selected + "*"
case x => x.toString
}.mkString(" " )
prev + pages + next
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment