Implement builtin function length()

This commit is contained in:
ConnorSkees 2020-02-14 10:10:51 -05:00
parent 38802b69c5
commit b31c9587bc
2 changed files with 15 additions and 0 deletions

View File

@ -1 +1,15 @@
use std::collections::BTreeMap;
use super::Builtin;
use crate::units::Unit;
use crate::value::{Number, Value};
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
decl!(f "length", |args, _| {
let len = match arg!(args, 0, "list").eval() {
Value::List(v, _) => Number::from(v.len()),
_ => Number::from(1)
};
Some(Value::Dimension(len, Unit::None))
});
}

View File

@ -22,6 +22,7 @@ lazy_static! {
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = { pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = {
let mut m = BTreeMap::new(); let mut m = BTreeMap::new();
color::register(&mut m); color::register(&mut m);
list::register(&mut m);
math::register(&mut m); math::register(&mut m);
meta::register(&mut m); meta::register(&mut m);
string::register(&mut m); string::register(&mut m);