Loom.PNCounter
Positive-negative counters
PNCounters are counters that are capable of incrementing and decrementing. They are useful for like counters, where a user may like and then unlike something in succession.
They are not delta-CRDT’s, as they are rather lightweight in general. A delta- CRDT implementation would just return the latest value for an actor.
They do, however, implement the CRDT protocol, and can be coposed into larger CRDT datastructures.
Summary
dec(c, actor, int \\ 1) | Decrement a counter on behalf of the actor |
inc(c, actor, int \\ 1) | Increment a counter on behalf of the actor |
join(pncounter, pncounter) | Joins 2 counters together |
new() | Instantiate a new PNCounter. Starts at 0 |
new(opts) | Instantiate a new PNCounter with previous values |
value(pncounter) | Gets a natural value for the counter |
Functions
Specs:
Decrement a counter on behalf of the actor.
iex> alias Loom.PNCounter, as: Counter
iex> Counter.new
...> |> Counter.dec(:a, 1)
...> |> Counter.dec(:a, 29)
...> |> Counter.value
-30
iex> Counter.new
...> |> Counter.inc(:a, 1)
...> |> Counter.dec(:a, 1)
...> |> Counter.value
0
Specs:
Increment a counter on behalf of the actor.
iex> alias Loom.PNCounter, as: Counter
iex> Counter.new
...> |> Counter.inc(:a, 1)
...> |> Counter.inc(:a, 29)
...> |> Counter.value
30
Specs:
Joins 2 counters together.
iex> alias Loom.PNCounter, as: Counter
iex> ctr1 = Counter.new |> Counter.inc(:a) |> Counter.dec(:a, 10)
iex> ctr2 = Counter.new |> Counter.dec(:b) |> Counter.inc(:b, 5)
iex> Counter.join(ctr1,ctr2) |> Counter.value
-5
Specs:
- new :: t
Instantiate a new PNCounter. Starts at 0.
iex> Loom.PNCounter.new |> Loom.PNCounter.value
0
Specs:
Instantiate a new PNCounter with previous values.
iex> alias Loom.PNCounter, as: Counter
iex> values = [a: {10,0}, b: {5,5}, c: {37,5}]
iex> Counter.new(values: values) |> Counter.value
42