mailang-stdlib 0.2.5

Standard library for MaìLang
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
//! String built-in functions

use mailang_bytecode::Value;

pub fn builtin_len(args: &[Value]) -> Result<Value, String> {
    match &args[0] {
        Value::Str(s) => Ok(Value::Int(s.chars().count() as i64)),
        Value::Array(a) => Ok(Value::Int(a.borrow().len() as i64)),
        Value::Map(m) => Ok(Value::Int(m.borrow().len() as i64)),
        Value::Tuple(t) => Ok(Value::Int(t.borrow().len() as i64)),
        _ => Err("len expects a string, array, map, or tuple".to_string()),
    }
}