Skip to content

Commit

Permalink
Add from/to_list and at functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lunagl committed Dec 11, 2023
1 parent 8d00820 commit 0b97893
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 9 deletions.
25 changes: 22 additions & 3 deletions src/glearray.gleam
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
4 changes: 3 additions & 1 deletion src/glearray_ffi.erl
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).
12 changes: 10 additions & 2 deletions src/glearray_ffi.mjs
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];
}
53 changes: 50 additions & 3 deletions test/glearray_test.gleam
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
}

0 comments on commit 0b97893

Please sign in to comment.