diff options
| author | Joel Martin <github@martintribe.org> | 2014-10-27 21:13:50 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-06 21:59:00 -0600 |
| commit | b12d98e4e3424be6b1e851c9450e9532910996ec (patch) | |
| tree | 317d8b69d1e0a67f42af06ebe257ffbac2393869 /rust/src/types.rs | |
| parent | 3744d56621ff4c1c6c8a1fd6beb1d03123df53a5 (diff) | |
| download | mal-b12d98e4e3424be6b1e851c9450e9532910996ec.tar.gz mal-b12d98e4e3424be6b1e851c9450e9532910996ec.zip | |
rust: core hash-map functions.
Diffstat (limited to 'rust/src/types.rs')
| -rw-r--r-- | rust/src/types.rs | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/rust/src/types.rs b/rust/src/types.rs index 97915cb..80079ce 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -245,12 +245,12 @@ pub fn vector_q(a:Vec<MalVal>) -> MalRet { // Hash Maps pub fn hash_map(hm: HashMap<String,MalVal>) -> MalVal { Rc::new(Hash_Map(hm)) } -pub fn hash_mapv(seq: Vec<MalVal>) -> MalRet { - if seq.len() % 2 == 1 { - return err_str("odd number of elements to hash-map"); +pub fn _assoc(hm: &HashMap<String,MalVal>, a:Vec<MalVal>) -> MalRet { + if a.len() % 2 == 1 { + return err_str("odd number of hash-map keys/values"); } - let mut new_hm: HashMap<String,MalVal> = HashMap::new(); - let mut it = seq.iter(); + let mut new_hm = hm.clone(); + let mut it = a.iter(); loop { let k = match it.next() { Some(mv) => match *mv.clone() { @@ -264,6 +264,25 @@ pub fn hash_mapv(seq: Vec<MalVal>) -> MalRet { } Ok(Rc::new(Hash_Map(new_hm))) } +pub fn _dissoc(hm: &HashMap<String,MalVal>, a:Vec<MalVal>) -> MalRet { + let mut new_hm = hm.clone(); + let mut it = a.iter(); + loop { + let k = match it.next() { + Some(mv) => match *mv.clone() { + Strn(ref s) => s.to_string(), + _ => return err_str("key is not a string in hash-map call"), + }, + None => break, + }; + new_hm.remove(&k); + } + Ok(Rc::new(Hash_Map(new_hm))) +} +pub fn hash_mapv(seq: Vec<MalVal>) -> MalRet { + let new_hm: HashMap<String,MalVal> = HashMap::new(); + _assoc(&new_hm, seq) +} pub fn hash_map_q(a:Vec<MalVal>) -> MalRet { if a.len() != 1 { return err_str("Wrong arity to map? call"); @@ -274,6 +293,7 @@ pub fn hash_map_q(a:Vec<MalVal>) -> MalRet { } } +// Functions pub fn func(f: fn(Vec<MalVal>) -> MalRet ) -> MalVal { Rc::new(Func(f)) } |
