git.delta.rocks / jrsonnet / refs/commits / a8186ce96137

difftreelog

perf move mapWithIndex to native

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

5 files changed

modifiedcrates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -54,7 +54,12 @@
 
 	#[must_use]
 	pub fn map(self, mapper: FuncVal) -> Self {
-		Self::new(MappedArray::new(self, mapper))
+		Self::new(<MappedArray<false>>::new(self, mapper))
+	}
+
+	#[must_use]
+	pub fn map_with_index(self, mapper: FuncVal) -> Self {
+		Self::new(<MappedArray<true>>::new(self, mapper))
 	}
 
 	pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {
modifiedcrates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -430,12 +430,12 @@
 }
 
 #[derive(Trace, Debug, Clone)]
-pub struct MappedArray {
+pub struct MappedArray<const WithIndex: bool> {
 	inner: ArrValue,
 	cached: Cc<RefCell<Vec<ArrayThunk<()>>>>,
 	mapper: FuncVal,
 }
-impl MappedArray {
+impl<const WithIndex: bool> MappedArray<WithIndex> {
 	pub fn new(inner: ArrValue, mapper: FuncVal) -> Self {
 		let len = inner.len();
 		Self {
@@ -444,8 +444,15 @@
 			mapper,
 		}
 	}
+	fn evaluate(&self, index: usize, value: Val) -> Result<Val> {
+		if WithIndex {
+			self.mapper.evaluate_simple(&(index, value), false)
+		} else {
+			self.mapper.evaluate_simple(&(value,), false)
+		}
+	}
 }
-impl ArrayLike for MappedArray {
+impl<const WithIndex: bool> ArrayLike for MappedArray<WithIndex> {
 	fn len(&self) -> usize {
 		self.cached.borrow().len()
 	}
@@ -472,7 +479,7 @@
 			.get(index)
 			.transpose()
 			.expect("index checked")
-			.and_then(|r| self.mapper.evaluate_simple(&(r,), false));
+			.and_then(|r| self.evaluate(index, r));
 
 		let new_value = match val {
 			Ok(v) => v,
@@ -486,12 +493,12 @@
 	}
 	fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
 		#[derive(Trace)]
-		struct ArrayElement {
-			arr_thunk: MappedArray,
+		struct ArrayElement<const WithIndex: bool> {
+			arr_thunk: MappedArray<WithIndex>,
 			index: usize,
 		}
 
-		impl ThunkValue for ArrayElement {
+		impl<const WithIndex: bool> ThunkValue for ArrayElement<WithIndex> {
 			type Output = Val;
 
 			fn get(self: Box<Self>) -> Result<Self::Output> {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_flatmap(66	func: NativeFn<((Either![String, Val],), Val)>,67	arr: IndexableVal,68) -> Result<IndexableVal> {69	use std::fmt::Write;70	match arr {71		IndexableVal::Str(str) => {72			let mut out = String::new();73			for c in str.chars() {74				match func(Either2::A(c.to_string()))? {75					Val::Str(o) => write!(out, "{o}").unwrap(),76					Val::Null => continue,77					_ => bail!("in std.join all items should be strings"),78				};79			}80			Ok(IndexableVal::Str(out.into()))81		}82		IndexableVal::Arr(a) => {83			let mut out = Vec::new();84			for el in a.iter() {85				let el = el?;86				match func(Either2::B(el))? {87					Val::Arr(o) => {88						for oe in o.iter() {89							out.push(oe?);90						}91					}92					Val::Null => continue,93					_ => bail!("in std.join all items should be arrays"),94				};95			}96			Ok(IndexableVal::Arr(out.into()))97		}98	}99}100101#[builtin]102pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {103	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))104}105106#[builtin]107pub fn builtin_filter_map(108	filter_func: FuncVal,109	map_func: FuncVal,110	arr: ArrValue,111) -> Result<ArrValue> {112	Ok(builtin_filter(filter_func, arr)?.map(map_func))113}114115#[builtin]116pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {117	let mut acc = init;118	for i in arr.iter() {119		acc = func.evaluate_simple(&(acc, i?), false)?;120	}121	Ok(acc)122}123124#[builtin]125pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {126	let mut acc = init;127	for i in arr.iter().rev() {128		acc = func.evaluate_simple(&(i?, acc), false)?;129	}130	Ok(acc)131}132133#[builtin]134pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {135	if to < from {136		return Ok(ArrValue::empty());137	}138	Ok(ArrValue::range_inclusive(from, to))139}140141#[builtin]142pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {143	use std::fmt::Write;144	Ok(match sep {145		IndexableVal::Arr(joiner_items) => {146			let mut out = Vec::new();147148			let mut first = true;149			for item in arr.iter() {150				let item = item?.clone();151				if let Val::Arr(items) = item {152					if !first {153						out.reserve(joiner_items.len());154						// TODO: extend155						for item in joiner_items.iter() {156							out.push(item?);157						}158					}159					first = false;160					out.reserve(items.len());161					for item in items.iter() {162						out.push(item?);163					}164				} else if matches!(item, Val::Null) {165					continue;166				} else {167					bail!("in std.join all items should be arrays");168				}169			}170171			IndexableVal::Arr(out.into())172		}173		IndexableVal::Str(sep) => {174			let mut out = String::new();175176			let mut first = true;177			for item in arr.iter() {178				let item = item?.clone();179				if let Val::Str(item) = item {180					if !first {181						out += &sep;182					}183					first = false;184					write!(out, "{item}").unwrap();185				} else if matches!(item, Val::Null) {186					continue;187				} else {188					bail!("in std.join all items should be strings");189				}190			}191192			IndexableVal::Str(out.into())193		}194	})195}196197#[builtin]198pub fn builtin_reverse(arr: ArrValue) -> ArrValue {199	arr.reversed()200}201202#[builtin]203pub fn builtin_any(arr: ArrValue) -> Result<bool> {204	for v in arr.iter() {205		let v = bool::from_untyped(v?)?;206		if v {207			return Ok(true);208		}209	}210	Ok(false)211}212213#[builtin]214pub fn builtin_all(arr: ArrValue) -> Result<bool> {215	for v in arr.iter() {216		let v = bool::from_untyped(v?)?;217		if !v {218			return Ok(false);219		}220	}221	Ok(true)222}223224#[builtin]225pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {226	match arr {227		IndexableVal::Str(str) => {228			let x: IStr = IStr::from_untyped(x)?;229			Ok(!x.is_empty() && str.contains(&*x))230		}231		IndexableVal::Arr(a) => {232			for item in a.iter() {233				let item = item?;234				if equals(&item, &x)? {235					return Ok(true);236				}237			}238			Ok(false)239		}240	}241}242243#[builtin]244pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {245	builtin_member(arr, elem)246}247248#[builtin]249pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {250	let mut count = 0;251	for item in arr.iter() {252		if equals(&item?, &x)? {253			count += 1;254		}255	}256	Ok(count)257}258259#[builtin]260pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {261	if arr.is_empty() {262		return eval_on_empty(onEmpty);263	}264	Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))265}266267#[builtin]268pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {269	let newArrLeft = arr.clone().slice(None, Some(at), None);270	let newArrRight = arr.slice(Some(at + 1), None, None);271272	Ok(ArrValue::extended(newArrLeft, newArrRight))273}274275#[builtin]276pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {277	for (index, item) in arr.iter().enumerate() {278		if equals(&item?, &elem)? {279			return builtin_remove_at(arr.clone(), index as i32);280		}281	}282	Ok(arr)283}284285#[builtin]286pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {287	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {288		if values.len() == 1 {289			return values[0].clone();290		} else if values.len() == 2 {291			return ArrValue::extended(values[0].clone(), values[1].clone());292		}293		let (a, b) = values.split_at(values.len() / 2);294		ArrValue::extended(flatten_inner(a), flatten_inner(b))295	}296	if arrs.is_empty() {297		return ArrValue::empty();298	} else if arrs.len() == 1 {299		return arrs.into_iter().next().expect("single");300	}301	flatten_inner(&arrs)302}303304#[builtin]305pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {306	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {307		match value {308			Val::Arr(arr) => {309				for ele in arr.iter() {310					process(ele?, out)?;311				}312			}313			_ => out.push(value),314		}315		Ok(())316	}317	let mut out = Vec::new();318	process(value, &mut out)?;319	Ok(out)320}321322#[builtin]323pub fn builtin_prune(324	a: Val,325326	#[default(false)]327	#[cfg(feature = "exp-preserve-order")]328	preserve_order: bool,329) -> Result<Val> {330	fn is_content(val: &Val) -> bool {331		match val {332			Val::Null => false,333			Val::Arr(a) => !a.is_empty(),334			Val::Obj(o) => !o.is_empty(),335			_ => true,336		}337	}338	Ok(match a {339		Val::Arr(a) => {340			let mut out = Vec::new();341			for (i, ele) in a.iter().enumerate() {342				let ele = ele343					.and_then(|v| {344						builtin_prune(345							v,346							#[cfg(feature = "exp-preserve-order")]347							preserve_order,348						)349					})350					.with_description(|| format!("elem <{i}> pruning"))?;351				if is_content(&ele) {352					out.push(ele);353				}354			}355			Val::Arr(ArrValue::eager(out))356		}357		Val::Obj(o) => {358			let mut out = ObjValueBuilder::new();359			for (name, value) in o.iter(360				#[cfg(feature = "exp-preserve-order")]361				preserve_order,362			) {363				let value = value364					.and_then(|v| {365						builtin_prune(366							v,367							#[cfg(feature = "exp-preserve-order")]368							preserve_order,369						)370					})371					.with_description(|| format!("field <{name}> pruning"))?;372				if !is_content(&value) {373					continue;374				}375				out.field(name).value(value);376			}377			Val::Obj(out.build())378		}379		_ => a,380	})381}
after · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66	let arr = arr.to_array();67	arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_flatmap(72	func: NativeFn<((Either![String, Val],), Val)>,73	arr: IndexableVal,74) -> Result<IndexableVal> {75	use std::fmt::Write;76	match arr {77		IndexableVal::Str(str) => {78			let mut out = String::new();79			for c in str.chars() {80				match func(Either2::A(c.to_string()))? {81					Val::Str(o) => write!(out, "{o}").unwrap(),82					Val::Null => continue,83					_ => bail!("in std.join all items should be strings"),84				};85			}86			Ok(IndexableVal::Str(out.into()))87		}88		IndexableVal::Arr(a) => {89			let mut out = Vec::new();90			for el in a.iter() {91				let el = el?;92				match func(Either2::B(el))? {93					Val::Arr(o) => {94						for oe in o.iter() {95							out.push(oe?);96						}97					}98					Val::Null => continue,99					_ => bail!("in std.join all items should be arrays"),100				};101			}102			Ok(IndexableVal::Arr(out.into()))103		}104	}105}106107#[builtin]108pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {109	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))110}111112#[builtin]113pub fn builtin_filter_map(114	filter_func: FuncVal,115	map_func: FuncVal,116	arr: ArrValue,117) -> Result<ArrValue> {118	Ok(builtin_filter(filter_func, arr)?.map(map_func))119}120121#[builtin]122pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {123	let mut acc = init;124	for i in arr.iter() {125		acc = func.evaluate_simple(&(acc, i?), false)?;126	}127	Ok(acc)128}129130#[builtin]131pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {132	let mut acc = init;133	for i in arr.iter().rev() {134		acc = func.evaluate_simple(&(i?, acc), false)?;135	}136	Ok(acc)137}138139#[builtin]140pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {141	if to < from {142		return Ok(ArrValue::empty());143	}144	Ok(ArrValue::range_inclusive(from, to))145}146147#[builtin]148pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {149	use std::fmt::Write;150	Ok(match sep {151		IndexableVal::Arr(joiner_items) => {152			let mut out = Vec::new();153154			let mut first = true;155			for item in arr.iter() {156				let item = item?.clone();157				if let Val::Arr(items) = item {158					if !first {159						out.reserve(joiner_items.len());160						// TODO: extend161						for item in joiner_items.iter() {162							out.push(item?);163						}164					}165					first = false;166					out.reserve(items.len());167					for item in items.iter() {168						out.push(item?);169					}170				} else if matches!(item, Val::Null) {171					continue;172				} else {173					bail!("in std.join all items should be arrays");174				}175			}176177			IndexableVal::Arr(out.into())178		}179		IndexableVal::Str(sep) => {180			let mut out = String::new();181182			let mut first = true;183			for item in arr.iter() {184				let item = item?.clone();185				if let Val::Str(item) = item {186					if !first {187						out += &sep;188					}189					first = false;190					write!(out, "{item}").unwrap();191				} else if matches!(item, Val::Null) {192					continue;193				} else {194					bail!("in std.join all items should be strings");195				}196			}197198			IndexableVal::Str(out.into())199		}200	})201}202203#[builtin]204pub fn builtin_reverse(arr: ArrValue) -> ArrValue {205	arr.reversed()206}207208#[builtin]209pub fn builtin_any(arr: ArrValue) -> Result<bool> {210	for v in arr.iter() {211		let v = bool::from_untyped(v?)?;212		if v {213			return Ok(true);214		}215	}216	Ok(false)217}218219#[builtin]220pub fn builtin_all(arr: ArrValue) -> Result<bool> {221	for v in arr.iter() {222		let v = bool::from_untyped(v?)?;223		if !v {224			return Ok(false);225		}226	}227	Ok(true)228}229230#[builtin]231pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {232	match arr {233		IndexableVal::Str(str) => {234			let x: IStr = IStr::from_untyped(x)?;235			Ok(!x.is_empty() && str.contains(&*x))236		}237		IndexableVal::Arr(a) => {238			for item in a.iter() {239				let item = item?;240				if equals(&item, &x)? {241					return Ok(true);242				}243			}244			Ok(false)245		}246	}247}248249#[builtin]250pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {251	builtin_member(arr, elem)252}253254#[builtin]255pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {256	let mut count = 0;257	for item in arr.iter() {258		if equals(&item?, &x)? {259			count += 1;260		}261	}262	Ok(count)263}264265#[builtin]266pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {267	if arr.is_empty() {268		return eval_on_empty(onEmpty);269	}270	Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))271}272273#[builtin]274pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {275	let newArrLeft = arr.clone().slice(None, Some(at), None);276	let newArrRight = arr.slice(Some(at + 1), None, None);277278	Ok(ArrValue::extended(newArrLeft, newArrRight))279}280281#[builtin]282pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {283	for (index, item) in arr.iter().enumerate() {284		if equals(&item?, &elem)? {285			return builtin_remove_at(arr.clone(), index as i32);286		}287	}288	Ok(arr)289}290291#[builtin]292pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {293	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {294		if values.len() == 1 {295			return values[0].clone();296		} else if values.len() == 2 {297			return ArrValue::extended(values[0].clone(), values[1].clone());298		}299		let (a, b) = values.split_at(values.len() / 2);300		ArrValue::extended(flatten_inner(a), flatten_inner(b))301	}302	if arrs.is_empty() {303		return ArrValue::empty();304	} else if arrs.len() == 1 {305		return arrs.into_iter().next().expect("single");306	}307	flatten_inner(&arrs)308}309310#[builtin]311pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {312	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {313		match value {314			Val::Arr(arr) => {315				for ele in arr.iter() {316					process(ele?, out)?;317				}318			}319			_ => out.push(value),320		}321		Ok(())322	}323	let mut out = Vec::new();324	process(value, &mut out)?;325	Ok(out)326}327328#[builtin]329pub fn builtin_prune(330	a: Val,331332	#[default(false)]333	#[cfg(feature = "exp-preserve-order")]334	preserve_order: bool,335) -> Result<Val> {336	fn is_content(val: &Val) -> bool {337		match val {338			Val::Null => false,339			Val::Arr(a) => !a.is_empty(),340			Val::Obj(o) => !o.is_empty(),341			_ => true,342		}343	}344	Ok(match a {345		Val::Arr(a) => {346			let mut out = Vec::new();347			for (i, ele) in a.iter().enumerate() {348				let ele = ele349					.and_then(|v| {350						builtin_prune(351							v,352							#[cfg(feature = "exp-preserve-order")]353							preserve_order,354						)355					})356					.with_description(|| format!("elem <{i}> pruning"))?;357				if is_content(&ele) {358					out.push(ele);359				}360			}361			Val::Arr(ArrValue::eager(out))362		}363		Val::Obj(o) => {364			let mut out = ObjValueBuilder::new();365			for (name, value) in o.iter(366				#[cfg(feature = "exp-preserve-order")]367				preserve_order,368			) {369				let value = value370					.and_then(|v| {371						builtin_prune(372							v,373							#[cfg(feature = "exp-preserve-order")]374							preserve_order,375						)376					})377					.with_description(|| format!("field <{name}> pruning"))?;378				if !is_content(&value) {379					continue;380				}381				out.field(name).value(value);382			}383			Val::Obj(out.build())384		}385		_ => a,386	})387}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -78,6 +78,7 @@
 		("repeat", builtin_repeat::INST),
 		("slice", builtin_slice::INST),
 		("map", builtin_map::INST),
+		("mapWithIndex", builtin_map_with_index::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
@@ -3,14 +3,6 @@
 
   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',
 
-  mapWithIndex(func, arr)::
-    if !std.isFunction(func) then
-      error ('std.mapWithIndex first param must be function, got ' + std.type(func))
-    else if !std.isArray(arr) && !std.isString(arr) then
-      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))
-    else
-      std.makeArray(std.length(arr), function(i) func(i, arr[i])),
-
   mapWithKey(func, obj)::
     if !std.isFunction(func) then
       error ('std.mapWithKey first param must be function, got ' + std.type(func))