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

clojure.core/get ought to support an optional 3rd arg: not-found (default value) #86

Open
phrohdoh opened this issue Jul 14, 2021 · 2 comments · May be fixed by #87
Open

clojure.core/get ought to support an optional 3rd arg: not-found (default value) #86

phrohdoh opened this issue Jul 14, 2021 · 2 comments · May be fixed by #87

Comments

@phrohdoh
Copy link
Contributor

phrohdoh commented Jul 14, 2021

API for clojure.core/get - Clojure v1.10.2 (stable)

(get map key)
(get map key not-found)
for src in '(get,{},:k)#_nil' '(get,{:k,:v},:k)#_:v' '(get,{},:k,:not-found)#_:not-found' '(get,{:k,:v},:k,:not-found)#_:v'; do clj -e "(prn,$src)"; done
nil
:v
:not-found
:v

ClojureRS 1a1681f supports only

(get map key)
for src in '(get,{},:k)' '(get,{:k,:v},:k)' '(get,{},:k,:not-found)' '(get,{:k,:v},:k,:not-found)'; do cargo run -- -e "$src"; done
nil
:v
#Condition["Wrong number of arguments given to function (Given: 3, Expected: 2)"]
#Condition["Wrong number of arguments given to function (Given: 3, Expected: 2)"]
@phrohdoh phrohdoh changed the title get Clojure function ought to support 1 or 2 args, the latter being an optional default value get Clojure function ought to support 2 or 3 args, the latter being an optional default value Jul 14, 2021
@phrohdoh phrohdoh changed the title get Clojure function ought to support 2 or 3 args, the latter being an optional default value clojure.core/get ought to support an optional 3rd arg: not-found (default value) Jul 14, 2021
@phrohdoh
Copy link
Contributor Author

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 let Value::PersistentListMap(pmap) = &*(args.get(0).unwrap().clone()) {
let key = args.get(1).unwrap();
return pmap.get(key).to_value();
}

references

fn get(&self, key: &Rc<Value>) -> Rc<Value>;

which does not currently take an optional default Value

@phrohdoh
Copy link
Contributor Author

phrohdoh commented Jul 14, 2021

A possible solution currently exists in my clojure.core/get,arity-3,not-found branch (likely to be deleted some day) which exhibits the following behavior:

for src in '(get {} :k)' '(get {:k :v} :k)' '(get {} :k :not-found)' '(get {:k :v} :k :not-found)'; do cargo run -- -e "$src"; done
nil
:v
:not-found
:v

Matching what we see from the clj usage in the original post.

That is to say, supporting (get map key) and (get map key not-found).

@phrohdoh phrohdoh linked a pull request Jul 14, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant