From 3a67a140093335e93cee560c1d2f9873035ffa95 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Tue, 6 Sep 2022 11:58:28 -0700 Subject: feat(tvix/eval): Support builtins.elemAt (Attempt to) index into a list. Change-Id: I3592e60a79e64d265e34100d4062041b0b410e00 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6551 Reviewed-by: wpcarro Reviewed-by: tazjin Autosubmit: wpcarro Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'tvix/eval/src/builtins') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 149669015b..3f8b73f7d7 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -140,6 +140,20 @@ fn pure_builtins() -> Vec { let a = args.pop().unwrap(); arithmetic_op!(a, b, /) }), + Builtin::new("elemAt", 2, |args, vm| { + force!(vm, &args[0], value, { + let xs = value.to_list()?; + let i = args[1].as_int()?; + if i < 0 { + Err(ErrorKind::IndexOutOfBounds { index: i }) + } else { + match xs.get(i as usize) { + Some(x) => Ok(x.clone()), + None => Err(ErrorKind::IndexOutOfBounds { index: i }), + } + } + }) + }), Builtin::new("length", 1, |args, vm| { if let Value::Thunk(t) = &args[0] { t.force(vm)?; -- cgit 1.4.1