Does anybody else feel that (fnil) should delay evaluation of its argument?
Maybe I've been abusing mutability a bit too much, but for a map which 
refers to mutable object instances, I've found this useful:


(defmacro fnil*
  "Delayed evaluation version of fnil, where the default expression 
gets                                                                            
                                                                                
         

evaluated every time it is required."
  ([f x]
     `(fn
        ([a#] (~f (if (nil? a#) ~x a#)))
        ([a# b#] (~f (if (nil? a#) ~x a#) b#))
        ([a# b# c#] (~f (if (nil? a#) ~x a#) b# c#))
        ([a# b# c# & ds#] (apply ~f (if (nil? a#) ~x a#) b# c# ds#))))
  ([f x ~y]
     `(fn
        ([a# b#] (~f (if (nil? a#) ~x a#) (if (nil? b#) ~y b#)))
        ([a# b# c#] (~f (if (nil? a#) ~x a#) (if (nil? b#) ~y b#) c#))
        ([a# b# c# & ds#] (apply ~f (if (nil? a#) ~x a#) (if (nil? b#) ~y 
b#) c# ds#))))
  ([f x ~y ~z]
     `(fn
        ([a# b#] (~f (if (nil? a#) ~x a#) (if (nil? b#) ~y b#)))
        ([a# b# c#] (~f (if (nil? a#) ~x a#) (if (nil? b#) ~y b#) (if (nil? 
c#) ~z c#)))
        ([a# b# c# & ds#] (apply ~f (if (nil? a#) ~x a#) (if (nil? b#) ~y 
b#) (if (nil? c#) ~z c#) ds#)))))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to