Skip to content

Instantly share code, notes, and snippets.

@rbolog
Last active September 8, 2021 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbolog/e5ed92ce74771204d244be2dca843e99 to your computer and use it in GitHub Desktop.
Save rbolog/e5ed92ce74771204d244be2dca843e99 to your computer and use it in GitHub Desktop.
Motoko HashMap mapFilter
import Blob "mo:base/Blob";
import HashMap "mo:base/HashMap";
import List "mo:base/List";
import Nat8 "mo:base/Nat8";
import Nat "mo:base/Nat";
import Hash "mo:base/Hash";
import Iter "mo:base/Iter";
import Text "mo:base/Text";
import Debug "mo:base/Debug";
import Option "mo:base/Option";
// Playground https://m7sm4-2iaaa-aaaab-qabra-cai.raw.ic0.app/?tag=4111785473
actor {
public func testHashMap() {
let testMap = HashMap.HashMap<Nat,Text>(10, Nat.equal, Hash.hash);
testMap.put(1,"A");
testMap.put(2,"B");
testMap.put(3,"C");
testMap.put(4,"D");
testMap.put(5,"E");
testMap.put(6,"F");
testMap.put(7,"G");
testMap.put(8,"H");
testMap.put(9,"I");
testMap.put(10,"J");
for ((k,v) in testMap.entries()) {
Debug.print("1] " # Nat.toText(k) # "-" # v);
};
let test2 = HashMap.mapFilter<Nat,Text,Text>(testMap,Nat.equal, Hash.hash,func(k : Nat,v : Text): ?Text{ if(k%2==0) ?v else null});
for ((k,v) in test2.entries()) {
Debug.print("2] " # Nat.toText(k) # "-" # v);
};
};
}
@rbolog
Copy link
Author

rbolog commented Sep 8, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment