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

difftreelog

perf move mapWithKey to native

Yaroslav Bolyukin2024-06-18parent: #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
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
2 local std = self,2 local std = self,
33
4 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',4 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',
5
6 mapWithKey(func, obj)::
7 if !std.isFunction(func) then
8 error ('std.mapWithKey first param must be function, got ' + std.type(func))
9 else if !std.isObject(obj) then
10 error ('std.mapWithKey second param must be object, got ' + std.type(obj))
11 else
12 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
13}5}
146