Back
chunk (clj)
(source)function
(chunk b)
Examples
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
;; START CHUNK HACKS
;; These are hacks to get around the expansion of doseq>
;; Basically, inference isn't good enough to narrow a (t/Seqable x) to
;; an (IChunk x), because chunked-seq? needs to be (t/Pred (IChunk t/Any)).
#?@(:cljs [] :default [
cc/chunked-seq? [t/Any :-> t/Any]
cc/chunk-first
(t/All [x]
;should be IChunkedSeq :-> IChunk
[(t/Seqable x) :-> (clojure.lang.IChunk x)])
cc/chunk-rest
(t/All [x]
;should be IChunkRest :-> t/Seq
[(t/Seqable x) :-> (t/ASeq x)])
cc/chunk-buffer
(t/All [x]
[(t/U Integer Long) :-> (clojure.lang.ChunkBuffer x)])
cc/chunk
(t/All [x]
[(clojure.lang.ChunkBuffer x) :-> (clojure.lang.IChunk x)])
cc/chunk-cons
(t/All [x]
[(clojure.lang.IChunk x) (t/Seqable x) :-> (t/ASeq x)])
cc/chunk-append
(t/All [x]
[(clojure.lang.ChunkBuffer x) x :-> t/Any])
;;END CHUNK HACKS
])
ReactiveX/RxClojure
(ns rx.lang.clojure.chunk-test
(:require [rx.lang.clojure.chunk :as rx-chunk]
[rx.lang.clojure.core :as rx]
[rx.lang.clojure.future :as rx-future]
[rx.lang.clojure.blocking :as rx-blocking]
[clojure.test :refer [deftest testing is]]))
(deftest test-chunk
(let [n 20
chunk-size 10
factory (rx-future/future-generator*
future-call
(fn[o]
(doseq [i (range n)]
(Thread/sleep (rand-int 50))
(rx/on-next o (rx-future/future*
future-call
#(let [t (rand-int 500)]
(Thread/sleep t)
i))))))]
(is (= (range n)
(sort (rx-blocking/into []
(rx-chunk/chunk chunk-size {:debug true} factory)))))))
(deftest test-chunk-with-error
(testing "error from source is propagated"
(let [n 20
chunk-size 4
factory (rx-future/future-generator*
future-call
(fn [o]
(doseq [i (range n)]
(Thread/sleep (rand-int 50))
(rx/on-next o (rx-future/future*
future-call
#(let [t (rand-int 1000)]
(Thread/sleep t)
i))))
(throw (IllegalArgumentException. "hi"))))]
(is (thrown-with-msg? IllegalArgumentException #"hi"
(rx-blocking/into []
(rx-chunk/chunk chunk-size {:debug true} factory))))))
(testing "error from single observable is propagated"
(let [n 20
chunk-size 4
factory (rx-future/future-generator*
future-call
(fn [o]
(doseq [i (range n)]
(Thread/sleep (rand-int 50))
(rx/on-next o (rx-future/future*
future-call
#(let [t (rand-int 1000)]
(throw (IllegalArgumentException. "byebye"))
(Thread/sleep t)
i))))))]
(is (thrown? rx.exceptions.CompositeException
(rx-blocking/into []
(rx-chunk/chunk chunk-size
{:debug true
:delay-error? true }
factory)))))))
dco-dev/interval-tree
(ns com.dean.interval-tree.tree.interval-set
(:require [clojure.core.reducers :as r :refer [coll-fold]]
[clojure.set]
[com.dean.interval-tree.tree.interval :as interval]
[com.dean.interval-tree.tree.node :as node]
[com.dean.interval-tree.tree.order :as order]
[com.dean.interval-tree.tree.protocol :as proto]
[com.dean.interval-tree.tree.root]
[com.dean.interval-tree.tree.tree :as tree])
(:import [clojure.lang RT]
[com.dean.interval_tree.tree.protocol PExtensibleSet]
[com.dean.interval_tree.tree.root INodeCollection
IBalancedCollection
IOrderedCollection
IIntervalCollection]))
clojure.core.reducers.CollFold
(coll-fold [this n combinef reducef]
(with-interval-set this
(tree/node-chunked-fold n root combinef
(fn [acc node] (reducef acc (node/-k node)))))))
dco-dev/interval-tree
(ns com.dean.interval-tree.tree.ordered-set
(:require [clojure.core.reducers :as r :refer [coll-fold]]
[clojure.set]
[com.dean.interval-tree.tree.node :as node]
[com.dean.interval-tree.tree.order :as order]
[com.dean.interval-tree.tree.protocol :as proto]
[com.dean.interval-tree.tree.root]
[com.dean.interval-tree.tree.tree :as tree])
(:import [clojure.lang RT]
[com.dean.interval_tree.tree.protocol PExtensibleSet]
[com.dean.interval_tree.tree.root INodeCollection
IBalancedCollection
IOrderedCollection]))
clojure.core.reducers.CollFold
(coll-fold [this n combinef reducef]
(with-ordered-set this
(tree/node-chunked-fold n root combinef
(fn [acc node] (reducef acc (node/-k node)))))))
dco-dev/interval-tree
(ns com.dean.interval-tree.ordered-set-test
(:require [clojure.core.reducers :as r]
[clojure.math.combinatorics :as combo]
[clojure.set :as set]
[clojure.test :refer :all]
[com.dean.interval-tree.core :refer :all]))
(deftest foldable-reducible-collection-check
(doseq [size [1 10 100 1000 10000 100000 250000 500000 1000000]
chunk [1 10 100 1000]]
(let [data (shuffle (range size))
sum (reduce + data)
this (ordered-set data)]
(is (= sum (r/fold chunk + + this)))
(is (= sum (reduce + this))))))