1.3.0 permalink Arrow_down_16x16
  • (fnil f x)
  • (fnil f x y)
  • (fnil f x y z)
Takes a function f, and returns a function that calls f, replacing
a nil first argument to f with the supplied value x. Higher arity
versions can replace arguments in the second and third
positions (y, z). Note that the function f can take any number of
arguments, not just the one(s) being nil-patched.

1 Example top

  • ;; a function that expects a non-nil value
    (defn say-hello [name] (str "Hello " name))
    #'user/say-hello
    
    ;; fnil lets you create another function with a default
    ;; arg in case it is passed a nil
    (def say-hello-with-defaults (fnil say-hello "World"))
    #'user/say-hello-with-defaults
    
    ;; the happy path works as you would expect
    (say-hello-with-defaults "Sir")
    "Hello Sir"
    
    ;; but in the case that the function is passed a nil it will use the 
    ;; default supplied to fnil
    (say-hello-with-defaults nil)
    "Hello World"
    
    ;; this works with different arities too
    (defn say-hello [first other] (str "Hello " first " and " other))
    #'user/say-hello
    
    ;; lets create it with defaults
    (def say-hello-with-defaults (fnil say-hello "World" "People"))
    #'user/say-hello-with-defaults
    
    ;; call the function with all nil args - notice it uses the defaults
    ;; supplied to fnil
    (say-hello-with-defaults nil nil)
    "Hello World and People"
    
    ;; any of the args can be nil - the function will supply 
    ;; the default supplied with fnil
    (say-hello-with-defaults "Sir" nil)
    "Hello Sir and People"
    
    ;; and again - notice that "World" is the default here
    (say-hello-with-defaults nil "Ma'am")
    "Hello World and Ma'am"
    
    ;; or pass all args 
    (say-hello-with-defaults "Sir" "Ma'am")
    "Hello Sir and Ma'am"
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5764 top

(defn fnil
  "Takes a function f, and returns a function that calls f, replacing
  a nil first argument to f with the supplied value x. Higher arity
  versions can replace arguments in the second and third
  positions (y, z). Note that the function f can take any number of
  arguments, not just the one(s) being nil-patched."
  {:added "1.2"
   :static true}
  ([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)))))
Vars in clojure.core/fnil:
Used in 0 other vars

Comments top

No comments for fnil. Log in to add a comment.