-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcons.rs
75 lines (68 loc) · 2.22 KB
/
cons.rs
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
use crate::ifn::IFn;
use crate::value::{ToValue, Value};
use std::rc::Rc;
use crate::error_message;
use crate::iterable::Iterable;
use crate::persistent_list::ToPersistentList;
use crate::protocol::ProtocolCastable;
/// (cons x seq)
/// inserts x as first element of seq
#[derive(Debug, Clone)]
pub struct ConsFn {}
impl ToValue for ConsFn {
fn to_value(&self) -> Value {
Value::IFn(Rc::new(self.clone()))
}
}
impl IFn for ConsFn {
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
if args.len() != 2 {
return error_message::wrong_arg_count(2, args.len());
}
let mut coll_vec = match args.get(1).unwrap().try_as_protocol::<Iterable>() {
Some(iterable) => iterable.clone().iter().collect(),
_ => vec![],
};
coll_vec.insert(0, args.get(0).unwrap().to_owned());
return Value::PersistentList(coll_vec.into_list());
}
}
#[cfg(test)]
mod tests {
mod cons_tests {
use crate::ifn::IFn;
use crate::persistent_list::PersistentList;
use crate::persistent_vector::PersistentVector;
use crate::rust_core::ConsFn;
use crate::value::Value;
use std::rc::Rc;
#[test]
fn cons_test() {
let cons = ConsFn {};
let s = "insert as first";
let args = vec![
Rc::new(Value::String(String::from(s))),
Rc::new(Value::PersistentVector(
vec![
Rc::new(Value::String(String::from("second"))),
Rc::new(Value::String(String::from("third"))),
]
.into_iter()
.collect::<PersistentVector>(),
)),
];
assert_eq!(
Value::PersistentList(
vec![
Rc::new(Value::String(String::from("insert as first"))),
Rc::new(Value::String(String::from("second"))),
Rc::new(Value::String(String::from("third")))
]
.into_iter()
.collect::<PersistentList>()
),
cons.invoke(args)
);
}
}
}