Skip to content

Commit

Permalink
Refactor to use Types module (#130)
Browse files Browse the repository at this point in the history
* Refactor to use 'Types' module

* Remove unrelated change from diff

* Update API lockfile

* Refactor
  • Loading branch information
rvanasa authored Feb 6, 2025
1 parent 444da36 commit 1d56879
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 136 deletions.
16 changes: 8 additions & 8 deletions src/Array.mo
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
/// import Array "mo:base/Array";
/// ```

import Iter "type/Iter";
import Order "Order";
import Result "Result";
import VarArray "VarArray";
import Option "Option";
import Types "Types";
import Prim "mo:⛔";
import { todo } "Debug";

Expand Down Expand Up @@ -440,7 +440,7 @@ module {
///
/// Space: O(size)
/// *Runtime and space assumes that `k` runs in O(1) time and space.
public func flatMap<T, R>(array : [T], k : T -> Iter.Iter<R>) : [R] {
public func flatMap<T, R>(array : [T], k : T -> Types.Iter<R>) : [R] {
var flatSize = 0;
let arrays = Prim.Array_tabulate<[R]>(
array.size(),
Expand Down Expand Up @@ -539,7 +539,7 @@ module {
/// Runtime: O(number of elements in array)
///
/// Space: O(number of elements in array)
public func join<T>(arrays : Iter.Iter<[T]>) : [T] {
public func join<T>(arrays : Types.Iter<[T]>) : [T] {
flatten(fromIter(arrays))
};

Expand Down Expand Up @@ -598,7 +598,7 @@ module {
public func isEmpty<T>(array : [T]) : Bool = array.size() == 0;

/// Converts an iterator to an array.
public func fromIter<T>(iter : Iter.Iter<T>) : [T] {
public func fromIter<T>(iter : Types.Iter<T>) : [T] {
todo() // Pending `List` data structure
};

Expand All @@ -622,7 +622,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func keys<T>(array : [T]) : Iter.Iter<Nat> = array.keys();
public func keys<T>(array : [T]) : Types.Iter<Nat> = array.keys();

/// Iterator provides a single method `next()`, which returns
/// elements in order, or `null` when out of elements to iterate over.
Expand All @@ -643,7 +643,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func values<T>(array : [T]) : Iter.Iter<T> = array.vals();
public func values<T>(array : [T]) : Types.Iter<T> = array.vals();

/// Iterator provides a single method `next()`, which returns
/// pairs of (index, element) in order, or `null` when out of elements to iterate over.
Expand All @@ -661,7 +661,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func enumerate<T>(array : [var T]) : Iter.Iter<(Nat, T)> = object {
public func enumerate<T>(array : [var T]) : Types.Iter<(Nat, T)> = object {
let size = array.size();
var index = 0;
public func next() : ?(Nat, T) {
Expand Down Expand Up @@ -832,7 +832,7 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func range<T>(array : [T], fromInclusive : Int, toExclusive : Int) : Iter.Iter<T> {
public func range<T>(array : [T], fromInclusive : Int, toExclusive : Int) : Types.Iter<T> {
let size = array.size();
// Convert negative indices to positive and handle bounds
let startInt = if (fromInclusive < 0) {
Expand Down
3 changes: 2 additions & 1 deletion src/Iter.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Array "Array";
import VarArray "VarArray";
import Prim "mo:prim";
import Runtime "Runtime";
import Types "Types";

module {

Expand All @@ -21,7 +22,7 @@ module {
/// …do something with x…
/// }
/// ```
public type Iter<T> = { next : () -> ?T };
public type Iter<T> = Types.Iter<T>;

public func empty<T>() : Iter<T> {
object {
Expand Down
3 changes: 2 additions & 1 deletion src/List.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import Iter "Iter";
import Order "Order";
import Result "Result";
import Types "Types";
import { todo } "Debug";

module {
public type List<T> = (); // Placeholder
public type List<T> = Types.List<T>;

public func empty<T>() : List<T> {
todo()
Expand Down
51 changes: 16 additions & 35 deletions src/Map.mo
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
// Copyright (c) 2022 Byron Becker.
// Distributed under Apache 2.0 license

// import ImmutableMap "immutable/Map";
import IterType "type/Iter";
import Types "Types";
import Order "Order";
import VarArray "VarArray";
import Runtime "Runtime";
Expand All @@ -37,29 +36,11 @@ import Option "Option";
module {
let btreeOrder = 32; // Should be >= 4 and <= 512.

public type Node<K, V> = {
#leaf : Leaf<K, V>;
#internal : Internal<K, V>
};

public type Data<K, V> = {
kvs : [var ?(K, V)];
var count : Nat
};

public type Internal<K, V> = {
data : Data<K, V>;
children : [var ?Node<K, V>]
};

public type Leaf<K, V> = {
data : Data<K, V>
};

public type Map<K, V> = {
var root : Node<K, V>;
var size : Nat
};
public type Node<K, V> = Types.Map.Node<K, V>;
public type Data<K, V> = Types.Map.Data<K, V>;
public type Internal<K, V> = Types.Map.Internal<K, V>;
public type Leaf<K, V> = Types.Map.Leaf<K, V>;
public type Map<K, V> = Types.Map<K, V>;

// /// Convert the mutable key-value map to an immutable key-value map.
// ///
Expand Down Expand Up @@ -676,7 +657,7 @@ module {
/// where `n` denotes the number of key-value entries stored in the map.
///
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
public func entries<K, V>(map : Map<K, V>) : IterType.Iter<(K, V)> {
public func entries<K, V>(map : Map<K, V>) : Types.Iter<(K, V)> {
switch (map.root) {
case (#leaf(leafNode)) { return leafEntries(leafNode) };
case (#internal(internalNode)) { internalEntries(internalNode) }
Expand Down Expand Up @@ -713,7 +694,7 @@ module {
/// where `n` denotes the number of key-value entries stored in the map.
///
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
public func reverseEntries<K, V>(map : Map<K, V>) : IterType.Iter<(K, V)> {
public func reverseEntries<K, V>(map : Map<K, V>) : Types.Iter<(K, V)> {
switch (map.root) {
case (#leaf(leafNode)) { return reverseLeafEntries(leafNode) };
case (#internal(internalNode)) { reverseInternalEntries(internalNode) }
Expand Down Expand Up @@ -747,7 +728,7 @@ module {
/// Cost of iteration over all elements:
/// Runtime: `O(n)`.
/// Space: `O(1)`.
public func keys<K, V>(map : Map<K, V>) : IterType.Iter<K> {
public func keys<K, V>(map : Map<K, V>) : Types.Iter<K> {
object {
let iterator = entries(map);

Expand Down Expand Up @@ -787,7 +768,7 @@ module {
/// Cost of iteration over all elements:
/// Runtime: `O(n)`.
/// Space: `O(1)`.
public func values<K, V>(map : Map<K, V>) : IterType.Iter<V> {
public func values<K, V>(map : Map<K, V>) : Types.Iter<V> {
object {
let iterator = entries(map);

Expand Down Expand Up @@ -818,7 +799,7 @@ module {
/// Space: `O(n)`.
/// where `n` denotes the number of key-value entries returned by the iterator and
/// assuming that the `compare` function implements an `O(1)` comparison.
public func fromIter<K, V>(iter : IterType.Iter<(K, V)>, compare : (K, K) -> Order.Order) : Map<K, V> {
public func fromIter<K, V>(iter : Types.Iter<(K, V)>, compare : (K, K) -> Order.Order) : Map<K, V> {
let map = empty<K, V>();
for ((key, value) in iter) {
add(map, compare, key, value)
Expand Down Expand Up @@ -1152,7 +1133,7 @@ module {
/// Can be used to check that key/value pairs have been inserted with a consistent key comparison function.
/// Traps if the internal map structure is invalid.
public func assertValid<K, V>(map : Map<K, V>, compare : (K, K) -> Order.Order) : () {
func checkIteration(iterator : IterType.Iter<(K, V)>, order : Order.Order) {
func checkIteration(iterator : Types.Iter<(K, V)>, order : Order.Order) {
switch (iterator.next()) {
case null {};
case (?first) {
Expand Down Expand Up @@ -1282,7 +1263,7 @@ module {
}
};

func leafEntries<K, V>({ data } : Leaf<K, V>) : IterType.Iter<(K, V)> {
func leafEntries<K, V>({ data } : Leaf<K, V>) : Types.Iter<(K, V)> {
var i : Nat = 0;
object {
public func next() : ?(K, V) {
Expand All @@ -1297,7 +1278,7 @@ module {
}
};

func reverseLeafEntries<K, V>({ data } : Leaf<K, V>) : IterType.Iter<(K, V)> {
func reverseLeafEntries<K, V>({ data } : Leaf<K, V>) : Types.Iter<(K, V)> {
var i : Nat = data.count;
object {
public func next() : ?(K, V) {
Expand All @@ -1315,7 +1296,7 @@ module {
// Cursor type that keeps track of the current node and the current key-value index in the node
type NodeCursor<K, V> = { node : Node<K, V>; kvIndex : Nat };

func internalEntries<K, V>(internal : Internal<K, V>) : IterType.Iter<(K, V)> {
func internalEntries<K, V>(internal : Internal<K, V>) : Types.Iter<(K, V)> {
object {
// The nodeCursorStack keeps track of the current node and the current key-value index in the node
// We use a stack here to push to/pop off the next node cursor to visit
Expand Down Expand Up @@ -1396,7 +1377,7 @@ module {
}
};

func reverseInternalEntries<K, V>(internal : Internal<K, V>) : IterType.Iter<(K, V)> {
func reverseInternalEntries<K, V>(internal : Internal<K, V>) : Types.Iter<(K, V)> {
object {
// The nodeCursorStack keeps track of the current node and the current key-value index in the node
// We use a stack here to push to/pop off the next node cursor to visit
Expand Down
10 changes: 3 additions & 7 deletions src/Order.mo
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/// Utilities for `Order` (comparison between two values).

import Iter "type/Iter";
import Types "Types";
import { todo } "Debug";

module {

/// A type to represent an order.
public type Order = {
#less;
#equal;
#greater
};
public type Order = Types.Order;

/// Check if an order is #less.
public func isLess(order : Order) : Bool {
Expand Down Expand Up @@ -46,7 +42,7 @@ module {
}
};

public func allValues() : Iter.Iter<Order> {
public func allValues() : Types.Iter<Order> {
todo()
}

Expand Down
3 changes: 2 additions & 1 deletion src/Queue.mo
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import Iter "Iter";
import Immutable "immutable/Queue";
import Order "Order";
import Types "Types";
import { todo } "Debug";

module {

public type Queue<T> = { var immutable : Immutable.Queue<T> };
public type Queue<T> = Types.Queue<T>;

public func freeze<T>(queue : Queue<T>) : Immutable.Queue<T> = queue.immutable;

Expand Down
6 changes: 2 additions & 4 deletions src/Result.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Error handling with the Result type.

import Order "Order";
import Types "Types";

module {

Expand All @@ -18,10 +19,7 @@ module {
/// case (#err(msg)) { Debug.print("Failed to create user with the error: " # msg) };
/// }
/// ```
public type Result<Ok, Err> = {
#ok : Ok;
#err : Err
};
public type Result<Ok, Err> = Types.Result<Ok, Err>;

// Compares two Result's for equality.
public func equal<Ok, Err>(
Expand Down
3 changes: 2 additions & 1 deletion src/Set.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import Immutable "immutable/Set";
import Iter "Iter";
import Order "Order";
import Types "Types";
import { todo } "Debug";

module {

public type Set<T> = { var immutable : Immutable.Set<T> };
public type Set<T> = Types.Set<T>;

public func freeze<T>(set : Set<T>, compare : (T, T) -> Order.Order) : Immutable.Set<T> = set.immutable;

Expand Down
21 changes: 7 additions & 14 deletions src/Stack.mo
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
/// Mutable stack data structure.

import Immutable "immutable/Stack";
import IterType "type/Iter";
import Order "Order";
import Types "Types";
import { todo } "Debug";

module {
type StackNode<T> = {
value : T;
next : ?StackNode<T>
};

public type Stack<T> = {
var top : ?StackNode<T>;
var size : Nat
};
type Node<T> = Types.Stack.Node<T>;
public type Stack<T> = Types.Stack<T>;

public func freeze<T>(stack : Stack<T>) : Immutable.Stack<T> {
todo()
Expand All @@ -36,7 +29,7 @@ module {
};

public func clear<T>(stack : Stack<T>) {
todo();
todo()
};

public func clone<T>(stack : Stack<T>) : Stack<T> { todo() };
Expand Down Expand Up @@ -113,7 +106,7 @@ module {
todo()
};

public func join<T>(stack : IterType.Iter<Stack<T>>) : Stack<T> {
public func join<T>(stack : Types.Iter<Stack<T>>) : Stack<T> {
todo()
};

Expand Down Expand Up @@ -183,11 +176,11 @@ module {
todo()
};

public func values<T>(stack : Stack<T>) : IterType.Iter<T> {
public func values<T>(stack : Stack<T>) : Types.Iter<T> {
todo()
};

public func fromIter<T>(iter : IterType.Iter<T>) : Stack<T> {
public func fromIter<T>(iter : Types.Iter<T>) : Stack<T> {
todo()
};

Expand Down
7 changes: 2 additions & 5 deletions src/Text.mo
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Char "Char";
import Iter "Iter";
import Hash "Hash";
import Stack "Stack";
import Types "Types";
import Prim "mo:⛔";

module {
Expand Down Expand Up @@ -307,11 +308,7 @@ module {
/// let textPattern = #text "phrase";
/// let predicatePattern : Text.Pattern = #predicate (func(c) { c == 'A' or c == 'B' }); // matches "A" or "B"
/// ```
public type Pattern = {
#char : Char;
#text : Text;
#predicate : (Char -> Bool)
};
public type Pattern = Types.Pattern;

private func take(n : Nat, cs : Iter.Iter<Char>) : Iter.Iter<Char> {
var i = n;
Expand Down
Loading

0 comments on commit 1d56879

Please sign in to comment.