Skip to content

Latest commit

 

History

History
408 lines (283 loc) · 6.43 KB

map.md

File metadata and controls

408 lines (283 loc) · 6.43 KB

Module iiris/map

The iiris/map module includes functions for working with Maps. It is designed to be imported with a wildcard, e.g.

import * as M from 'iiris/map'

Table of contents

Map

empty

<K, V>() => Map<K, V>

Create an empty map.

Example
M.empty()
// => Map(0) {}

See also: singleton


entries

<K, V>(map: Map<K, V>) => IterableIterator<[K, V]>

Return a new iterator containing the key-value pairs of the map in insertion order.

Example
M.entries()
// => [Map Entries] { [ 'a', 1 ] }

forEach

<K, V>(fn: (key: K, value: V) => void) => (map: Map<K, V>) => Map<K, V>

Apply a fn to each key-value pair of the map.

Example
M.forEach((k, v) => {
  conso
}, map)

fromEntries

<K, V>(entries: Iterable<[K, V]>) => Map<K, V>

Create a map from an array of [key, value] pairs.

Example
M.fromEntries([
  ['a', 1],
  ['b', 2],
  ['c', 3],
])
// => Map(3) { 'a' => 1, 'b' => 2, 'c' => 3 }

See also: entries


fromObject

<K extends string, V>(object: Record<K, V>) => Map<K, V>

Convert an object to a map.

Example
M.fromObject({ a: 1, b: 2, c: 3 })
// => Map(3) { 'a' => 1, 'b' => 2, 'c' => 3 }

get

<K>(key: K) => <V>(map: Map<K, V>) => V | undefined

Retrieve the specified value from the map.

Example
M.get('a', M.singleton('a', 1))
// => 1

M.get('b', M.singleton('a', 1))
// => undefined

See also: getOr


getOr

<V>(defaultValue: V) => <K>(key: K) => (map: Map<K, V>) => V

Retrieve the specified value or defaultValue from the map.

Example
M.getOr(999, 'a', M.singleton('a', 1))
// => 1

M.getOr(999, 'b', M.singleton('a', 1))
// => 999

has

<K>(key: K) => <V>(map: Map<K, V>) => boolean

Check if map contains the specified key.

Example
M.has('a', M.singleton('a', 1))
// => true

isEmpty

<K, V>(map: Map<K, V>) => boolean

Check if the map is empty.

Example
M.isEmpty(M.empty())
// => true

keys

<K, V>(map: Map<K, V>) => IterableIterator<K>

Return a new iterator containing the keys of the map in insertion order.

Example
M.keys(M.singleton('a', 1))
// => [Map Iterator] { 'a' }

map

<V1, V2>(fn: (value: V1) => V2) => <K>(map: Map<K, V1>) => Map<K, V2>

Return a map containing the results of applying fn to each value of the original map.

Example
M.map((n) => n + 1, M.fromObject({ a: 1, b: 2, c: 3 }))
// => Map(3) { 'a' => 2, 'b' => 3, 'c' => 4 }

See also: mapKeys


mapKeys

<K1, K2>(fn: (key: K1) => K2) => <V>(map: Map<K1, V>) => Map<K1, V>

Return a map containing the results of applying fn to each key of the original map.

Example
M.mapKeys((k) => k.toUpperCase(), M.fromObject({ a: 1, b: 2, c: 3 }))
// => Map(3) { 'A' => 1, 'B' => 2, 'C' => 3 }

See also: mapKeys


modify

<K>(key: K) => <V>(fn: (value: V) => V) => (map: Map<K, V>) => Map<K, V>

Return a copy of map where key has been replaced by applying fn to its current value.

  • If the map doesn't contain key, it is returned unchanged.
Example
M.modify('a', (n) => n + 1, M.singleton('a', 1))
// => Map(1) { 'a' => 2 }

See also: modify, remove


remove

<K>(key: K) => <V>(map: Map<K, V>) => Map<K, V>

Return a copy of map without the specified key.

Example
M.remove('a', M.singleton('a', 1))
// => Map(2) { 'a' => 1, 'b' => 2 }

See also: modify, remove


set

<K>(key: K) => <V>(value: V) => (map: Map<K, V>) => Map<K, V>

Return a copy of map with key set to value.

Example
M.set('b', 2, M.singleton('a', 1))
// => Map(2) { 'a' => 1, 'b' => 2 }

See also: modify, remove


singleton

<K>(key: K) => <V>(value: V) => Map<K, V>

Create a map with a single element.

Example
M.singleton('a', 1)
// => Map(1) { 'a' => 1 }

See also: singleton


size

<K, V>(map: Map<K, V>) => number

Return the number of entries in the map.

Example
M.size(M.singleton('a', 1)
// => 1

values

<K, V>(map: Map<K, V>) => IterableIterator<V>

Return a new iterator containing the values of the map in insertion order.

Example
M.values(M.singleton('a', 1))
// => [Map Iterator] { 1 }