Skip to content

Commit

Permalink
Add Array.size (#494)
Browse files Browse the repository at this point in the history
This aims to address https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346 and other instances of people asking about how to get the size of an array due to it being omitted from the generated documentation.

Co-authored-by: Paul Young <84700+paulyoung@users.noreply.github.com>
  • Loading branch information
ggreif and paulyoung committed Jan 5, 2023
1 parent 66a682a commit 823b0b4
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Array.mo
Original file line number Diff line number Diff line change
Expand Up @@ -697,5 +697,21 @@ module {
/// Runtime: O(1)
///
/// Space: O(1)
public func keys<X>(array : [X]) : I.Iter<Nat> = array.keys()
public func keys<X>(array : [X]) : I.Iter<Nat> = array.keys();

/// Returns the size of `array`.
///
/// NOTE: You can also use `array.size()` instead of this function. See example
/// below.
///
/// ```motoko include=import
///
/// let array = [10, 11, 12];
/// let size = Array.size(array);
/// ```
///
/// Runtime: O(1)
///
/// Space: O(1)
public func size<X>(array : [X]) : Nat = array.size()
}

0 comments on commit 823b0b4

Please sign in to comment.