Fork me on GitHub
#clojurescript
<
2017-09-15
>
dragoncube00:09:12

x - floor(x)

cjhowe02:09:38

floor is technically the name of the mathematical operator: ⌊x⌋ is floor(x)

bherrmann12:09:50

@john Thanks, gave glow a try and it works well.

borkdude16:09:36

Is it easy to customize the behavior of one hash-map to raise an error when someone gets a key that doesn’t exist? It would be nice for dev when you’re refactoring

spinningtopsofdoom17:09:59

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
)

spinningtopsofdoom17:09:15

That should work if you're using the hash map as a constant

dnolen17:09:52

@borkdude as @spinningtopsofdoom said, specify is what you want

borkdude17:09:08

@spinningtopsofdoom What do you mean by “use as a constant” btw? When would it not work?

dnolen17:09:42

ah he’s talking about his snippet not specify

reefersleep17:09:15

I think he means that you're just altering a single instance with specify... right?

dnolen17:09:49

the snippet doesn’t return a proper full map

dnolen17:09:24

actually, scratch that

dnolen17:09:53

it just depends on what you want to do

dnolen17:09:05

anyways the snippet doesn’t override everything

dnolen17:09:21

so if you want to get new values, those values won’t have validation - that was @spinningtopsofdoom’s point

borkdude17:09:07

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

reefersleep14:09:40

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!

reefersleep14:09:26

(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"

borkdude17:09:44

yeah, it only works for that single instance, which is good enough for me

spinningtopsofdoom17:09:42

Cool I was worried that you'd want the map behavior to work after assoc and company

reefersleep17:09:26

In that case, I guess you'd rather want to create a new map type, right?

reefersleep17:09:55

or override the assoc protocol for the persistentarraymap type

reefersleep17:09:00

if it's even a protocol

reefersleep17:09:26

starts up REPL

spinningtopsofdoom17:09:17

ClojureScript is protocols all the way down

reefersleep17:09:11

also B rilliant