From 939b2194fe6b0159191892d7bfc7ff527c1b0371 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 2 Oct 2022 19:41:39 +0300 Subject: feat(tvix/eval): implement builtins.concatLists Concatenates (but not flattens) a list of lists. Change-Id: I692e0b3e7b5a5ff93d5768d3a27849b432ec5747 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6843 Autosubmit: tazjin Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 14 ++++++++++++++ .../tests/tvix_tests/eval-okay-builtins-concat-lists.exp | 1 + .../tests/tvix_tests/eval-okay-builtins-concat-lists.nix | 6 ++++++ 3 files changed, 21 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.nix (limited to 'tvix/eval') diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 45630819e124..94a035c2ece7 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -116,6 +116,20 @@ fn pure_builtins() -> Vec { std::cmp::Ordering::Greater => Ok(Value::Integer(1)), } }), + Builtin::new("concatLists", &[true], |args, vm| { + let list = args[0].to_list()?; + let lists = list + .into_iter() + .map(|elem| { + let value = elem.force(vm)?; + value.to_list() + }) + .collect::, ErrorKind>>()?; + + Ok(Value::List(NixList::from( + lists.into_iter().flatten().collect::>(), + ))) + }), Builtin::new( "div", &[false, false], diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.exp new file mode 100644 index 000000000000..64ae529ac257 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.exp @@ -0,0 +1 @@ +[ [ ] [ 1 2 3 4 5 6 ] [ [ 1 ] [ 2 ] [ 3 ] ] [ 1 2 3 4 5 6 ] ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.nix new file mode 100644 index 000000000000..19ef5eba1145 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-concat-lists.nix @@ -0,0 +1,6 @@ +[ + (builtins.concatLists [ ]) + (builtins.concatLists [ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]) + (builtins.concatLists [ [ [ 1 ] [ 2 ] ] [ [ 3 ] ] [ ] ]) + (builtins.concatLists [ [ 1 2 ] [ ] [ 3 4 ] [ ] [ 5 6 ] ]) +] -- cgit 1.4.1