Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

HashMap

42 exports Since v2.0.0 Source

Constructors

Since v2.0.0

Creates a new HashMap.

Signature

declare const empty: <K = never, V = never>() => HashMap<K, V>

Creates a new HashMap from an iterable collection of key/value pairs.

Signature

declare const fromIterable: <K, V>(entries: Iterable<readonly [K, V]>) => HashMap<K, V>
Since v2.0.0

Constructs a new HashMap from an array of key/value pairs.

Signature

declare const make: <Entries extends ReadonlyArray<readonly [any, any]>>(...entries: Entries) => HashMap<Entries[number] extends readonly [infer K, any] ? K : never, Entries[number] extends readonly [any, infer V] ? V : never>

Elements

Since v3.14.0

Checks if all entries in a hashmap meets a specific condition.

Signature

declare const every: {
<K, A>(predicate: (a: NoInfer<A>, k: K) => boolean): (self: HashMap<K, A>) => boolean;
<K, A>(self: HashMap<K, A>, predicate: (a: A, k: K) => boolean): boolean;
}

Returns the first element that satisfies the specified predicate, or None if no such element exists.

Signature

declare const findFirst: {
<K, A, B>(predicate: (a: NoInfer<A>, k: K) => a is B): (self: HashMap<K, A>) => Option<[K, B]>;
<K, A>(predicate: (a: NoInfer<A>, k: K) => boolean): (self: HashMap<K, A>) => Option<[K, A]>;
<K, A, B>(self: HashMap<K, A>, predicate: (a: A, k: K) => a is B): Option<[K, B]>;
<K, A>(self: HashMap<K, A>, predicate: (a: A, k: K) => boolean): Option<[K, A]>;
}
Since v2.0.0

Safely lookup the value for the specified key in the HashMap using the internal hashing function.

Signature

declare const get: {
<K1, K>(key: K1): <V>(self: HashMap<K, V>) => Option<V>;
<K1, K, V>(self: HashMap<K, V>, key: K1): Option<V>;
}
Since v2.0.0

Lookup the value for the specified key in the HashMap using a custom hash.

Signature

declare const getHash: {
<K1, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => Option<V>;
<K1, K, V>(self: HashMap<K, V>, key: K1, hash: number): Option<V>;
}
Since v2.0.0

Checks if the specified key has an entry in the HashMap.

Signature

declare const has: {
<K1, K>(key: K1): <K, V>(self: HashMap<K, V>) => boolean;
<K1, K, V>(self: HashMap<K, V>, key: K1): boolean;
}
Since v3.16.0

Checks if an element matching the given predicate exists in the given HashMap.

Signature

declare const hasBy: {
<K, V>(predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): (self: HashMap<K, V>) => boolean;
<K, V>(self: HashMap<K, V>, predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): boolean;
}

Example

import { HashMap } from "effect"
const hm = HashMap.make([1, 'a'])
HashMap.hasBy(hm, (value, key) => value === 'a' && key === 1); // -> true
HashMap.hasBy(hm, (value) => value === 'b'); // -> false
Since v2.0.0

Checks if the specified key has an entry in the HashMap using a custom hash.

Signature

declare const hasHash: {
<K1, K>(key: K1, hash: number): <V>(self: HashMap<K, V>) => boolean;
<K1, K, V>(self: HashMap<K, V>, key: K1, hash: number): boolean;
}
Since v2.0.0

Checks if the HashMap contains any entries.

Signature

declare const isEmpty: <K, V>(self: HashMap<K, V>) => boolean
Since v3.13.0

Checks if any entry in a hashmap meets a specific condition.

Signature

declare const some: {
<K, A>(predicate: (a: NoInfer<A>, k: K) => boolean): (self: HashMap<K, A>) => boolean;
<K, A>(self: HashMap<K, A>, predicate: (a: A, k: K) => boolean): boolean;
}

Filtering

Since v2.0.0

Filters out None values from a HashMap of Optionss.

Signature

declare const compact: <K, A>(self: HashMap<K, Option<A>>) => HashMap<K, A>
Since v2.0.0

Filters entries out of a HashMap using the specified predicate.

Signature

declare const filter: {
<K, A, B>(f: (a: NoInfer<A>, k: K) => a is B): (self: HashMap<K, A>) => HashMap<K, B>;
<K, A>(f: (a: NoInfer<A>, k: K) => boolean): (self: HashMap<K, A>) => HashMap<K, A>;
<K, A, B>(self: HashMap<K, A>, f: (a: A, k: K) => a is B): HashMap<K, B>;
<K, A>(self: HashMap<K, A>, f: (a: A, k: K) => boolean): HashMap<K, A>;
}

Maps over the entries of the HashMap using the specified partial function and filters out None values.

Signature

declare const filterMap: {
<A, K, B>(f: (value: A, key: K) => Option<B>): (self: HashMap<K, A>) => HashMap<K, B>;
<K, A, B>(self: HashMap<K, A>, f: (value: A, key: K) => Option<B>): HashMap<K, B>;
}

Folding

Since v3.17.0

Counts all the element of the given HashMap that pass the given predicate

Signature

declare const countBy: {
<K, V>(predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): (self: HashMap<K, V>) => number;
<K, V>(self: HashMap<K, V>, predicate: (value: NoInfer<V>, key: NoInfer<K>) => boolean): number;
}

Example

import { HashMap } from "effect"
const map = HashMap.make([1, "a"], [2, "b"], [3, "c"])
const result = HashMap.countBy(map, (_v, key) => key % 2 === 1)
console.log(result) // 2
Since v2.0.0

Reduces the specified state over the entries of the HashMap.

Signature

declare const reduce: {
<Z, V, K>(zero: Z, f: (accumulator: Z, value: V, key: K) => Z): (self: HashMap<K, V>) => Z;
<K, V, Z>(self: HashMap<K, V>, zero: Z, f: (accumulator: Z, value: V, key: K) => Z): Z;
}

Getter

Since v2.0.0

Returns a HashSet of keys within the HashMap.

Signature

declare const keySet: <K, V>(self: HashMap<K, V>) => HashSet<K>

Getters

Since v2.0.0

Returns an IterableIterator of the entries within the HashMap.

Signature

declare const entries: <K, V>(self: HashMap<K, V>) => IterableIterator<[K, V]>
Since v2.0.0

Returns an IterableIterator of the keys within the HashMap.

Signature

declare const keys: <K, V>(self: HashMap<K, V>) => IterableIterator<K>
Since v2.0.0

Returns the number of entries within the HashMap.

Signature

declare const size: <K, V>(self: HashMap<K, V>) => number

Returns an Array<[K, V]> of the entries within the HashMap.

Signature

declare function toEntries<K, V>(self: HashMap<K, V>): Array<[K, V]>
Since v3.13.0

Returns an Array of the values within the HashMap.

Signature

declare function toValues<K, V>(self: HashMap<K, V>): Array<V>
Since v2.0.0

Returns an IterableIterator of the values within the HashMap.

Signature

declare const values: <K, V>(self: HashMap<K, V>) => IterableIterator<V>

Mapping

Since v2.0.0

Maps over the entries of the HashMap using the specified function.

Signature

declare const map: {
<A, V, K>(f: (value: V, key: K) => A): (self: HashMap<K, V>) => HashMap<K, A>;
<K, V, A>(self: HashMap<K, V>, f: (value: V, key: K) => A): HashMap<K, A>;
}

Models

HashMap

interface
Since v2.0.0

Signature

interface HashMap<out Key, out Value> extends Iterable<[Key, Value]>, Equal, Pipeable, Inspectable {
readonly [TypeId]: typeof TypeId;
}

Other

Marks the HashMap as mutable.

Signature

declare const beginMutation: <K, V>(self: HashMap<K, V>) => HashMap<K, V>

Marks the HashMap as immutable.

Signature

declare const endMutation: <K, V>(self: HashMap<K, V>) => HashMap<K, V>
Since v2.0.0
Since v2.0.0

Updates the value of the specified key within the HashMap if it exists.

Signature

declare const modify: {
<K, V>(key: K, f: (v: V) => V): (self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, key: K, f: (v: V) => V): HashMap<K, V>;
}
Since v2.0.0

Set or remove the specified key in the HashMap using the specified update function. The value of the specified key will be computed using the provided hash.

The update function will be invoked with the current value of the key if it exists, or None if no such value exists.

Signature

declare const modifyAt: {
<K, V>(key: K, f: UpdateFn<V>): (self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, key: K, f: UpdateFn<V>): HashMap<K, V>;
}

Alter the value of the specified key in the HashMap using the specified update function. The value of the specified key will be computed using the provided hash.

The update function will be invoked with the current value of the key if it exists, or None if no such value exists.

This function will always either update or insert a value into the HashMap.

Signature

declare const modifyHash: {
<K, V>(key: K, hash: number, f: UpdateFn<V>): (self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, key: K, hash: number, f: UpdateFn<V>): HashMap<K, V>;
}
Since v2.0.0

Mutates the HashMap within the context of the provided function.

Signature

declare const mutate: {
<K, V>(f: (self: HashMap<K, V>) => void): (self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, f: (self: HashMap<K, V>) => void): HashMap<K, V>;
}
Since v2.0.0

Remove the entry for the specified key in the HashMap using the internal hashing function.

Signature

declare const remove: {
<K>(key: K): <V>(self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, key: K): HashMap<K, V>;
}

Removes all entries in the HashMap which have the specified keys.

Signature

declare const removeMany: {
<K>(keys: Iterable<K>): <V>(self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, keys: Iterable<K>): HashMap<K, V>;
}
Since v2.0.0

Sets the specified key to the specified value using the internal hashing function.

Signature

declare const set: {
<K, V>(key: K, value: V): (self: HashMap<K, V>) => HashMap<K, V>;
<K, V>(self: HashMap<K, V>, key: K, value: V): HashMap<K, V>;
}
Since v2.0.0

Performs a union of this HashMap and that HashMap.

Signature

declare const union: {
<K1, V1>(that: HashMap<K1, V1>): <K0, V0>(self: HashMap<K0, V0>) => HashMap<K1 | K0, V1 | V0>;
<K0, V0, K1, V1>(self: HashMap<K0, V0>, that: HashMap<K1, V1>): HashMap<K0 | K1, V0 | V1>;
}

Refinements

Signature

declare const isHashMap: {
<K, V>(u: Iterable<readonly [K, V]>): u is HashMap<K, V>;
(u: unknown): u is HashMap<unknown, unknown>;
}

Sequencing

Since v2.0.0

Chains over the entries of the HashMap using the specified function.

NOTE: the hash and equal of both maps have to be the same.

Signature

declare const flatMap: {
<A, K, B>(f: (value: A, key: K) => HashMap<K, B>): (self: HashMap<K, A>) => HashMap<K, B>;
<K, A, B>(self: HashMap<K, A>, f: (value: A, key: K) => HashMap<K, B>): HashMap<K, B>;
}

Symbol

Since v2.0.0

Signature

type TypeId = typeof TypeId

Traversing

Since v2.0.0

Applies the specified function to the entries of the HashMap.

Signature

declare const forEach: {
<V, K>(f: (value: V, key: K) => void): (self: HashMap<K, V>) => void;
<V, K>(self: HashMap<K, V>, f: (value: V, key: K) => void): void;
}

Unsafe

Unsafely lookup the value for the specified key in the HashMap using the internal hashing function.

Signature

declare const unsafeGet: {
<K1, K>(key: K1): <V>(self: HashMap<K, V>) => V;
<K1, K, V>(self: HashMap<K, V>, key: K1): V;
}