-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
pub type Array(a) | ||
|
||
@external(erlang, "glearray_ffi", "new") | ||
@external(javascript, "./glearray_ffi.mjs", "new_array") | ||
@external(javascript, "./glearray_ffi.mjs", "newArray") | ||
pub fn new() -> Array(a) | ||
|
||
@external(erlang, "erlang", "list_to_tuple") | ||
@external(javascript, "./glearray_ffi.mjs", "fromList") | ||
pub fn from_list(list: List(a)) -> Array(a) | ||
|
||
@external(erlang, "erlang", "tuple_to_list") | ||
@external(javascript, "./gleam.mjs", "toList") | ||
pub fn to_list(array: Array(a)) -> List(a) | ||
|
||
@external(erlang, "erlang", "tuple_size") | ||
@external(javascript, "./glearray_ffi.mjs", "array_length") | ||
pub fn length(array: Array(a)) -> Int | ||
@external(javascript, "./glearray_ffi.mjs", "arrayLength") | ||
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) { | ||
True -> Ok(do_at(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
-module(glearray_ffi). | ||
|
||
-export([new/0]). | ||
-export([new/0, at/2]). | ||
|
||
new() -> {}. | ||
|
||
at(Array, Index) -> element(Index + 1, Array). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
export function new_array() { | ||
export function newArray() { | ||
return []; | ||
} | ||
|
||
export function array_length(array) { | ||
export function fromList(list) { | ||
return list.toArray(); | ||
} | ||
|
||
export function arrayLength(array) { | ||
return array.length; | ||
} | ||
|
||
export function at(array, index) { | ||
return array[index]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,60 @@ | ||
import gleam/list | ||
import gleeunit | ||
import gleeunit/should | ||
import glearray | ||
import glearray.{type Array} | ||
|
||
pub fn main() { | ||
gleeunit.main() | ||
} | ||
|
||
pub fn empty_array_test() { | ||
glearray.new() | ||
assert_empty(glearray.new()) | ||
} | ||
|
||
pub fn from_to_list_test() { | ||
[] | ||
|> glearray.from_list | ||
|> assert_empty | ||
|> glearray.to_list | ||
|> should.equal([]) | ||
|
||
let list = [1, 2, 3, -8, 8954] | ||
list | ||
|> glearray.from_list | ||
|> assert_length(5) | ||
|> glearray.to_list | ||
|> should.equal(list) | ||
} | ||
|
||
pub fn at_test() { | ||
let list = ["a", "B", "fdsau", "rh3892wfd", "äèëå亣", "Another ONE!"] | ||
let array = glearray.from_list(list) | ||
list.index_map( | ||
list, | ||
fn(index, element) { | ||
array | ||
|> glearray.at(index) | ||
|> should.equal(Ok(element)) | ||
}, | ||
) | ||
|
||
glearray.at(array, -1) | ||
|> should.be_error | ||
glearray.at(array, glearray.length(array)) | ||
|> should.be_error | ||
glearray.at(array, 100) | ||
|> should.be_error | ||
} | ||
|
||
fn assert_empty(array: Array(a)) -> Array(a) { | ||
assert_length(array, 0) | ||
should.equal(array, glearray.new()) | ||
array | ||
} | ||
|
||
fn assert_length(array: Array(a), length: Int) -> Array(a) { | ||
array | ||
|> glearray.length | ||
|> should.equal(0) | ||
|> should.equal(length) | ||
array | ||
} |