git.delta.rocks / jrsonnet / refs/commits / 0ae36baea489

difftreelog

perf move mapWithKey to native

Yaroslav Bolyukin2024-05-19parent: #57f709a.patch.diff
in: master

3 files changed

modifiedcrates/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,
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
79 ("slice", builtin_slice::INST),79 ("slice", builtin_slice::INST),
80 ("map", builtin_map::INST),80 ("map", builtin_map::INST),
81 ("mapWithIndex", builtin_map_with_index::INST),81 ("mapWithIndex", builtin_map_with_index::INST),
82 ("mapWithKey", builtin_map_with_key::INST),
82 ("flatMap", builtin_flatmap::INST),83 ("flatMap", builtin_flatmap::INST),
83 ("filter", builtin_filter::INST),84 ("filter", builtin_filter::INST),
84 ("foldl", builtin_foldl::INST),85 ("foldl", builtin_foldl::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -2,12 +2,4 @@
   local std = self,
 
   thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',
-
-  mapWithKey(func, obj)::
-    if !std.isFunction(func) then
-      error ('std.mapWithKey first param must be function, got ' + std.type(func))
-    else if !std.isObject(obj) then
-      error ('std.mapWithKey second param must be object, got ' + std.type(obj))
-    else
-      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
 }