From 3cb16462db79e3deef84220192199721b1c9cee5 Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Wed, 5 Jan 2022 02:52:09 +0100 Subject: [PATCH] Call a function on every item in a vector when given as parameter --- kalk/src/interpreter.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/kalk/src/interpreter.rs b/kalk/src/interpreter.rs index 09f6d23..1e440f7 100644 --- a/kalk/src/interpreter.rs +++ b/kalk/src/interpreter.rs @@ -323,6 +323,33 @@ pub(crate) fn eval_fn_call_expr( }; if let Some((result, _)) = prelude_func { + // If the result is nan and only one argument was given, + // it may be due to incompatible types. + if result.is_nan() && expressions.len() == 1 { + let x = eval_expr(context, &expressions[0], "")?; + + // If a vector was given, call the function on every item + // in the vector. + if let KalkValue::Vector(values) = x { + let mut new_values = Vec::new(); + let mut success = true; + for value in values { + if let Some(result) = + prelude::call_unary_func(context, &identifier.full_name, value, "") + { + new_values.push(result.0); + } else { + success = false; + break; + } + } + + if success { + return Ok(KalkValue::Vector(new_values)); + } + } + } + return Ok(result); }