TeSSLa: TeSSLa Standard Library for TeSSLa 2.0.0

module Map

Maps

Imports

ANCHOR Type Map

Map[K, V]

ANCHOR add

liftable add[K, V](map: expand Map[K, V], key: strict K, value: expand 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
liftable def add[K, V](map: expand Map[K, V], key: strict K, value: expand V): Map[K, V] = extern("Map_add")

ANCHOR collectCount

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
def collectCount[T](events: Events[T]): Events[Map[T, Int]] =
  StreamFunctions.fold(events, empty[T, Int], inc)

ANCHOR collectMax

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
def collectMax[K](key: Events[K], value: Events[Int]): Events[Map[K, Int]] = {
  def oldMap = last(map, value)
  def maxValue: Events[Int] = max(getOrElse(oldMap, on(value, key), value), value)
  def map: Events[Map[K, Int]] = default(add(oldMap, on(value, key), maxValue), empty[K, Int])
  map
}

ANCHOR collectMin

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
def collectMin[A](key: Events[A], value: Events[Int]) = {
  def oldMap = last(map, value)
  def minValue: Events[Int] = min(getOrElse(oldMap, on(value, key), value), value)
  def map: Events[Map[A, Int]] = default(add(oldMap, on(value, key), minValue), empty[A, Int])
  map
}

ANCHOR contains

liftable contains[K, V](map: expand 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
liftable def contains[K, V](map: expand Map[K, V], key: strict K): Bool = extern("Map_contains")

ANCHOR counting

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
}

ANCHOR empty

empty[K, V]: Map[K, V]

Returns an empty map without any element of type T

def empty[K, V]: Map[K, V] = extern("Map_empty")

ANCHOR fold

liftable fold[K, V, R](map: expand Map[K, V], start: lazy R, f: strict (expand R, strict K, expand 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 def fold[K, V, R](map: expand Map[K, V], start: lazy R, f: strict (expand R, strict K, expand V) => R): R = extern("Map_fold")

ANCHOR get

liftable get[K, V](map: expand 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 def get[K, V](map: expand Map[K, V], key: strict K): V = extern("Map_get")

ANCHOR getOrElse

liftable getOrElse[K, V](map: expand Map[K, V], key: K, default: expand 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 def getOrElse[K, V](map: expand Map[K, V], key: K, default: expand V): V =
  if contains(map, key)
  then get(map, key)
  else default

ANCHOR inc

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 def inc[K](counts: Map[K, Int], key: K): Map[K, Int] =
  add(counts, key, getOrElse(counts, key, 0) + 1)

ANCHOR keys

liftable keys[K, V](map: expand 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 def keys[K, V](map: expand Map[K, V]): List[K] = extern("Map_keys")

ANCHOR map

liftable map[A, B, C, D](m: expand Map[A, B], f: strict (strict A, expand B) => (C, D)): Map[C, D]

Applies a function to all elements in the map and returns a new map with the mapped elements. The applied function f is able to map key and value. The order in which the mapping function is applied to the elements is non-deterministic. If f maps several entries to the same key, the later mappings overwrite the previous ones. Usage example:

in x: Events[Map[Int, Int]]
def y: Events[Map[Int, Int]] = Map.map(l, (a: Int, b: Int) => (a + 1, b + 1))
out y

Trace example:

option timeDomain: [-1,7]
stream x: signal
stream y: signal
---
0: x = 1 → 2, 2 → 3
0: y = 2 → 3, 3 → 4
2: x = 1 → 0
2: y = 2 → 1
4: x = 1 → 3, 2 → 6
4: y = 2 → 4, 3 → 7
6: x = —
6: y = —
liftable def map[A, B, C, D](m: expand Map[A, B], f: strict (strict A, expand B) => (C, D)) : Map[C, D] = extern("Map_map")

ANCHOR remove

liftable remove[K, V](map: expand 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 def remove[K, V](map: expand Map[K, V], key: strict K): Map[K, V] = extern("Map_remove")

ANCHOR size

liftable size[K, V](map: expand 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
liftable def size[K, V](map: expand Map[K, V]): Int = extern("Map_size")
Imported from PrimitiveFunctions

atan, cos, floatToInt, intToFloat, log, max, min, pow, sin, tan, toString

Imported from StreamFunctions

average, boolFilter, bursts, burstsSince, const, constIf, count, default, defaultFrom, defined, delay, falling, filter, first, firstEvent, fold, isFirst, last, lift, lift1, lift2, lift3, lift4, lift5, maximum, merge, merge2, merge3, merge4, merge5, merge6, merge7, merge8, mergeUnit, mergeUnit2, mergeUnit3, mergeUnit4, minimum, nil, noEvent, on, period, prev, pure, reduce, resetCount, rising, runtime, sample, slift, slift1, slift2, slift3, slift4, slift5, sum, time, unitIf, zip, zip2, zip3, zip4, zip5