feat(tvix/eval): implement builtins.partition

Change-Id: I8b591f3057c68c1542046fc5a771973f2238c9df
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7020
Autosubmit: j4m3s <james.landrein@gmail.com>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
James Landrein 2022-10-14 13:49:20 +02:00 committed by clbot
parent 4dcb8f38c2
commit 5ee2258692
3 changed files with 39 additions and 0 deletions

View file

@ -446,6 +446,31 @@ fn pure_builtins() -> Vec<Builtin> {
("version".into(), version),
]))))
}),
Builtin::new(
"partition",
&[true, true],
|args: Vec<Value>, vm: &mut VM| {
let mut right: Vec<Value> = vec![];
let mut wrong: Vec<Value> = 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<NixString, Value> = 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],

View file

@ -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 ] ]; } ]

View file

@ -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 ]
])
]