clojure-rs / ClojureRS

Clojure, implemented atop Rust (unofficial)
Apache License 2.0
952 stars 27 forks source link

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

Open phrohdoh opened 3 years ago

phrohdoh commented 3 years ago

https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/get

Returns the value mapped to key, not-found or nil if key not present.


(get map key)           ;; supported prior to this PR
(get map key not-found) ;; supported as of this PR

Instead of adding a not_found: Option<&Rc<Value> to IPersistentMap::get (to be threaded through all nested calls and a seemingly-arbitrary default to ::get callsites) I opted to continue the *_with_* convention; creating a get_with_default function.


If merged, this would fix https://github.com/clojure-rs/ClojureRS/issues/86 (for maps, but I, perhaps incorrectly, recall reading that get can be applied to non-maps, though the linked docs certainly do not suggest so).

phrohdoh commented 3 years ago

See https://github.com/phrohdoh/cljrs/commit/ceb3f9eeed12c6987ff8e41ddcf9b260674d639e for changes building on top of this PR to support map and keyword application default values:

({:k :v} :x)            ;; => nil
({:k :v} :x :not-found) ;; => :not-found
(:x {:k :v})            ;; => nil
(:x {:k :v} :not-found) ;; => :not-found