From 76d480c465fe3725dfb5f0fa0c68e5ed6b7135d5 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 8 May 2023 08:36:13 -0500 Subject: [PATCH] `Some` function to check if tuple contains value --- src/function/builtin.rs | 19 +++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 20 insertions(+) diff --git a/src/function/builtin.rs b/src/function/builtin.rs index bb06453..1ec2bb6 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -148,6 +148,25 @@ pub fn builtin_function(identifier: &str) -> Option { let result_index = if arguments[0].as_boolean()? { 1 } else { 2 }; Ok(arguments.swap_remove(result_index)) })), + "some" => Some(Function::new(move |argument| { + let arguments = argument.as_tuple()?; + if let (Value::Tuple(a), b) = (&arguments[0].clone(), &arguments[1].clone()) { + if let Value::Tuple(b) = b { + for item in b { + if a.contains(item) { + return Ok(Value::Boolean(true)); + } + } + } else { + if a.contains(&b) { + return Ok(Value::Boolean(true)); + } + } + Ok(Value::Boolean(false)) + } else { + Ok(Value::Boolean(false)) + } + })), "len" => Some(Function::new(|argument| { if let Ok(subject) = argument.as_string() { Ok(Value::from(subject.len() as IntType)) diff --git a/src/lib.rs b/src/lib.rs index e492ac5..14c9598 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -328,6 +328,7 @@ //! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | //! | `ceil` | 1 | Numeric | Returns the smallest integer greater than or equal to a number | //! | `if` | 3 | Boolean, Any, Any | If the first argument is true, returns the second argument, otherwise, returns the third | +//! | `some` | 2 | Tuple, Any | Returns true if second argument exists in first argument(tuple). If second argument is tuple, checks if any exist in first argument. | //! | `typeof` | 1 | Any | returns "string", "float", "int", "boolean", "tuple", or "empty" depending on the type of the argument | //! | `math::is_nan` | 1 | Numeric | Returns true if the argument is the floating-point value NaN, false if it is another floating-point value, and throws an error if it is not a number | //! | `math::is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise |