This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-15
Channels
- # aleph (14)
- # aws (5)
- # beginners (144)
- # bitcoin (1)
- # boot (14)
- # chestnut (13)
- # cljs-dev (58)
- # cljs-experience (1)
- # cljsrn (47)
- # clojure (107)
- # clojure-dev (9)
- # clojure-finland (12)
- # clojure-gamedev (4)
- # clojure-russia (2)
- # clojure-sanfrancisco (1)
- # clojure-spec (5)
- # clojure-uk (28)
- # clojurescript (34)
- # cursive (9)
- # datomic (3)
- # emacs (11)
- # garden (16)
- # graphql (9)
- # hoplon (3)
- # jobs (2)
- # juxt (6)
- # off-topic (32)
- # om (10)
- # onyx (1)
- # pedestal (4)
- # proton (1)
- # re-frame (14)
- # reagent (8)
- # ring (1)
- # ring-swagger (34)
- # shadow-cljs (19)
- # sioux-falls (1)
- # spacemacs (16)
- # testing (14)
x - floor(x)
Is it easy to customize the behavior of one hash-map to raise an error when someone get
s a key that doesn’t exist? It would be nice for dev when you’re refactoring
You can try this
(let [normal (zipmap (range 10) (range 10))
lookup-error (specify normal
ILookup
(-lookup [_ k]
(if (-contains-key? normal k)
(-lookup normal k)
(throw (js/Error. "error"))))
(-lookup [_ k n]
(if (-contains-key? normal k)
(-lookup normal k n)
(throw (js/Error. "error")))))]
(-lookup lookup-error 2) ;good
(-lookup lookup-error 11) ;throws error
)
That should work if you're using the hash map as a constant
@borkdude as @spinningtopsofdoom said, specify
is what you want
That's neat!
@spinningtopsofdoom What do you mean by “use as a constant” btw? When would it not work?
I think he means that you're just altering a single instance with specify... right?
so if you want to get new values, those values won’t have validation - that was @spinningtopsofdoom’s point
I wanted to achieve this behavior, which works:
(defn throwing-map [m]
(let [lookup-error
(specify m
ILookup
(-lookup
([_ k]
(if (-contains-key? m k)
(-lookup m k)
(throw (js/Error. (str "error looking up key " k)))))
([_ k n]
(if (-contains-key? m k)
(-lookup m k n)
(throw (js/Error. (str "error looking up key " k)))))))]
lookup-error))
(:a (throwing-map {:b 1})) ;; throws
Just for fun, I tried to implement a new protocol that throws on missing key if no alternative "not-found" value is provided. My first foray into protocols. Very happy that it worked out in the end!
(defprotocol IErrorCausingLookup
(?get
[this k]
[this k not-found]))
(extend-type PersistentArrayMap
IErrorCausingLookup
(?get
([this k]
(if (contains? this k)
(get this k)
(throw (js/Error. (str "error looking up key " k)))))
([this k not-found]
(get this k not-found))))
;;cljs.user> (?get {:a "hey" :b "wow"} :c)
;;=> #object[Error Error: error looking up key :c]
;;=> Error: error looking up key :c
;; nil
;;cljs.user> (?get {:a "hey" :b "wow"} :c "it was just not found")
;;=> "it was just not found"
Brilliant
Cool I was worried that you'd want the map behavior to work after assoc
and company
In that case, I guess you'd rather want to create a new map type, right?
or override the assoc
protocol for the persistentarraymap type
if it's even a protocol
¯\(ツ)/¯
starts up REPL
ClojureScript is protocols all the way down
also B rilliant