Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:37:27+00:00 2026-06-15T22:37:27+00:00

I have a clojure map. Name it opts . I know 2 ways to

  • 0

I have a clojure map. Name it opts. I know 2 ways to get values from this map such that if there is no key get some default value:

(let [opts {}
      title-1 (or (:title opts) "Default title")
      title-2 (:title opts "Default title")]
  (println title-1 title-2))

I saw some libraries (quil, incanter) use first approach with or. It seems to me that second approach is more concise and cleaner. Is there advantages in the first approach?
Disadvantage of using (or (:title opts) "Default value") is that we cannot pass false and nil as values anymore, default value is always used in this case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:37:28+00:00Added an answer on June 15, 2026 at 10:37 pm

    A crucial difference between (or (:key hash) default) and (:key hash default) is the fact that the former evaluates default only if it is necessary. In the latter case it is always evaluated. Therefore you should use or if an evaluation of default is expensive.

    Another difference becomes apparent when your hash contains values which are false in a boolean context. In cases of such values (or (:key hash) default) will be evaluated to default instead of false or nil which you expect. In contrast to the or expression, (:key hash default) will yield correct results. As a side note, think twice before storing nil as values in a hash.

    Fine, those were the important differences. Now let’s move to minor ones.

    (or (:title opts) "Default title")
    

    is expanded by the reader to

    ;; Redacted for the sake of brevity.
    (let* [x (:title opts)]
      (if x
        x
        "Default title"))
    

    Arguably, it is less efficient than to simply evaluate

    (:title opts "Default title")
    

    Of course without any benchmarks it is hard to estimate the difference is speed, however I believe that it should be marginal.

    On the other hand, at first glance (or (:key hash) :default) seems to be easier to understand for someone not used to the (:key hash :default) idiom. Consider programmers coming from other languages. In Ruby for instance the typical approach to handling a non existant element of a hash is

    val = hash[:key] || :default
    

    Hence, the first expression might be easier to parse by humans not accustomed to certain Clojure’s idioms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that removes a key from a map: (defn remove-key [key
I have a Clojure map that may contain values that are nil and I'm
I have a string which is a representation of a clojure map. Is there
I have a map in Clojure something like this: (def stuff #{ {:a help
I am new to Clojure and have found that when I loop over this
I am pretty new to Clojure. I have a map of results from a
I have a bunch of functions that map to and from some codes defined
I have a clojure function that needs to push information into a map if
Does clojure have a powerful 'loop' like common lisp. for example: get two elements
I have installed clojure plugins in eclipse so that I can start developing apps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.