Clojure: how to avoid eval-ing the not-found expression in map get -


I have a map called "code> robots looking at the map, if given a robot by the given key If not present then I want to make one. not found RG is exactly the same as I want.

However, it seems that is not found expression is evaluated eagerly. How can I stop this assessment so that I can create a new robot, if someone is not present?

  robot> (Def robot {1 {: name "R2D2"}}) # 'robot / robot robot & gt; (Get robot1) {: name "R2D2"} robot & gt; (Get robot 1 (printlane "fight it")) This (i.e. "R2D2"}    

I agree completely, but if your map has incorrect or zero values, its implementation will not work.

Here's a more accurate solution:

  (If robot 1 is included) (get robot 1) (printline "beep boop"))   

and fancy macro:

  (defmacro get-small [map key not found] `(map # ~ map key # key) (Includes # map # key included) (get the map # key # ~))   

To eliminate the possibility of generating some side-effects again < Code> Walk , eg:

  (lazy {1 false}} (printline "side effect") 1) (printlane "oh!")) < / Code>   

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -