Generating Clojure Map Key From a Var

Did you ever want to create a map’s keys from a sequence, or create map keys from vars?

I need to do this for my latest CodeLesson Intro To Clojure Course. Here is an example. Assuming C is a checking account and 100 is $100.00:

From the REPL:


ba1-app=> (def acct-no 10000)
#'ba1-app/acct-no

ba1-app=> {(keyword (str acct-no)) [C 100]}

{:10000 [C 100]}

Advertisements

2 Comments

Filed under Clojure

2 responses to “Generating Clojure Map Key From a Var

  1. Is there a reason why you didn’t just use the value 1000 as the key? As far as I can tell the only downside is that the value 1000 can’t be used as a function of a map to get the value, but it seems a little gross to be dynamically generating keywords based on numbers.

    • Octopusgrabbus

      You are correct. There is no reason not to use the 1000 value, so that is what I will use for the key.

      As I am learning Clojure getting a handle on what keys actually are has remained elusive for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s