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
Related
larzpaginateSlice any list into pages with has_next/has_prev bookkeeping→Retry with Exponential Backoff in LarzscriptRetry a flaky call a few times, waiting longer between each attempt, instead of failing on the first hiccup or hammering a struggling service.→Memoize a Function in LarzscriptCache a function's results so calling it twice with the same input only does the real work once.→Implement a State Machine in LarzscriptDeclare states and transitions, fire events, and let the machine reject anything that isn't a legal move - a document workflow (draft -> review -> published) in a few lines.→