Options
All
  • Public
  • Public/Protected
  • All
Menu

Index

Type aliases

Accumulator

Accumulator: function

Type declaration

    • (input: chunk, state: result): result
    • Parameters

      • input: chunk
      • state: result

      Returns result

Dict

Dict: object

Dictionary of keys and values. Dict<User> is a dictionary that lets you look up a User by a string key (such as user names).

Type declaration

  • [key: string]: void | a

Entry

Entry: [string, a]

Mapper

Mapper: function

Type declaration

    • (input: a): b
    • Parameters

      • input: a

      Returns b

Predicate

Predicate: function

Type declaration

    • (input: a): boolean
    • Parameters

      • input: a

      Returns boolean

Updater

Updater: Mapper<Maybe<a>, Maybe<a>>

Variables

Dictionary

Dictionary: DictionaryConstructor = <DictionaryConstructor>function Dictionary():void {}

Dictionary class used across the library to create Dict instances.

Functions

diff

  • Keep a entries from left dictionary only if right dictionary does not have entry for that key.

    const left = Dictionary.fromEntries([["a", 1], ["b", 2]])
    left // => <Dict<number>>{a:1, b:2}
    const right = Dictionary.fromEntries([["b", 18], ["c", 9]])
    right // => <Dict<number>>{b:18, c:9}
    const diff = Dictionary.diff(left, right)
    diff // => <Dict<number>>{a:1}
    

    Type parameters

    • a

    Parameters

    Returns Dict<a>

empty

  • Create an empty dictionary.

    const v0 = Dictionary.empty()
    v0 // => <Dict<any>>{}
    const v1 = Dictionary.set('Jack', 1, v0)
    v1 // => <Dict<any>>{"Jack": 1}
    const v2 = Dictionary.set('Jane', 'Jane', v1)
    v2 // => <Dict<any>>{"Jack": 1, "Jane": "Jane"}
    

    If optional dictioray is passed retuned empty dictionary will be of the same type entries. If ommited it will be of any type.

    const v$:Dictionary.Dict<number> = Dictionary.empty()
    v$ // => <Dict<number>>{}
    const v0 = Dictionary.empty(v$)
    v0 // => <Dict<number>>{}
    const v1 = Dictionary.set('Jack', 1, v0)
    v1 // => <Dict<number>>{Jack: 1}
    Dictionary.set('Jane', 'Jane', v1) // <- ts: 'Jane' is not a number
    

    There are two things to notice in the examlpe above:

    1. Passing optional dictionary is useful as it narrovs down the type of values that can be inserted into dictionary.
    2. Alternatively type can be narroved down via explicit type annotation as seen on the first line.

    Returns Dict<any>

  • Type parameters

    • a

    Parameters

    • dictioray: Dict<a>

    Returns Dict<a>

entries

  • entries<a>(dict: Dict<a>): Iterable<Entry<a>>
  • Returns an iterable of dictionary [key, value] pairs using for of

    const dict = Dictionary.singleton('Car', {color:'blue'})
    
    for (let [key, value] of Dictionary.entries(dict)) {
       // ...
    }
    

    Type parameters

    • a

    Parameters

    Returns Iterable<Entry<a>>

filter

  • Keep a dictionary entries when it satisfies a predicate.

    const before = Dictionary.fromEntries([["a", -1], ["b", 2]])
    before // => <Dict<number>>{a: -1, b: 2}
    
    const after = Dictionary.filter(([_k, v]) => v > 0, before)
    after // => <Dict<number>>{b: 2}
    

    Type parameters

    • a

    Parameters

    Returns Dict<a>

fromEntries

  • fromEntries<a>(entries: Iterable<Entry<a>>): Dict<a>
  • Convert an iterable of [key, value] pairs into a dictionary.

    Dictionary.fromEntries([
       ['Zoe', 17],
       ['Sandro', 18]
    ]) // => <Dict<number>> {Zoe: 17, Sandro: 18}
    
    Dictionary
     .fromEntries((<Map<string, User>>db).entries()) // => <Dict<User>>
    

    Type parameters

    • a

    Parameters

    • entries: Iterable<Entry<a>>

    Returns Dict<a>

get

  • get<a>(key: string, dict: Dict<a>, fallback: a): a
  • Get the value of the entry with a given key and fallback in case if there is no such entry.

    const animals = Dictionary.fromEntries([["Tom", "Cat"], ["Jerry", "Mouse"]])
    
    Dictionary.get("Tom", animals, null) => <string|null> "Cat"
    Dictionary.get("Tom", animals, "") => <string> "Cat"
    Dictionary.get("Spike", animals, null) => <string|null> null
    Dictionary.get("Spike", animals, "") => <string> ""
    

    Type parameters

    • a

    Parameters

    • key: string
    • dict: Dict<a>
    • fallback: a

    Returns a

has

  • has<a>(key: string, dict: Dict<a>): boolean
  • Determine if there is an entry with a given key is in a dictionary.

    const dict = Dictionary.singleton("a", 1)
    
    Dictionary.has("a", dict) // => true
    Dictionary.has("b", dict) // => false
    

    Type parameters

    • a

    Parameters

    • key: string
    • dict: Dict<a>

    Returns boolean

intersect

  • Keep a entries from left dictionary when right dictionary has entries for the same key.

    const left = Dictionary.fromEntries([["a", 1], ["b", 2]])
    left // => <Dict<number>>{a:1, b:2}
    
    const right = Dictionary.fromEntries([["b", 18], ["c", 9]])
    right // => <Dict<number>>{b:18, c:9}
    
    const intersect = Dictionary.intersect(left, right)
    intersect // => <Dict<number>>{b:2}
    

    Type parameters

    • a

    Parameters

    Returns Dict<a>

keys

  • keys<a>(dict: Dict<a>): Iterable<string>
  • Returns an iterable of dictionary keys using for of

    const dict = Dictionary.singleton('Bicycle', {color:'red'})
    
    for (let key of Dictionary.keys(dict)) {
       // ...
    }
    

    Type parameters

    • a

    Parameters

    Returns Iterable<string>

map

  • Creates new dictionary with entries from given dictionary but with it's values mapped via given f function.

    const before = Dictionary.fromEntries([["a", 1], ["b", 2]])
    before // => <Dict<number>>{a: 1, b: 2}
    
    const after = Dictionary.map(([k, v]) =>
                                  [k.toUpperCase(), String(v + 5)], before)
    after // => <Dict<string>>{A:"6", B:"7"}
    

    Type parameters

    • a

    • b

    Parameters

    Returns Dict<b>

merge

  • The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:

    • Only in the left dictionary.
    • In both dictionaries.
    • Only in the right dictionary.

    You then traverse all the keys from lowest to highest, building up whatever you want.

    Dictionary.merge(
      ([key, left], log) =>
         [...log, `- ${key} : ${left}`],
      ([key, [left, right]], log) =>
         [...log, `= ${key} : ${left} -> ${right}`],
      ([key, right], log) =>
         [...log, `+ ${key} : ${right}`],
      Dictionary.fromEntries([["a", 1], ["b", 2]]),
      Dictionary.fromEntries([["b", 18], ["c", 9]]),
      <string[]>[]) // => ['- a : 1', '= b : 2 -> 18', '+ c : 9']
    

    Type parameters

    • a

    • b

    • result

    Parameters

    Returns result

partition

  • Partition a dictionary according to a predicate. The first dictionary contains all entries which satisfy the predicate, and the second contains the rest.

    const all = Dictionary.fromEntries([["a", -1], ["b", 2]])
    all // => <Dict<number>>{a: -1, b: 2}
    
    const [positive, negative] = Dictionary.partition(([_k, v]) => v > 0, all)
    positive // => <Dict<number>>{b: 2}
    negative // => <Dict<number>>{a: -1}
    

    Type parameters

    • a

    Parameters

    Returns [Dict<a>, Dict<a>]

remove

  • remove<a>(key: string, dict: Dict<a>): Dict<a>
  • Remove an entry for the given key from a dictionary. If there is no entry for the given key no changes are made.

    const before = Dictionary.fromEntries([["a", 1], ["b", 2]])
    before // => <Dict<number>>{a: 1, b:2}
    const after = Dictionary.remove("a", before)
    after // => <Dict<number>>{b:2}
    Diremove("c", after) // => <Dict<number>>{b:2}
    

    Type parameters

    • a

    Parameters

    • key: string
    • dict: Dict<a>

    Returns Dict<a>

set

  • set<a>(key: string, value: a, dict: Dict<a>): Dict<a>
  • Insert an entry under the given key with a gievn value into a dictionary. Replaces value of the entry if there was one under that key.

    const v0 = Dictionary.fromEntries([["a", 1]])
    v0 // => <Dict<number>> {a:1}
    
    // Add
    const v1 = Dictionary.set("b", 2, v0)
    v1 // => <Dict<number>> {a:1, b:2}
    
    // Replace
    const v2 = Dictionary.set("b", 15, v1)
    v2 // => <Dict<number>> {a:1, b:15}
    

    Type parameters

    • a

    Parameters

    • key: string
    • value: a
    • dict: Dict<a>

    Returns Dict<a>

singleton

  • singleton<a>(key: string, value: a): Dict<a>
  • Create a dictionary with one entry of given key, value pair.

    Dictionary.singleton('Zoe', 15) // => <Dict<number>>{Zoe: 15}
    Dictionary.singleton('a', {foo:"bar"}) // => <Dict<{foo:string}>>{a: {foo:"bar"}}
    

    Type parameters

    • a

    Parameters

    • key: string
    • value: a

    Returns Dict<a>

union

  • Combine two dictionaries. If there is a collision, preference is given to the first dictionary.

    const left = Dictionary.fromEntries([["a", 1], ["b", 2]])
    left // => <Dict<number>>{a:1, b:2}
    
    const right = Dictionary.fromEntries([["b", 18], ["c", 9]])
    right // => <Dict<number>>{b:18, c:9}
    
    const union = Dictionary.union(left, right)
    union // => <Dict<number>>{a:1, b:2, c:9}
    

    Type parameters

    • a

    Parameters

    Returns Dict<a>

update

  • Updates the entry in the dictionary for a given key with a provided updater function. If updader returns Maybe.nothing entry is removed from the dictionory otherwise it's value is swapped with returned value.

    const v0 = Dictionary.fromEntries([["a", 1], ["b", 2]])
    v0 // => <Dict<number>>{a:1, b:2}
    
    const inc = (v:null|undefined|number):number =>
       v == null ? 0 : v + 1
    
    // Add
    const v1 = Dictionary.update("c", inc, v0)
    v1 // => <Dict<number>>{a:1, b:2, c:0}
    
    // Modify
    const v2 = Dictionary.update("b", inc, v1)
    v2 // => <Dict<number>>{a:1, b:3, c:0}
    
    // Delete
    const v3 = Dictionary.update("b", _ => null, v2)
    v3 // => <Dict<number>>{a:1, c:0}
    
    const v4 = Dictionary.update("c", _ => undefined, v3)
    v4 // => <Dict<number>>{a:1}
    
    // NoOp
    const v5 = Dictionary.update("d", _ => null, v4)
    v5 // => <Dict<number>>{a: 1}
    

    Type parameters

    • a

    Parameters

    Returns Dict<a>

values

  • values<a>(dict: Dict<a>): Iterable<a>
  • Returns an iterable of dictionary values using for of

    const dict = Dictionary.singleton('Horse', {color:'black'})
    
    for (let value of Dictionary.values(dict)) {
       // ...
    }
    

    Type parameters

    • a

    Parameters

    Returns Iterable<a>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc