Skip to content

Commit

Permalink
Add reverse function to stack module
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuhan committed Mar 11, 2024
1 parent e590dcb commit 757ddd2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions stack/stack.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,29 @@ test "forall" {
)?
}

/// Checks if all elements of the list satisfy the predicate f.
/// If the list is empty, return true.
///
/// NOTE: Since the current standard library List lacks the forall function,
/// this function internally implements the forall function of List.
///
/// # Example
///
/// ```
/// println(Stack::[2, 4, 6].reverse())
/// // output: Cons(2, Cons(4, Cons(6, Nil)))
/// ```
pub fn reverse[T](self : Stack[T]) -> Stack[T] {
{ elements: self.elements.reverse(), len: self.len }
}

test "reverse" {
@assertion.assert_eq(
Stack::[2, 4, 6].reverse().elements,
Cons(2, Cons(4, Cons(6, Nil))),
)?
}

/// Compare two stacks.
///
/// NOTE: Since the current standard library List lacks the equal or op_equal function,
Expand Down

0 comments on commit 757ddd2

Please sign in to comment.