Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support clojure.core/get arity 3 #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/persistent_list_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! b => {:a 1 :b 3}

use crate::maps::MapEntry;
use crate::value::Value;
use crate::value::{ToValue, Value};
use crate::traits;

use std::collections::HashMap;
Expand Down Expand Up @@ -101,6 +101,7 @@ macro_rules! merge {
/// A PersistentListMap.
pub trait IPersistentMap {
fn get(&self, key: &Rc<Value>) -> Rc<Value>;
fn get_with_default(&self, key: &Rc<Value>, default: &Rc<Value>) -> Rc<Value>;
fn assoc(&self, key: Rc<Value>, value: Rc<Value>) -> Self;
fn contains_key(&self,key: &Rc<Value>) -> bool;
}
Expand All @@ -117,6 +118,20 @@ impl IPersistentMap for PersistentListMap {
PersistentListMap::Empty => Rc::new(Value::Nil),
}
}
fn get_with_default(&self, key: &Rc<Value>, default: &Rc<Value>) -> Rc<Value> {
match self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return Rc::clone(&entry.val);
}
match parent.get_with_default(key, default).as_ref() {
Value::Nil => default.clone(),
val => (*val).to_rc_value(),
}
}
PersistentListMap::Empty => default.clone(),
}
}
fn assoc(&self, key: Rc<Value>, val: Rc<Value>) -> PersistentListMap {
PersistentListMap::Map(Rc::new(self.clone()), MapEntry { key, val })
}
Expand Down Expand Up @@ -146,6 +161,17 @@ impl IPersistentMap for Rc<PersistentListMap> {
PersistentListMap::Empty => Rc::new(Value::Nil),
}
}
fn get_with_default(&self, key: &Rc<Value>, default: &Rc<Value>) -> Rc<Value> {
match &**self {
PersistentListMap::Map(parent, entry) => {
if entry.key == *key {
return Rc::clone(&entry.val);
}
parent.get_with_default(key, default)
}
PersistentListMap::Empty => Rc::new(Value::Nil),
}
}
fn assoc(&self, key: Rc<Value>, val: Rc<Value>) -> Rc<PersistentListMap> {
Rc::new(PersistentListMap::Map(
Rc::clone(self),
Expand Down Expand Up @@ -306,6 +332,25 @@ mod tests {
println!("{}", map3);
println!("{}", map4);
}

#[test]
fn get_with_default() {
let map = persistent_list_map!(map_entry!("x", "v"));
let key = Keyword::intern("k").to_rc_value();
let default = Keyword::intern("not-found").to_rc_value();
let val: Rc<Value> = map.get_with_default(&key, &default);
Copy link
Contributor Author

@phrohdoh phrohdoh Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not intend to leave these type annotations in.

  • remove type annotations in get_with_default and get_with_default_empty tests

assert_eq!(default, val);
}

#[test]
fn get_with_default_empty() {
let map = persistent_list_map!(/*empty*/);
let key = Keyword::intern("k").to_rc_value();
let default = Keyword::intern("not-found").to_rc_value();
let val: Rc<Value> = map.get_with_default(&key, &default);
assert_eq!(default, val);
}

#[test]
fn contains_key() {
let map1 = persistent_list_map!{ "a" => 12, "b" => 13 };
Expand Down
8 changes: 8 additions & 0 deletions src/protocols/ipersistent_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ impl persistent_list_map::IPersistentMap for IPersistentMap {
_ => panic!("Called Iterable iter on non-iterable"),
}
}
fn get_with_default(&self, key: &Rc<Value>, default: &Rc<Value>) -> Rc<Value> {
match &*self.value {
Value::PersistentListMap(plist_map) => {
plist_map.get_with_default(key, default)
},
_ => panic!("Called Iterable iter on non-iterable"),
}
}
fn assoc(&self, key: Rc<Value>, value: Rc<Value>) -> IPersistentMap {
match &*self.value {
Value::PersistentListMap(plist_map) => {
Expand Down
14 changes: 8 additions & 6 deletions src/rust_core/get.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::error_message;
use crate::ifn::IFn;
use crate::persistent_list_map::IPersistentMap;
use crate::value::{ToValue, Value};
Expand All @@ -14,16 +15,17 @@ impl ToValue for GetFn {
}
impl IFn for GetFn {
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
if args.len() != 2 {
return Value::Condition(format!(
"Wrong number of arguments given to function (Given: {}, Expected: 2)",
args.len()
));
if args.len() != 2 && args.len() != 3 {
return error_message::wrong_varg_count(&[2, 3], args.len());
}

if let Value::PersistentListMap(pmap) = &*(args.get(0).unwrap().clone()) {
let key = args.get(1).unwrap();
return pmap.get(key).to_value();
return if let Some(not_found) = args.get(2) {
pmap.get_with_default(key, not_found)
} else {
pmap.get(key)
}.to_value();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last part I'd like to add then is a test for GetFn (including one covering the original 2 arg'd GetFn we never wrote)

}
// @TODO add error in here with erkk's new error tools

Expand Down