aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-08-29 15:28:05 +0100
committerLouis Pilfold <louis@lpil.uk>2021-08-29 15:28:05 +0100
commitd7cf6c9a27487b182c1e6d832a58e01d022b97fa (patch)
treea97b848a5ac4b98ca844ba9f58492bd71603204b /src
parentefc54b447788d325ec38222d3cb1ec5e98c1c030 (diff)
downloadjavascript-d7cf6c9a27487b182c1e6d832a58e01d022b97fa.tar.gz
javascript-d7cf6c9a27487b182c1e6d832a58e01d022b97fa.zip
Array index
Diffstat (limited to 'src')
-rw-r--r--src/ffi.js6
-rw-r--r--src/gleam/javascript/array.gleam5
2 files changed, 11 insertions, 0 deletions
diff --git a/src/ffi.js b/src/ffi.js
index 81362bb..c0b2076 100644
--- a/src/ffi.js
+++ b/src/ffi.js
@@ -1,3 +1,5 @@
+import { Ok, Error } from "./gleam.js";
+
export function toArray(list) {
return list.toArray();
}
@@ -17,3 +19,7 @@ export function reduce(thing, acc, fn) {
export function reduceRight(thing, acc, fn) {
return thing.reduceRight(fn, acc);
}
+
+export function index(thing, index) {
+ return index in thing ? new Ok(thing[index]) : new Error(undefined);
+}
diff --git a/src/gleam/javascript/array.gleam b/src/gleam/javascript/array.gleam
index 0895ff1..4b59b85 100644
--- a/src/gleam/javascript/array.gleam
+++ b/src/gleam/javascript/array.gleam
@@ -1,3 +1,5 @@
+// TODO: docs
+// TODO: labels
pub external type Array(element)
pub external fn to_list(Array(element)) -> List(element) =
@@ -17,3 +19,6 @@ pub external fn fold(Array(e), a, fn(a, e) -> a) -> a =
pub external fn fold_right(Array(e), a, fn(a, e) -> a) -> a =
"../../ffi.js" "reduceRight"
+
+pub external fn get(Array(e), Int) -> Result(e, Nil) =
+ "../../ffi.js" "index"