Skip to content

Instantly share code, notes, and snippets.

@yantonov
Last active December 26, 2015 14:07
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 yantonov/5209876 to your computer and use it in GitHub Desktop.
Save yantonov/5209876 to your computer and use it in GitHub Desktop.
How to dechunk(unchunk) sequence in clojure. Chunked and dechunked sequence realization count example.
(ns seq-test)
(ns seq-test
(:require [clojure.test :as test]))
;; clarification 1
;; be carefull this functions guarantees one-by-one fetching for client of the dechunk function,
;; but NOT guarantees that source seq will not be evaluated forward by chunks
;; given tests demonstrates it
;; clarirication 2
;; given tests actually check how map function works (check source of map function before)
(defn dechunk [s]
(lazy-seq
(when-let [[x] (seq s)]
(cons x (dechunk (rest s))))))
(defn get-realization-count [seq-length drop-count chunked?]
(let [realization-agent (agent 0)]
(first (take 1
(drop drop-count
(seq (map #(do (send-off realization-agent inc)
%)
(if chunked?
(range seq-length)
(dechunk (range seq-length)))))
)
))
(await realization-agent)
@realization-agent))
(test/deftest chunked-seq-realized-count
(test/testing "number of realized items for chunked seq"
(test/are [seq-length drop-count expected-realized-count]
(= expected-realized-count
(get-realization-count seq-length drop-count true))
;; precaclulation size = 32 (up to 32 elements realized even if one requested)
1 0 1
2 0 2
4 2 4
8 3 8
15 7 15
32 5 32
64 1 32 ; precalculate 32
64 32 64
64 33 64; precalculate 64
)))
(test/deftest dechunked-seq-realized-count
(test/testing "number of realized items for dechunked seq"
(test/are [seq-length drop-count expected-realized-count]
(= expected-realized-count
(get-realization-count seq-length drop-count false))
;; only needed element realized
1 0 1
2 0 1
4 2 3
8 3 4
15 7 8
32 5 6
64 1 2
64 32 33
64 33 34)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment