Skip to content

Commit

Permalink
Add set function
Browse files Browse the repository at this point in the history
  • Loading branch information
lunagl committed Dec 11, 2023
1 parent 0b97893 commit f3068b7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/glearray.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn to_list(array: Array(a)) -> List(a)
pub fn length(of array: Array(a)) -> Int

pub fn at(in array: Array(a), get index: Int) -> Result(a, Nil) {
case index >= 0 && index < length(array) {
case is_valid_index(array, index) {
True -> Ok(do_at(array, index))
False -> Error(Nil)
}
Expand All @@ -26,3 +26,22 @@ pub fn at(in array: Array(a), get index: Int) -> Result(a, Nil) {
@external(erlang, "glearray_ffi", "at")
@external(javascript, "./glearray_ffi.mjs", "at")
fn do_at(array: Array(a), index: Int) -> a

pub fn set(
in array: Array(a),
at index: Int,
value value: a,
) -> Result(Array(a), Nil) {
case is_valid_index(array, index) {
True -> Ok(do_set(array, index, value))
False -> Error(Nil)
}
}

@external(erlang, "glearray_ffi", "set")
@external(javascript, "./glearray_ffi.mjs", "set")
fn do_set(array: Array(a), index: Int, value: a) -> Array(a)

fn is_valid_index(array: Array(a), index: Int) -> Bool {
index >= 0 && index < length(array)
}
4 changes: 3 additions & 1 deletion src/glearray_ffi.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
-module(glearray_ffi).

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

new() -> {}.

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

set(Array, Index, Value) -> setelement(Index + 1, Array, Value).
6 changes: 6 additions & 0 deletions src/glearray_ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export function arrayLength(array) {
export function at(array, index) {
return array[index];
}

export function set(array, index, value) {
const copy = [...array];
copy[index] = value;
return copy;
}
22 changes: 21 additions & 1 deletion test/glearray_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn from_to_list_test() {
}

pub fn at_test() {
let list = ["a", "B", "fdsau", "rh3892wfd", "äèëå亣", "Another ONE!"]
let list = ["a", "B", "fdsau", "rh3892wfd", "äèëå亣"]
let array = glearray.from_list(list)
list.index_map(
list,
Expand All @@ -44,6 +44,26 @@ pub fn at_test() {
|> should.be_error
glearray.at(array, 100)
|> should.be_error
glearray.at(glearray.new(), 0)
|> should.be_error
}

pub fn set_test() {
let array = glearray.from_list([1, 2, 3, 4])
let assert Ok(modified) = glearray.set(in: array, at: 1, value: 10)
array
|> glearray.to_list
|> should.equal([1, 2, 3, 4])
modified
|> glearray.to_list
|> should.equal([1, 10, 3, 4])

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

fn assert_empty(array: Array(a)) -> Array(a) {
Expand Down

0 comments on commit f3068b7

Please sign in to comment.