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
6 runtime_error,6 runtime_error,
7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},7 typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},
8 val::{equals, ArrValue, IndexableVal},8 val::{equals, ArrValue, IndexableVal},
9 Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,9 Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
10};10};
1111
12pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {12pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {
67 arr.map_with_index(func)67 arr.map_with_index(func)
68}68}
69
70#[builtin]
71pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {
72 let mut out = ObjValueBuilder::new();
73 for (k, v) in obj.iter() {
74 let v = v?;
75 out.field(k).value(func.evaluate_simple(&(v,), false)?);
76 }
77 Ok(out.build())
78}
6979
70#[builtin]80#[builtin]
71pub fn builtin_flatmap(81pub fn builtin_flatmap(
modifiedcrates/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),
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) },
 }