1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use r_derive::*;

use crate::callable::core::*;
use crate::error::Error;
use crate::formals;
use crate::lang::*;
use crate::object::*;

/// Get an Object's Length
///
/// Calculate the length of an object.
///
/// # In-Language
///
/// ## Usage
///
/// ```custom,{class=r}
/// length(x)
/// ```
///
/// ## Arguments
///
/// `x`: An object whose length to calculate.
///
/// ## Examples
///
/// ```custom,{class=r-repl}
/// length([1, 2, 3])
/// ```
///
#[doc(alias = "length")]
#[builtin(sym = "length")]
#[derive(Debug, Clone, PartialEq)]
pub struct PrimitiveLength;

formals!(PrimitiveLength, "(x,)");

impl Callable for PrimitiveLength {
    fn call_matched(&self, args: List, _ellipsis: List, stack: &mut CallStack) -> EvalResult {
        let mut args = Obj::List(args);
        let x = args.try_get_named("x")?.force(stack)?;

        let length: usize = match x {
            Obj::Vector(ref vec) => match vec {
                Vector::Double(rep) => rep.len(),
                Vector::Integer(rep) => rep.len(),
                Vector::Logical(rep) => rep.len(),
                Vector::Character(rep) => rep.len(),
            },
            Obj::List(rep) => rep.len(),
            Obj::Environment(env) => env.len(),
            _ => return Error::Other("Argument 'x' does not have a length".into()).into(),
        };

        EvalResult::Ok(Obj::Vector(Vector::from(vec![OptionNA::Some(
            length as i32,
        )])))
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Error;
    use crate::lang::EvalResult;
    use crate::{r, r_expect};

    #[test]
    fn double() {
        r_expect!(length(c(1, 2)) == 2)
    }
    #[test]
    fn integer() {
        r_expect!(length(2:11) == 10)
    }
    #[test]
    fn logical() {
        r_expect!(length(c(true, true, false)) == 3)
    }
    #[test]
    fn character() {
        r_expect!(length(c("a", "b", "c", "d")) == 4)
    }
    // TODO
    // #[test]
    // fn list() {
    //     r_expect!(length(list(1, 2, 3, 5) == 5))
    // }
    #[test]
    fn environment() {
        r_expect! {{"
            e = environment()
            x = 1
            length(e) == 2
        "}}
    }
    #[test]
    fn empty() {
        r_expect!(length(1[false]) == 0)
    }
    #[test]
    fn subset_mask() {
        r_expect! {{
            length((1:3)[c(true, true, false)]) == 2
        }}
    }
    #[test]
    fn null() {
        assert_eq!(
            r! {length(null)},
            EvalResult::Err(Error::Other("Argument 'x' does not have a length".to_string()).into())
        )
    }

    #[test]
    fn list() {
        r_expect! {{"
            length(list(1)) == 1 && length(list()) == 0
        "}}
    }
}