Skip to content

Commit

Permalink
Merge branch 'main' into int-nat-conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanasa authored Jan 30, 2025
2 parents c40d66b + 26fdb1c commit cdd308d
Show file tree
Hide file tree
Showing 21 changed files with 1,996 additions and 200 deletions.
301 changes: 241 additions & 60 deletions src/Array.mo

Large diffs are not rendered by default.

62 changes: 60 additions & 2 deletions src/Int.mo
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,70 @@ module {
/// as a function value at the moment.
public func pow(x : Int, y : Int) : Int { x ** y };

/// Returns an iterator over the integers from the first to second argument with an exclusive upper bound.
/// ```motoko
/// import Iter "mo:base/Iter";
///
/// let iter = Int.range(1, 4);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko
/// import Iter "mo:base/Iter";
///
/// let iter = Int.range(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func range(fromInclusive : Int, toExclusive : Int) : Iter.Iter<Int> {
todo()
object {
var n = fromInclusive;
public func next() : ?Int {
if (n >= toExclusive) {
null
} else {
let result = n;
n += 1;
?result
}
}
}
};

/// Returns an iterator over the integers from the first to second argument, inclusive.
/// ```motoko
/// import Iter "mo:base/Iter";
///
/// let iter = Int.rangeInclusive(1, 3);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko
/// import Iter "mo:base/Iter";
///
/// let iter = Int.rangeInclusive(3, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func rangeInclusive(from : Int, to : Int) : Iter.Iter<Int> {
todo()
object {
var n = from;
public func next() : ?Int {
if (n > to) {
null
} else {
let result = n;
n += 1;
?result
}
}
}
};

}
87 changes: 84 additions & 3 deletions src/Int16.mo
Original file line number Diff line number Diff line change
Expand Up @@ -642,16 +642,97 @@ module {
/// as a function value at the moment.
public func powWrap(x : Int16, y : Int16) : Int16 { x **% y };

/// Returns an iterator over `Int16` values from the first to second argument with an exclusive upper bound.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int16.range(1, 4);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int16.range(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func range(fromInclusive : Int16, toExclusive : Int16) : Iter.Iter<Int16> {
todo()
if (fromInclusive >= toExclusive) {
Iter.empty()
} else {
object {
var n = fromInclusive;
public func next() : ?Int16 {
if (n == toExclusive) {
null
} else {
let result = n;
n += 1;
?result
}
}
}
}
};

/// Returns an iterator over `Int16` values from the first to second argument, inclusive.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int16.rangeInclusive(1, 3);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int16.rangeInclusive(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func rangeInclusive(from : Int16, to : Int16) : Iter.Iter<Int16> {
todo()
if (from > to) {
Iter.empty()
} else {
object {
var n = from;
var done = false;
public func next() : ?Int16 {
if (done) {
null
} else {
let result = n;
if (n == to) {
done := true
} else {
n += 1
};
?result
}
}
}
}
};

/// Returns an iterator over all Int16 values, from minValue to maxValue.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int16.allValues();
/// assert(?-32_768 == iter.next());
/// assert(?-32_767 == iter.next());
/// assert(?-32_766 == iter.next());
/// // ...
/// ```
public func allValues() : Iter.Iter<Int16> {
todo()
rangeInclusive(minValue, maxValue)
};

}
87 changes: 84 additions & 3 deletions src/Int32.mo
Original file line number Diff line number Diff line change
Expand Up @@ -652,16 +652,97 @@ module {
/// as a function value at the moment.
public func powWrap(x : Int32, y : Int32) : Int32 { x **% y };

/// Returns an iterator over `Int32` values from the first to second argument with an exclusive upper bound.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int32.range(1, 4);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int32.range(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func range(fromInclusive : Int32, toExclusive : Int32) : Iter.Iter<Int32> {
todo()
if (fromInclusive >= toExclusive) {
Iter.empty()
} else {
object {
var n = fromInclusive;
public func next() : ?Int32 {
if (n == toExclusive) {
null
} else {
let result = n;
n += 1;
?result
}
}
}
}
};

/// Returns an iterator over `Int32` values from the first to second argument, inclusive.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int32.rangeInclusive(1, 3);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int32.rangeInclusive(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func rangeInclusive(from : Int32, to : Int32) : Iter.Iter<Int32> {
todo()
if (from > to) {
Iter.empty()
} else {
object {
var n = from;
var done = false;
public func next() : ?Int32 {
if (done) {
null
} else {
let result = n;
if (n == to) {
done := true
} else {
n += 1
};
?result
}
}
}
}
};

/// Returns an iterator over all Int32 values, from minValue to maxValue.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int32.allValues();
/// assert(?-2_147_483_648 == iter.next());
/// assert(?-2_147_483_647 == iter.next());
/// assert(?-2_147_483_646 == iter.next());
/// // ...
/// ```
public func allValues() : Iter.Iter<Int32> {
todo()
rangeInclusive(minValue, maxValue)
};

}
87 changes: 84 additions & 3 deletions src/Int64.mo
Original file line number Diff line number Diff line change
Expand Up @@ -639,16 +639,97 @@ module {
/// as a function value at the moment.
public func powWrap(x : Int64, y : Int64) : Int64 { x **% y };

/// Returns an iterator over `Int64` values from the first to second argument with an exclusive upper bound.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int64.range(1, 4);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int64.range(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func range(fromInclusive : Int64, toExclusive : Int64) : Iter.Iter<Int64> {
todo()
if (fromInclusive >= toExclusive) {
Iter.empty()
} else {
object {
var n = fromInclusive;
public func next() : ?Int64 {
if (n == toExclusive) {
null
} else {
let result = n;
n += 1;
?result
}
}
}
}
};

/// Returns an iterator over `Int64` values from the first to second argument, inclusive.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int64.rangeInclusive(1, 3);
/// assert(?1 == iter.next());
/// assert(?2 == iter.next());
/// assert(?3 == iter.next());
/// assert(null == iter.next());
/// ```
///
/// If the first argument is greater than the second argument, the function returns an empty iterator.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int64.rangeInclusive(4, 1);
/// assert(null == iter.next()); // empty iterator
/// ```
public func rangeInclusive(from : Int64, to : Int64) : Iter.Iter<Int64> {
todo()
if (from > to) {
Iter.empty()
} else {
object {
var n = from;
var done = false;
public func next() : ?Int64 {
if (done) {
null
} else {
let result = n;
if (n == to) {
done := true
} else {
n += 1
};
?result
}
}
}
}
};

/// Returns an iterator over all Int64 values, from minValue to maxValue.
/// ```motoko include=import
/// import Iter "mo:base/Iter";
///
/// let iter = Int64.allValues();
/// assert(?-9_223_372_036_854_775_808 == iter.next());
/// assert(?-9_223_372_036_854_775_807 == iter.next());
/// assert(?-9_223_372_036_854_775_806 == iter.next());
/// // ...
/// ```
public func allValues() : Iter.Iter<Int64> {
todo()
rangeInclusive(minValue, maxValue)
};

}
Loading

0 comments on commit cdd308d

Please sign in to comment.