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 8329171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:41:11+00:00 2026-06-09T01:41:11+00:00

What is the idiomatic way of checking if a key on a map has

  • 0

What is the idiomatic way of checking if a key on a map has a value? For example if we have:

=> (def seq-of-maps [{:foo 1 :bar "hi"} {:foo 0 :bar "baz"}])

To find out all the maps with :foo == 0, I like:

=> (filter (comp zero? :foo) seq-of-maps)
({:foo 0, :bar "baz"})

But if I want to find all the maps with :bar == “hi”, the best that I can think of is:

=> (filter #(= (:bar %) "hi") seq-of-maps)
({:foo 1, :bar "hi"})

which I don’t find very readable. Is there a better/more idiomatic way of doing it?

  • 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-09T01:41:13+00:00Added an answer on June 9, 2026 at 1:41 am

    I personally like refactoring this kind of thing to use a clearly named higher order function:

    (def seq-of-maps [{:foo 1 :bar "hi"} {:foo 0 :bar "baz"}])
    
    (defn has-value [key value]
      "Returns a predicate that tests whether a map contains a specific value"
      (fn [m]
        (= value (m key))))
    
    (filter (has-value :bar "hi") seq-of-maps)
    => ({:foo 1, :bar "hi"})
    

    Downside is that it gives you an extra function definition to manage and maintain, but I think the elegance / code readability is worth it. This approach can also be very efficient from a performance perspective if you re-use the predicate many times.

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

Sidebar

Related Questions

What is the most idiomatic way to do the following? def xstr(s): if s
If I have a List[Option[A]] in Scala, what is the idiomatic way to filter
I have a dead simple Common Lisp question: what is the idiomatic way of
If I have the request size=3&mean=1&sd=3&type=pdf&distr=normal what's the idiomatic way of writing the function
Imagine you have a map like this: (def person { :name { :first-name John
I'm looking for a much more idiomatic way to do the following little ruby
Possible Duplicate: Why is Java's Iterator not an Iterable? Idiomatic way to use for-each
What would be the best way and more idiomatic to break a string into
Is there an idiomatic way to bind variables in a MATLAB function? It seems
What's the idiomatic way to do maximumBy (higher order function taking a comparison function

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.