Some function to check if tuple contains value

This commit is contained in:
Nick 2023-05-08 08:36:13 -05:00
parent f2263bd218
commit 76d480c465
2 changed files with 20 additions and 0 deletions

View File

@ -148,6 +148,25 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
let result_index = if arguments[0].as_boolean()? { 1 } else { 2 }; let result_index = if arguments[0].as_boolean()? { 1 } else { 2 };
Ok(arguments.swap_remove(result_index)) 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| { "len" => Some(Function::new(|argument| {
if let Ok(subject) = argument.as_string() { if let Ok(subject) = argument.as_string() {
Ok(Value::from(subject.len() as IntType)) Ok(Value::from(subject.len() as IntType))

View File

@ -328,6 +328,7 @@
//! | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | //! | `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 | //! | `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 | //! | `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 | //! | `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_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 | //! | `math::is_finite` | 1 | Numeric | Returns true if the argument is a finite floating-point number, false otherwise |