Liking cljdoc? Tell your friends :D

Persistent Deque

Deque(double-ended queue) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front or back.

data.deque is a persistent deque for Clojure(Script). It's implementation is based on slightly modified version of finger tree. data.deque gives O(1) access to both ends and amortized O(1) for immutable insertion/deletion.

Why Finger Tree?

Bankers Deque is also a purely functional data structure that guarantee amortized constant time but performs worse due to reverse operation. Real-Time Deque eliminates amortization by "Lazy Rebuilding" technique, but it also has some overhead due to its laziness. Finger Tree provides a balanced framework for building deque in terms of both time and space complexity.

Differences from core.data.finger-tree ?

  • ClojureScript support
  • Better performance
  • No unnecessary features for deque
    • Trees are not concatable / splittable
    • Measurements only being used for counting

Example

(require '[data.deque :refer [deque]])

;; create an empty deque with `(deque)` or
(def dl (deque 5 4 3 2 1))

(peek-first dl)
=> 1

(peek-last dl)
=> 5

(-> dl
    (add-first 0)
    (add-last 6)
    seq)
=> (0 1 2 3 4 5 6)

(-> dl
    (remove-first)
    (remove-last)
    seq)
=> (1 2 3)

Benchmark

implementationsmallmediumlargerate
java.util.ArrayDeque (base)37.94ms271.44ms3.47sx1
clojure.data.finger-tree196.50ms1.23s12.88sx3.86
data.deque (JVM)158.50ms595.49ms6.13sx1.89
data.deque (Browser)152ms807ms7.47sx2.31

Reference

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close