feat(tvix/eval): Implement builtins.foldl'
Change-Id: Ibc97db4343cb3a1a1677f69fb6c3518c61978aad Reviewed-on: https://cl.tvl.fyi/c/depot/+/6906 Autosubmit: grfn <grfn@gws.fyi> Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
This commit is contained in:
parent
0e97555644
commit
ba8ec1910b
3 changed files with 24 additions and 0 deletions
|
@ -222,6 +222,24 @@ fn pure_builtins() -> Vec<Builtin> {
|
|||
.map(|list| Value::List(NixList::from(list)))
|
||||
.map_err(Into::into)
|
||||
}),
|
||||
Builtin::new(
|
||||
"foldl'",
|
||||
&[true, false, true],
|
||||
|mut args: Vec<Value>, 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<Value>, vm: &mut VM| {
|
||||
let len = args[1].as_int()?;
|
||||
(0..len)
|
||||
|
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-foldl.exp
Normal file
|
@ -0,0 +1 @@
|
|||
[ 6 [ 0 1 2 3 ] 2 ]
|
5
tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix
Normal file
5
tvix/eval/src/tests/tvix_tests/eval-okay-foldl.nix
Normal file
|
@ -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])
|
||||
]
|
Loading…
Reference in a new issue