need a pagination function in clojure
- User
need a pagination function in clojure
Assistant
Sure, here's a simple pagination function in Clojure:
(defn paginate [coll page-size page-number] (let [start-index (* (dec page-number) page-size) end-index (+ start-index page-size)] (if (>= end-index (count coll)) (subvec coll start-index) (subvec coll start-index end-index))))This function takes a collection, a page size and a page number as input and returns a sub-vector of the collection, starting from the start-index and ending at the end-index, where the start-index and end-index are calculated based on the page size and page number.
If the end-index is greater than the count of the collection, it will return a sub-vector starting from the start-index to the end of the collection.
You can use this function on any sequence, not just vectors, as long as the sequence implements the subvec function.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.