such.ns-state

Manipulate, in a stateful way, a key-value store that belongs to a particular namespace.

Throughout this documentation, f is any callable. It must not have side effects. k is a value (of any type) that indexes the key-value store.

Values in the store can be treated as single entities or as stacks.

All state-changing operations end in `“!”. They are atomic. Except where explicitly noted, their return value is undefined.

alter!

(alter! k f & args)

Replace the value of the store at k with the value of (apply f <current-val> args).

(nss/alter! :counter + 2)

count

(count k)

The value of the store at k must be a stack. Returns the number of elements. A never-created (or destroyed) stack has zero elements.

dissoc!

(dissoc! & ks)(dissoc!)

In the no-argument versions, delete all keys from the store. In the N-argument version, delete each of the keys.

empty?

(empty? k)

The value of the store at k must be a stack. Returns true iff the stack has no elements. A never-created (or destroyed) stack has zero elements.

flattened-history

(flattened-history k)

The value of the store at k must be a stack. Each element must be a sequential collection. The result is a vector of flatten applied to the history.

 (nss/push! :s [1 2)
 (nss/push! :s [])
 (nss/push! :s [3])
 (nss/flattened-history :s) => [1 2 3]

get

(get k)(get k default)

Return the value of the store at k or the default if there is no value. If no default value, return nil.

history

(history k)

The value of the store at k must be a stack. The elements are returned in the order they were pushed. (Thus, top is the final element.) The return value is specifically a vector.

 (nss/push! :s 1)
 (nss/push! :s 2)
 (nss/history :s) => [1 2]

pop!

(pop! k)

Change the stack at k to remove its top. Throws an Exception if the stack is empty or does not exist.

push!

(push! k v)

Change the stack at k to have v as its top element. The stack need not be created before the first push.

set!

(set! k v)

Replace the value of the store at k with v.

(nss/set! :counter 0)

top

(top k)

Returns the element of the stack at k that was most recently pushed. Throws an Exception if the stack is empty or does not exist.

 (nss/push! :s 1)
 (nss/push! :s 2)
 (nss/pop! :s)
 (nss/top :s) => 1