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

Implement basic std functions to manipulate arrays #601

Closed
wants to merge 4 commits into from
Closed
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
27 changes: 27 additions & 0 deletions src/std/array.ab
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,30 @@ pub fun includes(array, value) {
let result = array_first_index(array, value)
return result >= 0
}

/// Returns the last element in the array
pub fun last(array) {
return array[len(array) - 1]
}

/// Removes an element at the index from the array
pub fun remove_at(ref array: [], index: Num): Null {
trust $ unset {nameof array}[{index}] $
}

/// Removes an element at the index from the array, and returns it
pub fun extract_at(ref array, index) {
let elem = array[index]
remove_at(array, index)
return elem
}

/// Removes the last element from the array, and returns it
pub fun pop(ref array) {
return extract_at(array, len(array) - 1)
}

/// Removes the first element from the array, and returns it
pub fun shift(ref array) {
return extract_at(array, 0)
}
12 changes: 12 additions & 0 deletions src/tests/stdlib/array_extract_at.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { extract_at, array_first_index } from "std/array"

main {
let array = ["Failed", "Extracted", "Failed"]

if extract_at(array, 1) == "Extracted"
and len(array) == 2
and array_first_index(array, "Extracted") == -1
{
echo "Succeeded"
}
}
6 changes: 6 additions & 0 deletions src/tests/stdlib/array_last.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { last } from "std/array"

main {
let array = ["Failed", "Not", "Succeeded"]
echo last(array)
}
11 changes: 11 additions & 0 deletions src/tests/stdlib/array_pop.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { pop, array_first_index } from "std/array"

main {
let array = ["Failed", "Failed", "Popped"]
if pop(array) == "Popped"
and len(array) == 2
and array_first_index(array, "Popped") == -1
{
echo "Succeeded"
}
}
9 changes: 9 additions & 0 deletions src/tests/stdlib/array_remove_at.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { remove_at, array_first_index } from "std/array"

main {
let array = ["Failed", "Removed", "Failed"]
remove_at(array, 1)
if len(array) == 2 and array_first_index(array, "Removed") == -1 {
echo "Succeeded"
}
}
11 changes: 11 additions & 0 deletions src/tests/stdlib/array_shift.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { shift, array_first_index } from "std/array"

main {
let array = ["Shifted", "Failed", "Failed"]
if shift(array) == "Shifted"
and len(array) == 2
and array_first_index(array, "Shifted") == -1
{
echo "Succeeded"
}
}
Loading