Maps
Map[K, V]
liftable add[K, V](map: strict Map[K, V], key: strict K, value: strict V): Map[K, V]
Associate a key
to a value
in this map, overriding the previous mapping for key
if existent.
Usage example:
in x: Events[(String, Int)]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
---
0: map = —
1: x = (A, 2)
1: map = A → 2
4: x = (B, 4)
4: map = A → 2, B → 4
7: x = (B, 8)
7: map = A → 2, B → 8
collectCount[T](events: Events[T]): Events[Map[T, Int]]
Counts the occurrences of each value in events
and returns a mapping of each value to its count.
Usage example:
in x: Events[String]
def map = Map.collectCount(x)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
---
0: map = —
1: x = A
1: map = A → 1
3: x = A
3: map = A → 2
6: x = C
6: map = A → 2, C → 1
collectMax[K](key: Events[K], value: Events[Int]): Events[Map[K, Int]]
Get the maximum of value
, grouped by key
.
Usage example:
in x: Events[String]
in y: Events[Int]
def map = Map.collectMax(x, y)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream y: events
stream map: signal
---
0: map = —
1: x = A
1: y = 5
1: map = A → 5
3: y = 2
3: map = A → 5
4: x = C
5: y = 7
5: map = A → 5, C → 7
collectMin[A](key: Events[A], value: Events[Int])
Get the minimum of value
, grouped by key
.
Usage example:
in x: Events[String]
in y: Events[Int]
def map = Map.collectMin(x, y)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: signal
stream y: events
stream map: signal
---
0: map = —
1: x = A
1: y = 5
1: map = A → 5
3: y = 2
3: map = A → 2
4: x = C
5: y = 7
5: map = A → 2, C → 7
liftable contains[K, V](map: strict Map[K, V], key: strict K): Bool
Returns true if the given map
contains a mapping for the specified key
Usage example:
in x: Events[(String, Int)]
in y: Events[String]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def contains = Map.contains(map, y)
out contains
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream y: signal
stream contains: signal
---
0: map = —
1: x = (A, 2)
1: map = A → 2
1: y = B
1: contains = false
3: x = (B, 4)
3: map = A → 2, B → 4
3: contains = true
7: x = (B, 8)
7: map = A → 2, B → 8
7: y = C
7: contains = false
counting[A](events: Events[A])
Map every new value of events
to a unique integer ID
Usage example:
in x: Events[String]
def map = Map.counting(x)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
---
0: map = —
1: x = A
1: map = A → 1
3: x = C
3: map = A → 1, C → 2
6: x = A
6: map = A → 1, C → 2
def counting[A](events: Events[A]) = {
def counter: Events[Int] =
default(
if !contains(last(map, events), events)
then last(counter, events) + 1
else last(counter, events),
0)
def map: Events[Map[A, Int]] =
default(
if !contains(last(map, events), events)
then add(last(map, events), events, counter)
else last(map, events),
empty[A, Int])
map
}
empty[K, V]: Map[K, V]
Returns an empty map
without any element of type T
liftable fold[K, V, R](map: strict Map[K, V], start: lazy R, f: strict (strict R, strict K, strict V) => R): R
Applies a function f
on all values of the map
. Starting with the initial value start
the function f
is called for every pair of the map
with the last result and the current element value as arguments.
Usage example:
in x: Events[(String, Int)]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def sum = Map.fold(map, 0, (r: Int, k: String, v: Int) => r + v)
out sum
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream sum: signal
---
0: map = —
0: sum = 0
1: x = (A, 2)
1: map = A → 2
1: sum = 2
3: x = (B, 4)
3: map = A → 2, B → 4
3: sum = 6
7: x = (B, 8)
7: map = A → 2, B → 8
7: sum = 10
liftable get[K, V](map: strict Map[K, V], key: strict K): V
Get the corresponding value
for the given key
Usage example:
in x: Events[(String, Int)]
in y: Events[String]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def value = Map.get(map, y)
out value
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream y: signal
stream value: signal
---
0: map = —
1: x = (A, 2)
1: map = A → 2
2: y = A
2: value = 2
3: x = (B, 4)
3: map = A → 2, B → 4
3: y = B
3: value = 4
7: x = (B, 8)
7: map = A → 2, B → 8
7: value = 8
liftable getOrElse[K, V](map: Map[K, V], key: K, default: V): V
Get the value
associated with the key
if it exists in the map, and the default value otherwise.
Usage example:
in x: Events[(String, Int)]
in y: Events[String]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def result = Map.getOrElse(map, y, 42)
out result
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream y: signal
stream result: signal
---
0: map = —
1: x = (A, 2)
1: map = A → 2
1: y = A
1: result = 2
3: y = B
3: result = 42
5: x = (B, 4)
5: map = A → 2, B → 4
5: result = 4
liftable inc[K](counts: Map[K, Int], key: K): Map[K, Int]
Increments the value
of the given key
by one, or add a new entry key → 1
if the key
does not exist.
Usage example:
in x: Events[String]
def map: Events[Map[String, Int]] = fold(x, Map.empty[String, Int], Map.inc)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
---
0: map = —
1: x = A
1: map = A → 1
3: x = A
3: map = A → 2
6: x = C
6: map = A → 2, C → 1
liftable keys[K, V](map: strict Map[K, V]): List[K]
Returns the keys
of the elements in the given map
as list
Usage example:
in x: Events[(String, Int)]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def keys = Map.keys(map)
out keys
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream keys: signal
---
0: map = —
0: keys = —
3: x = (A, 2)
3: map = A → 2
3: keys = A
7: x = (B, 4)
7: map = A → 2, B → 4
7: keys = A, B
liftable remove[K, V](map: strict Map[K, V], key: strict K): Map[K, V]
Removes the entry matching the given key
Usage example:
in x: Events[String]
def filledMap = Map.add(Map.add(Map.empty[String, Int], "A", 2), "B", 4)
def map: Events[Map[String, Int]] = fold(x, filledMap, Map.remove)
out map
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
---
0: map = A → 2, B → 4
4: x = A
4: map = B → 4
7: x = C
7: map = B → 4
liftable size[K, V](map: strict Map[K, V]): Int
Returns the number of elements in the given map
Usage example:
in x: Events[(String, Int)]
def map: Events[Map[String, Int]] =
fold(x, Map.empty[String, Int],
(acc: Map[String, Int], v: (String, Int)) => Map.add(acc, v._1, v._2)
)
def size = Map.size(map)
out size
Trace example:
option timeDomain: [-1, 10]
stream x: events
stream map: signal
stream size: signal
---
0: map = —
0: size = 0
1: x = (A, 2)
1: map = A → 2
1: size = 1
4: x = B → 4
4: map = A → 2, B → 4
4: size = 2
7: x = (B, 8)
7: map = A → 2, B → 8
7: size = 2