By newBieDev


2018-07-11 23:09:24 8 Comments

I have an issue that I've run into and not sure how to approach. Basically, I have a database call which returns a sequence of maps. I need to apply a change to a specific element of each map, and then get back a new sequence with all the same values except the one I've changed.

I currently have the following, which iterates through the sequence and prints it, but I'm not sure how to actually build the new one.

(defn find-character-by-id []
  (let [
      character (mc/find-maps db "characters" {:user_id "5b4403d3c1025107593fa0b4" })
    ]
    (doseq [s character]
      (println s))))

an example map might be:

{:_id #object[org.bson.types.ObjectId 0x74bb0de6 5b467e5bc102511a1729a0f1], :user_id 5b4403d3c1025107593fa0b4, :value 20}

My goal right now is to be able to apply a custom function to cover the _id to a string, but I might also want to add an arbitrary amount to value so something generic is best.

1 comments

@Carcigenicate 2018-07-11 23:55:23

If you wanted to convert each :_id to a string using some to-str function, you could write:

(mapv ; Turn characters into a modified vector
  #(update % :_id to-str) ; by updating each map :_id using to-str
  characters)

I'm mapping over each map, then updateing the :_id of each by applying to-str to each :_id val.

Any time you want to turn one list into another list of an equal length, use map or mapv. You can also use for if you need some filtering capability.

Any time you want to change a value associated with a key in a map based on its old value, use update.

I'm just combining the two here.

doseq should really only be used when you're iterating without the intent of creating a new list, or the new list you're creating is being created via some mutation. It doesn't evaluate to anything useful.

@newBieDev 2018-07-12 00:53:59

This worked perfectly! Thank you for the detailed explanation as well!

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Clojure nested doseq loop

1 Answered Questions

[SOLVED] Clojure: Extract maps with specific values from Lazy Sequence

2 Answered Questions

[SOLVED] Access data from a Lazy Sequence

  • 2016-12-16 18:45:30
  • ben
  • 39 View
  • 1 Score
  • 2 Answer
  • Tags:   clojure

2 Answered Questions

[SOLVED] How do I flatten a sequence of sequences of maps into a sequence of vectors?

  • 2015-05-24 04:14:36
  • chebyshev
  • 197 View
  • 0 Score
  • 2 Answer
  • Tags:   clojure

1 Answered Questions

[SOLVED] Clojure printing lazy sequence

1 Answered Questions

[SOLVED] Efficient side-effect-only analogue of Clojure's map function

  • 2014-01-30 06:42:05
  • Mars
  • 442 View
  • 3 Score
  • 1 Answer
  • Tags:   clojure

1 Answered Questions

[SOLVED] Retrieving maps from a sequence based on contents - Clojure

  • 2013-08-20 16:47:22
  • David
  • 63 View
  • 2 Score
  • 1 Answer
  • Tags:   clojure

2 Answered Questions

[SOLVED] How to append to variable from within another function in clojure?

  • 2013-07-17 11:00:42
  • LocustHorde
  • 215 View
  • 1 Score
  • 2 Answer
  • Tags:   clojure

1 Answered Questions

[SOLVED] Clojure returning a list of strings from a for method

1 Answered Questions

Sponsored Content