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

  • Home
  • SEARCH
  • 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 430783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:54:34+00:00 2026-05-12T19:54:34+00:00

I am not to Clojure and attempting to figure out how to do this.

  • 0

I am not to Clojure and attempting to figure out how to do this.

I want to create a new hash-map that for a subset of the keys in the hash-map applies a function to the elements. What is the best way to do this?

(let 
   [my-map {:hello "World" :try "This" :foo "bar"}]
   (println (doToMap my-map [:hello :foo] (fn [k] (.toUpperCase k)))

This should then result a map with something like

{:hello "WORLD" :try "This" :foo "BAR"}
  • 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-05-12T19:54:35+00:00Added an answer on May 12, 2026 at 7:54 pm
    (defn do-to-map [amap keyseq f]
      (reduce #(assoc %1 %2 (f (%1 %2))) amap keyseq))

    Breakdown:

    It helps to look at it inside-out. In Clojure, hash-maps act like functions; if you call them like a function with a key as an argument, the value associated with that key is returned. So given a single key, the current value for that key can be obtained via:

    (some-map some-key)
    

    We want to take old values, and change them to new values by calling some function f on them. So given a single key, the new value will be:

    (f (some-map some-key))
    

    We want to associate this new value with this key in our hash-map, “replacing” the old value. This is what assoc does:

    (assoc some-map some-key (f (some-map some-key)))
    

    (“Replace” is in scare-quotes because we’re not mutating a single hash-map object; we’re returning new, immutable, altered hash-map objects each time we call assoc. This is still fast and efficient in Clojure because hash-maps are persistent and share structure when you assoc them.)

    We need to repeatedly assoc new values onto our map, one key at a time. So we need some kind of looping construct. What we want is to start with our original hash-map and a single key, and then “update” the value for that key. Then we take that new hash-map and the next key, and “update” the value for that next key. And we repeat this for every key, one at a time, and finally return the hash-map we’ve “accumulated”. This is what reduce does.

    • The first argument to reduce is a function that takes two arguments: an “accumulator” value, which is the value we keep “updating” over and over; and a single argument used in one iteration to do some of the accumulating.
    • The second argument to reduce is the initial value passed as the first argument to this fn.
    • The third argument to reduce is a collection of arguments to be passed as the second argument to this fn, one at a time.

    So:

    (reduce fn-to-update-values-in-our-map 
            initial-value-of-our-map 
            collection-of-keys)
    

    fn-to-update-values-in-our-map is just the assoc statement from above, wrapped in an anonymous function:

    (fn [map-so-far some-key] (assoc map-so-far some-key (f (map-so-far some-key))))
    

    So plugging it into reduce:

    (reduce (fn [map-so-far some-key] (assoc map-so-far some-key (f (map-so-far some-key))))
            amap
            keyseq)
    

    In Clojure, there’s a shorthand for writing anonymous functions: #(...) is an anonymous fn consisting of a single form, in which %1 is bound to the first argument to the anonymous function, %2 to the second, etc. So our fn from above can be written equivalently as:

    #(assoc %1 %2 (f (%1 %2)))
    

    This gives us:

    (reduce #(assoc %1 %2 (f (%1 %2))) amap keyseq)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 241k
  • Answers 241k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer plan on sending it raw xml based based off of… May 13, 2026 at 7:23 am
  • Editorial Team
    Editorial Team added an answer I figured out that the returned node is of course… May 13, 2026 at 7:23 am
  • Editorial Team
    Editorial Team added an answer [\s] matches either a \ or an s. Try using… May 13, 2026 at 7:23 am

Related Questions

For some reason, I am having trouble thinking of a good way to rewrite
I want to embed a dsl or existing full language within my application. It
I'm dabbling in clojure and am having a little trouble trying to determine the
I have a problem with the Enclojure REPL and using clojure modules from it.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.