git.delta.rocks / jrsonnet / refs/commits / b82846440add

difftreelog

fix lazy array filter impl

sqxrkzuvYaroslav Bolyukin2026-03-23parent: #31c77c8.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
9use jrsonnet_interner::IBytes;9use jrsonnet_interner::IBytes;
10use jrsonnet_ir::Expr;10use jrsonnet_ir::Expr;
1111
12use crate::{function::NativeFn, Context, Result, Thunk, Val};12use crate::{function::NativeFn, typed::IntoUntyped, Context, Result, Thunk, Val};
1313
14mod spec;14mod spec;
15pub use spec::{ArrayLike, *};15pub use spec::{ArrayLike, *};
71 Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))71 Self::new(<MappedArray>::new(self, ArrayMapper::WithIndex(mapper)))
72 }72 }
7373
74 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {74 pub fn filter(self, filter: NativeFn!((Thunk<Val>) -> bool)) -> Result<Self> {
75 // TODO: ArrValue::Picked(inner, indexes) for large arrays75 // TODO: ArrValue::Picked(inner, indexes) for large arrays
76 'eager: {
76 let mut out = Vec::new();77 let mut out = Vec::new();
77 for i in self.iter() {78 for i in self.iter() {
78 let i = i?;79 let Ok(i) = i else {
80 break 'eager;
81 };
79 if filter(&i)? {82 if filter.call(IntoUntyped::into_lazy_untyped(i.clone()))? {
80 out.push(i);83 out.push(i);
81 }84 }
82 }85 }
83 Ok(Self::eager(out))86 return Ok(Self::eager(out));
87 };
88
89 let mut out = Vec::new();
90 for i in self.iter_lazy() {
91 if filter.call(i.clone())? {
92 out.push(i);
93 }
94 }
95 Ok(Self::lazy(out))
84 }96 }
8597
86 pub fn extended(a: Self, b: Self) -> Self {98 pub fn extended(a: Self, b: Self) -> Self {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -126,11 +126,11 @@
 	}
 }
 
-type FilterFunc = NativeFn!((Val) -> bool);
+type FilterFunc = NativeFn!((Thunk<Val>) -> bool);
 
 #[builtin]
 pub fn builtin_filter(func: FilterFunc, arr: ArrValue) -> Result<ArrValue> {
-	arr.filter(|val| func.call(val.clone()))
+	arr.filter(func)
 }
 
 #[builtin]
@@ -139,7 +139,7 @@
 	map_func: NativeFn!((Val) -> Val),
 	arr: ArrValue,
 ) -> Result<ArrValue> {
-	Ok(builtin_filter(filter_func, arr)?.map(map_func))
+	Ok(arr.filter(filter_func)?.map(map_func))
 }
 
 #[builtin]