Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keys() and vals() for AssocList #646

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/AssocList.mo
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// structures. Thus, other map implementations are easier to use in most cases.

import List "List";
import Iter "Iter";

module {
/// Import from the base library to use this module.
Expand Down Expand Up @@ -398,5 +399,61 @@ module {
}
};
rec(map)
}
};

/// Returns an Iterator (`Iter`) over the keys of the list.
/// Iterator provides a single method `next()`, which returns
/// keys in no specific order, or `null` when out of keys to iterate over.
///
/// Example:
/// ```motoko include=import,initialize
/// // Create map = [(0, 10), (1, 11), (2, 12)]
/// var map : AssocList<Nat, Nat> = null;
/// map := AssocList.replace(map, 0, Nat.equal, ?10).0;
/// map := AssocList.replace(map, 1, Nat.equal, ?11).0;
/// map := AssocList.replace(map, 2, Nat.equal, ?12).0;
///
/// var keys = "";
/// for (key in map.keys()) {
/// keys := "(" # key # ") " # keys
/// };
/// keys // => "(0) (1) (2)"
/// ```
///
/// Cost of iteration over all keys:
///
/// Runtime: O(size)
///
/// Space: O(1)
public func keys<K, V>(assocList : AssocList<K, V>) : Iter.Iter<K> {
Iter.map(List.toIter<(K, V)>(assocList), func(kv : (K, V)) : K { kv.0 })
};

/// Returns an Iterator (`Iter`) over the values of the list.
/// Iterator provides a single method `next()`, which returns
/// keys in no specific order, or `null` when out of keys to iterate over.
///
/// Example:
/// ```motoko include=import,initialize
/// // Create map = [(0, 10), (1, 11), (2, 12)]
/// var map : AssocList<Nat, Nat> = null;
/// map := AssocList.replace(map, 0, Nat.equal, ?10).0;
/// map := AssocList.replace(map, 1, Nat.equal, ?11).0;
/// map := AssocList.replace(map, 2, Nat.equal, ?12).0;
///
/// var values = "";
/// for (val in map.vals()) {
/// values := "(" # val # ") " # keys
/// };
/// keys // => "(10) (11) (12)"
/// ```
///
/// Cost of iteration over all keys:
///
/// Runtime: O(size)
///
/// Space: O(1)
public func vals<K, V>(assocList : AssocList<K, V>) : Iter.Iter<V> {
Iter.map(List.toIter<(K, V)>(assocList), func(kv : (K, V)) : V { kv.1 })
};
}
11 changes: 11 additions & 0 deletions test/AssocList.test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AssocList "../src/AssocList";
import List "../src/List";
import Nat "../src/Nat";
import Debug "../src/Debug";
import Iter "../src/Iter";

import Suite "mo:matchers/Suite";
import T "mo:matchers/Testable";
Expand Down Expand Up @@ -247,6 +248,16 @@ let suite = Suite.suite(
func(k, v, acc) = k * v + acc
),
M.equals(T.nat(0))
),
Suite.test(
"keys",
Iter.toArray(AssocList.keys(map1)),
M.equals(T.array<Nat>(T.natTestable, [0, 2, 4]))
),
Suite.test(
"vals",
Iter.toArray(AssocList.vals(map1)),
M.equals(T.array<Nat>(T.natTestable, [10, 12, 14]))
)
]
);
Expand Down
Loading