A set of tuple types, a sequential persistent collection efficient for small vectors.

Taken and adapted from clj-tuple by Zachary Tellman, MIT license.

This namespace provides number of tuple types for small vectors. Tuples have a constant time access to their items and efficient memory use. There is no structural sharing between tuples, which if efficient only for small vectors.

Current implementation switches to the bvt-vector when number of items exceeds 6.

Tuples have support for transients, folding, efficient reversion and flipping, sectioning, slicing and catenation. Tuples with 2 items also implement a host MapEntry interface, which makes them an ideal type for map [key value] entries.

Besides tuples, Dunaj offers following persistent vector types:

  • BVT vectors for efficient representation of medium to large vectors

  • RRB-Tree vectors for efficient catenation or insertion/removal of items in the middle of a vector.

  • primitive vectors for efficient storage of host primitive data types.

Just like all vector types, tuples add/remove items to/from the rear of the collection.

Primary

In this section: empty-tuple tuple tuple-factory

tuple

Available since version 1.0 (view source)

Usage:
  • (tuple)

  • (tuple x)

  • (tuple x y)

  • (tuple x y z)

  • (tuple x y z w)

  • (tuple x y z w u)

  • (tuple x y z w u v)

  • (tuple x y z w u v & rst)

Type signatures:
  • () → IPersistentVector

  • (Any) → IPersistentVector

  • (Any ⨯ Any) → IPersistentVector

  • (Any ⨯ Any ⨯ Any) → IPersistentVector

  • (Any ⨯ Any ⨯ Any ⨯ Any) → IPersistentVector

  • (Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → IPersistentVector

  • (Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → IPersistentVector

  • (Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any ⨯ Any) → IPersistentVector

Returns a tuple which behaves like a vector, but is highly efficient for index lookups, hash calculations, equality checks, and reduction. If there are more than six items, returns a normal vector.

1
2
3
4
5
(tuple 1 2 3 4 5)
;;=> [1 2 3 4 5]

(flip (tuple 1 2 3 4 5))
;;=> [5 4 3 2 1]

tuple-factory

Available since version 1.0 (view source)

not referred automatically

VAR of type ICollectionFactory

A tuple factory instance. Currently there are no options.

This factory implements ICollectionFactory factory protocol.

New instances of tuple can be created with collection and ->collection.

See also: tuple

Pairs

In this section: key pair val

key

Available since version 1.0 (view source)

Usage:
  • (key pair)

Type signature:
  • (java.util.Map$Entry) → Any

Returns key of a given pair tuple. More efficient than first. Also works with host pairs, e.g. map entry in JVM.

1
2
(seq (map key {:a 1 :b 2}))
;;=> (:b :a)

pair

Available since version 1.0 (view source)

Usage:
  • (pair x y)

Type signature:
  • (Any ⨯ Any) → Tuple2

Returns a pair tuple containing x and y.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;; pair is faster than destructuring but more verbose
(defn foo [[k v]]
    (+ k v))
;;=> #'foo.bar/foo

(defn foo-pair [p]
  (let [k (key p)
        v (val p)]
    (+ k v)))
;;=> #'foo.bar/foo-pair

(time (dotimes [x 1000000] (foo [x x])))
;; Elapsed time: 400.177109 msecs
;;=> nil

(time (dotimes [x 1000000] (foo (pair x x))))
;; Elapsed time: 300.855405 msecs
;;=> nil

(time (dotimes [x 1000000] (foo-pair (pair x x))))
;; Elapsed time: 264.42369 msecs
;;=> nil

val

Available since version 1.0 (view source)

Usage:
  • (val pair)

Type signature:
  • (java.util.Map$Entry) → Any

Returns value of a given pair tuple. More efficient than second. Also works with host pairs, e.g. map entry in JVM.

1
2
(seq (map val {:a 1 :b 2}))
;;=> (2 1)