Last active
September 8, 2021 21:40
-
-
Save rbolog/e5ed92ce74771204d244be2dca843e99 to your computer and use it in GitHub Desktop.
Motoko HashMap mapFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Playground