Dictionary class used across the library to create Dict
instances.
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}
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:
dictionary
is useful as it narrovs down the type of
values that can be inserted into dictionary.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}
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>>
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> ""
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
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}
Returns an iterable of dictionary keys using for of
const dict = Dictionary.singleton('Bicycle', {color:'red'})
for (let key of Dictionary.keys(dict)) {
// ...
}
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"}
The most general way of combining two dictionaries. You provide three accumulators for when a given key appears:
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']
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}
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}
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}
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"}}
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}
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}
Returns an iterable of dictionary values using for of
const dict = Dictionary.singleton('Horse', {color:'black'})
for (let value of Dictionary.values(dict)) {
// ...
}
Generated using TypeDoc
Dictionary of keys and values.
Dict<User>
is a dictionary that lets you look up aUser
by astring
key (such as user names).