App patterns · real, tested code

Paginate a List in Larzscript

Slice a list into pages with the has_next/has_prev/1-based-index bookkeeping every list-showing page needs, without hand-rolling the off-by-one math again.

paginate.paginate(items, page, per_page) clamps an out-of-range page number back into bounds instead of returning an empty page, and hands back everything a pager UI needs in one dict.

larzscript pkg install paginate

Code

import "paginate" as paginate

let items = list(range(1, 24))
let p = paginate.paginate(items, 2, 10)
print(p["items"])
print(p["page"], p["pages"], p["has_next"], p["has_prev"])
Output
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
2 3 true true