difftreelog
perf move mapWithKey to native
in: master
3 files changed
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -6,7 +6,7 @@
runtime_error,
typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
val::{equals, ArrValue, IndexableVal},
- Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,
+ Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};
pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
@@ -68,6 +68,16 @@
}
#[builtin]
+pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {
+ let mut out = ObjValueBuilder::new();
+ for (k, v) in obj.iter() {
+ let v = v?;
+ out.field(k).value(func.evaluate_simple(&(v,), false)?);
+ }
+ Ok(out.build())
+}
+
+#[builtin]
pub fn builtin_flatmap(
func: NativeFn<((Either![String, Val],), Val)>,
arr: IndexableVal,
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -79,6 +79,7 @@
("slice", builtin_slice::INST),
("map", builtin_map::INST),
("mapWithIndex", builtin_map_with_index::INST),
+ ("mapWithKey", builtin_map_with_key::INST),
("flatMap", builtin_flatmap::INST),
("filter", builtin_filter::INST),
("foldl", builtin_foldl::INST),
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth