From ba8ec1910bf2527eb290c128a04c23f42ec6a3bb Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 9 Oct 2022 12:59:23 -0400 Subject: feat(tvix/eval): Implement builtins.foldl' Change-Id: Ibc97db4343cb3a1a1677f69fb6c3518c61978aad Reviewed-on: https://cl.tvl.fyi/c/depot/+/6906 Autosubmit: grfn Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 18 ++++++++++++++++++ tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix | 5 +++++ 3 files changed, 24 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 93b04bb37b44..f32a29a20660 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -222,6 +222,24 @@ fn pure_builtins() -> Vec { .map(|list| Value::List(NixList::from(list))) .map_err(Into::into) }), + Builtin::new( + "foldl'", + &[true, false, true], + |mut args: Vec, vm: &mut VM| { + let list = args.pop().unwrap().to_list()?; + let mut res = args.pop().unwrap(); + let op = args.pop().unwrap(); + for val in list { + val.force(vm)?; + vm.push(val); + vm.push(res); + let partial = vm.call_value(&op)?; + res = vm.call_value(&partial)?; + } + + Ok(res) + }, + ), Builtin::new("genList", &[true, true], |args: Vec, vm: &mut VM| { let len = args[1].as_int()?; (0..len) diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp new file mode 100644 index 000000000000..8d683a20fab7 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp @@ -0,0 +1 @@ +[ 6 [ 0 1 2 3 ] 2 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix new file mode 100644 index 000000000000..44c0349387ff --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix @@ -0,0 +1,5 @@ +[ + (builtins.foldl' builtins.add 0 [1 2 3]) + (builtins.foldl' (l1: l2: l1 ++ l2) [0] [[1] [2 3]]) + (builtins.foldl' (x: y: if x == 0 then y else x * y) 0 [1 2]) +] -- cgit 1.4.1