Skip to content

Commit

Permalink
Rename at to get
Browse files Browse the repository at this point in the history
  • Loading branch information
lunagl committed Dec 11, 2023
1 parent 3f14909 commit 0354df6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/glearray.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,25 @@ pub fn length(of array: Array(a)) -> Int
/// ## Examples
///
/// ```gleam
/// > from_list([5, 6, 7]) |> at(1)
/// > from_list([5, 6, 7]) |> get(1)
/// Ok(6)
/// ```
///
/// ```gleam
/// > from_list([5, 6, 7]) |> at(3)
/// > from_list([5, 6, 7]) |> get(3)
/// Error(Nil)
/// ```
///
pub fn at(in array: Array(a), get index: Int) -> Result(a, Nil) {
pub fn get(in array: Array(a), at index: Int) -> Result(a, Nil) {
case is_valid_index(array, index) {
True -> Ok(do_at(array, index))
True -> Ok(do_get(array, index))
False -> Error(Nil)
}
}

@external(erlang, "glearray_ffi", "at")
@external(javascript, "./glearray_ffi.mjs", "at")
fn do_at(array: Array(a), index: Int) -> a
@external(erlang, "glearray_ffi", "get")
@external(javascript, "./glearray_ffi.mjs", "get")
fn do_get(array: Array(a), index: Int) -> a

/// Replaces the element at the given index with `value`.
///
Expand Down Expand Up @@ -205,7 +205,7 @@ fn do_insert(array: Array(a), index: Int, value: a) -> Array(a)
///
pub fn iterate(array: Array(a)) -> Iterator(a) {
use index <- iterator.unfold(from: 0)
case at(array, index) {
case get(array, index) {
Ok(element) -> iterator.Next(element, index + 1)
Error(_) -> iterator.Done
}
Expand Down
4 changes: 2 additions & 2 deletions src/glearray_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-module(glearray_ffi).

-export([new/0, at/2, set/3, insert/3]).
-export([new/0, get/2, set/3, insert/3]).

new() -> {}.

at(Array, Index) -> element(Index + 1, Array).
get(Array, Index) -> element(Index + 1, Array).

set(Array, Index, Value) -> setelement(Index + 1, Array, Value).

Expand Down
2 changes: 1 addition & 1 deletion src/glearray_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function arrayLength(array) {
return array.length;
}

export function at(array, index) {
export function get(array, index) {
return array[index];
}

Expand Down
12 changes: 6 additions & 6 deletions test/glearray_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ pub fn from_to_list_test() {
|> should.equal(list)
}

pub fn at_test() {
pub fn get_test() {
let list = ["a", "B", "fdsau", "rh3892wfd", "äèëå亣"]
let array = glearray.from_list(list)
list.index_map(
list,
fn(index, element) {
array
|> glearray.at(index)
|> glearray.get(index)
|> should.equal(Ok(element))
},
)

glearray.at(array, -1)
glearray.get(array, -1)
|> should.be_error
glearray.at(array, glearray.length(array))
glearray.get(array, glearray.length(array))
|> should.be_error
glearray.at(array, 100)
glearray.get(array, 100)
|> should.be_error
glearray.at(glearray.new(), 0)
glearray.get(glearray.new(), 0)
|> should.be_error
}

Expand Down

0 comments on commit 0354df6

Please sign in to comment.