From 5ee2258692da4d5c65b22416fcc3ec87db013ff8 Mon Sep 17 00:00:00 2001 From: James Landrein Date: Fri, 14 Oct 2022 13:49:20 +0200 Subject: feat(tvix/eval): implement builtins.partition Change-Id: I8b591f3057c68c1542046fc5a771973f2238c9df Reviewed-on: https://cl.tvl.fyi/c/depot/+/7020 Autosubmit: j4m3s Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 25 ++++++++++++++++++++++ .../tvix_tests/eval-okay-builtins-partition.exp | 1 + .../tvix_tests/eval-okay-builtins-partition.nix | 13 +++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.nix diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 03466af65f89..e9ad4a8ee82e 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -446,6 +446,31 @@ fn pure_builtins() -> Vec { ("version".into(), version), ])))) }), + Builtin::new( + "partition", + &[true, true], + |args: Vec, vm: &mut VM| { + let mut right: Vec = vec![]; + let mut wrong: Vec = vec![]; + + let list: NixList = args[1].to_list()?; + for elem in list.into_iter() { + let result = vm.call_with(&args[0], [elem.clone()])?; + + if result.force(vm)?.as_bool()? { + right.push(elem); + } else { + wrong.push(elem); + }; + } + + let mut res: BTreeMap = BTreeMap::new(); + res.insert("right".into(), Value::List(right.into())); + res.insert("wrong".into(), Value::List(wrong.into())); + + Ok(Value::attrs(NixAttrs::from_map(res))) + }, + ), Builtin::new( "removeAttrs", &[true, true], diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.exp new file mode 100644 index 000000000000..d2390db4f528 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.exp @@ -0,0 +1 @@ +[ { right = [ 1 2 3 4 5 ]; wrong = [ ]; } { right = [ ]; wrong = [ 1 2 3 4 5 ]; } { right = [ 2 ]; wrong = [ 1 3 4 5 ]; } { right = [ [ 1 2 ] [ 3 4 ] ]; wrong = [ [ 1 ] [ 2 ] [ 3 ] ]; } ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.nix new file mode 100644 index 000000000000..0587330ff917 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-partition.nix @@ -0,0 +1,13 @@ +[ + (builtins.partition (_: true) [ 1 2 3 4 5]) + (builtins.partition (_: false) [ 1 2 3 4 5]) + (builtins.partition (x: x == 2) [ 1 2 3 4 5]) + + (builtins.partition (x: (builtins.length x) > 1) [ + [ 1 ] + [ 1 2 ] + [ 2 ] + [ 3 ] + [ 3 4 ] + ]) +] -- cgit 1.4.1