constantly

clojure.core

  • (constantly x)
Returns a function that takes any number of arguments and returns x.

3 Examples top

  • user=> (def boring (constantly 10))
    #'user/boring
    
    user=> (boring 1 2 3)
    10
    user=> (boring)
    10
    user=> (boring "Is anybody home?")
    10
    
  • ;; A really goofy way to find the size of a collection
    user=> (reduce + (map (constantly 1) [:a :b :c]))
    3
  • ;; constantly returns a function which always returns the same value
    (map (constantly 9) [1 2 3])
    user=> (9 9 9)
    
    (map (constantly (rand-int 100)) [:a :b :c])
    user=> (43 43 43)
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:1344 top

(defn constantly
  "Returns a function that takes any number of arguments and returns x."
  {:added "1.0"
   :static true}
  [x] (fn [& args] x))
Vars in clojure.core/constantly:
Used in 0 other vars

Comments top

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