GithubHelp home page GithubHelp logo

motoko-hash-tree's Introduction

Hash Map

Stable hash maps for Motoko

import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Principal "mo:base/Principal";
import Map "mo:hashmap/Map";

let { ihash; nhash; thash; phash; calcHash } = Map;

let map1 = Map.new<Nat, Nat>(nhash);
let map2 = Map.new<Int, Nat>(ihash);
let map3 = Map.new<Text, Nat>(thash);
let map4 = Map.new<Principal, Nat>(phash);

Map.set(map1, nhash, 1, 1);
Map.set(map2, ihash, -1, 2);
Map.set(map3, thash, "aaa", 3);
Map.set(map4, phash, Principal.fromText("aaaaa-aa"), 4);

ignore Map.put(map3, thash, "bbb", 5);
ignore Map.put(map3, thash, "ccc", 6);

ignore Map.has(map1, nhash, 1);

ignore Map.get(map2, ihash, -1);

ignore Map.size(map3);

Map.delete(map3, thash, "bbb");

ignore Map.remove(map3, thash, "ccc");

for (key in Map.keys(map1)) {};
for (value in Map.vals(map1)) {};
for ((key, value) in Map.entries(map1)) {};

let map5 = Map.filter<Int, Nat>(map2, ihash, func(key, value) { key != 1 });

let map6 = Map.map<Nat, Nat, Text>(map1, nhash, func(key, value) { Nat.toText(value) });

let map7 = Map.mapFilter<Nat, Nat, Text>(map1, nhash, func(key, value) { if (key != 1) ?Nat.toText(value) else null });

let iter = Iter.map<Nat, (Nat, Nat)>(Iter.fromArray([]), func(value) { (value, value) });

let map8 = Map.fromIter<Nat, Nat>(iter, nhash);

Map.forEach<Nat, Nat>(map1, func(key, value) { Map.set(map8, nhash, key, value) });

ignore Map.some<Nat, Nat>(map1, func(key, value) { key != 1 });

ignore Map.every<Nat, Nat>(map1, func(key, value) { key != 1 });

ignore Map.find<Nat, Nat>(map1, func(key, value) { key == 1 });

ignore Map.findDesc<Nat, Nat>(map1, func(key, value) { key == 1 });

let hash = calcHash(thash, "ddd");

for (map in [Map.new<Text, Nat>()].vals()) Map.set(map, hash, "ddd", 2);

Map.clear(map1);

motoko-hash-tree's People

Contributors

zhenyausenko avatar

Stargazers

LightningLad avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.