Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Sep 22, 2020. It is now read-only.
/typed-hashmapPublic archive

Persistent HashMap for TypeScript

License

NotificationsYou must be signed in to change notification settings

TylorS/typed-hashmap

Repository files navigation

Immutable HashMap for TypeScript

A fast and persistent (immutable) Hash Array Map Trie for TypeScript.

This is heavily based off ofhamt andwas mainly done by me for learning purposes, but it is likely very useful, and Iplan to use it as well.

Once main difference to the hamt library is that keys can be of any typeincluding objects or arrays much like ES2015Maps.Another notable change is there arenot prototype methods to allowfor ES2015 modules to be tree-shaken for smaller builds 🔥

There is a heavy emphasis on typing this library, and it's highly recommendedto be used with TypeScript to reap the full benefits of type guarantees.

There is more to be done but the bare minimum is definitely present.

Let me have it!

npm install --save @typed/hashmap

API

All multi-parameter functions are curried!

Creating a HashMap

####empty<K, V>(): HashMap<K, V>

Creates an empty HashMap that will accept typeK as keys andV as values.

import{empty}from'@typed/hashmap';constmap=empty<string,number>();

fromObject<V>(object: { [key: string]: V }): HashMap<K, V>

Creates a HashMap from an object.

import{fromObject}from'@typed/hashmap';constmap=fromObject<number>({a:1,b:2});

fromArray<K, V>(array: Array<[K, V]>): HashMap<K, V>

Creates a HashMap from an array of tuples.

import{fromArray}from'@typed/hashmap';constmap=fromArray<string,number>([['a',1],['b',2]]);

fromIterable<K, V>(iterable: Iterable<[K, V]>): HashMap<K, V>

Creates a HashMap from an Iterable.

Warning: this method usingArray.from internally, and will require a polyfillif not in an environment that supports this feature.

import{fromIterable}from'@typed/hashmap';constmap=fromIterable(someIterable);

Using a HashMap

set<K, V>(key: K, value: V, map: HashMap<K, V>): HashMap<K, V>

Returns a new HashMap containing thekey andvalue passed toset.This operation is immutable and will not alter the map passed to it.

import{set,get,empty}from'@typed/hashmap';constmap=empty<string,number>();consta=set('a',1,map);console.log(get('a',a))// 1

get<K, V>(key: K, map: HashMap<K, V>): V | null

Attempts to find a value in a given HashMap. Returnsnull if none can be found.

import{set,get,empty}from'@typed/hashmap';constmap=empty<string,number>();consta=set('a',1,map);console.log(get('a',a))// 1

has<K, V>(key: K, map: HashMap<K, V>): boolean

Returns true if a map contains a particular key and false if it does not.

import{empty,has,set}from'@typed/hashmap';consthasA=has('a');constmap=empty<string,number>();hasA(map)// falsehasA(set('a',1,map))// true

size<K, V>(map: HashMap<K, V>): number

Returns the number of key value pairs a given map contains

import{size,empty,fromObject}from'@typed/hashmap';size(empty())// 0size(fromObject({a:1,b:2}))// 2

remove<K, V>(key: K, map: HashMap<K, V>): HashMap<K ,V>

Returns a HashMap that no longer contains a value forkey.

import{remove,fromObject,has}from'@typed/hashmap';constmap=fromObject({a:1,b:2,c:3})consthasB=has('b')hasB(map)// truehasB(remove('b',map))// false

entries<K, V>(map: HashMap<K, V>): Iterator<[K, V]>

Guaranteeing no order creates an iterator of keys and values held withina given HashMap.

import{entries,fromObject}from'@typed/hashmap';constmap=fromObject({a:1,b:2,c:3})for(letentryofentries(map)){console.log(entry)// ['a', 1] ['b', 2] ['c' 3]}// manually using iteratorconstiterator=entries(map)console.log(iterator.next().value)// ['a', 1]console.log(iterator.next().value)// ['c', 3]console.log(iterator.next().value)// ['b', 2]console.log(iterator.next().value)// null

keys<K, V>(map: HashMap<K, V>): Iterator<K>

Guaranteeing no order creates an iterator of keys held withina given HashMap.

import{keys,fromArray}from'@typed/hashmap';constmap=fromArray([['a',1],['b',2],['c',3]])constiterator=keys(map)console.log(iterator.next().value)// 'a'console.log(iterator.next().value)// 'b'console.log(iterator.next().value)// 'c'console.log(iterator.next().value)// null

values<K, V>(map: HashMap<K, V>): Iterator<V>

Guaranteeing no order creates an iterator of keys held withina given HashMap.

import{keys,fromArray}from'@typed/hashmap';constmap=fromArray([['a',1],['b',2],['c',3]])constiterator=keys(map)console.log(iterator.next().value)// 1console.log(iterator.next().value)// 2console.log(iterator.next().value)// 3console.log(iterator.next().value)// null

reduce<K, V, R>(f: (accum: R, value: V, key?: K) => R, seed: R, map: HashMap<K, V>): R

Fold over the values held in a HashMap, similar toArray.prototype.reduce.

import{reduce,fromIterable}from'@typed/hashmap';constiterable=newMap([[1,1],[2,2],[3,3]]);constmap=fromIterable(iterable);constsum=(x:number,y:number)=>x+y;console.log(reduce(sum,0,map))// 6

forEach<K, V>(f: (value: V, key?: K) => any, map: HashMap<K, V>): HashMap<K, V>

Perform side effects on each value contained in a HashMap, returning the originalHashMap.

import{forEach,fromObject}from'@typed/hashmap';constmap=fromObject({a:1,b:2,c:3})constmap2=forEach(x=>console.log(x),map)// 1, 2, 3map===map2// true

map<K, V, R>(f: (value: V, key?: K) => R, map: HashMap<K, V>): HashMap<K, R>;

Creates a new HashMap of the same keys, but new values as the result of callingthe provided function on each value contained in the given HashMap, similar toArray.prototype.map.

import{map,forEach,fromObject}from'@typed/hashmap';consta=map(x=>x+1,fromObject({a:1,b:2,c:3}))forEach((value,key)=>console.log(value,key),a)// 'a' 2 , 'b' 3, 'c' 4

filter<K, V>(predicate: (value: V, key?: K) => boolean, map: HashMap<K, V>): HashMap<K, V>

Creates a new HashMap containing only values that returntrue when the predicatefunction is called with a given value, similar toArray.prototype.filter.

import{filter,forEach,fromObject}from'@typed/hashmap';consta=filter(x=>x%2===0,fromObject({a:1,b:2,c:3}))forEach((value,key)=>console.log(value,key),a)// 'b' 2

[8]ページ先頭

©2009-2025 Movatter.jp