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
126 }126 }
127}127}
128128
129type FilterFunc = NativeFn!((Val) -> bool);129type FilterFunc = NativeFn!((Thunk<Val>) -> bool);
130130
131#[builtin]131#[builtin]
132pub fn builtin_filter(func: FilterFunc, arr: ArrValue) -> Result<ArrValue> {132pub fn builtin_filter(func: FilterFunc, arr: ArrValue) -> Result<ArrValue> {
133 arr.filter(|val| func.call(val.clone()))133 arr.filter(func)
134}134}
135135
136#[builtin]136#[builtin]
139 map_func: NativeFn!((Val) -> Val),139 map_func: NativeFn!((Val) -> Val),
140 arr: ArrValue,140 arr: ArrValue,
141) -> Result<ArrValue> {141) -> Result<ArrValue> {
142 Ok(builtin_filter(filter_func, arr)?.map(map_func))142 Ok(arr.filter(filter_func)?.map(map_func))
143}143}
144144
145#[builtin]145#[builtin]