Search code examples
clojureclojure-java-interop

Clojure update maps in a list


I'm trying add some modifications to this script. This method is given:

(defn- emit-class!
  [[class fields]]
  (let [vals {:view?   (:view? class)
          :type    (if (:view? class) "View" "Object")
          :package (:package class)
          :name    (str (:dollar-name class) Icepick/SUFFIX)
          :target  (:dotted-name class)
          :parent  (if-let [parent (:qualified-parent-name class)]
                     (str parent Icepick/SUFFIX)
                     (if (:view? class) "View" "Object"))
          :fields  fields}
    file-name (str (:package class) "." (:dollar-name class) Icepick/SUFFIX)
    file-object (file-object file-name (:element class))]

     (doto (.openWriter file-object)
          (.write (mustache/render-string template vals))
          (.flush)
          (.close))))

As far as I understand this code fields is a list containing maps. If I print the content with

(doseq [fff fields
   [k v] fff]
  (info (str k " " fff)))

Then I get this content

{
:name "counterAlt", 
:enclosing-class 
    {
        :package "com.some.package", 
        :dotted-name "DemoPresenter", 
        :dollar-name "DemoPresenter", 
        :annote (#object[com.sun.tools.javac.code.Attribute$Compound 0x6054b6e "@com.Bla"]), 
        :elem #object[com.sun.tools.javac.code.Symbol$ClassSymbol 0x21312e84 "com.evernote.android.common.demo.DemoPresenter"], 
        :view? false, 
        :qualified-parent-name nil
    }, 
:bundler false, 
:method "Int"
}

What I'm trying to do is to add another value called fieldsCapitalize to the vals variable, where the maps in the list are exactly the same, but only the name is capitalized. In this sample counterAlt should become CounterAlt.

I have a working capitalize function, but I'm unable to create another list with the updated maps. What's the best way to achieve this in this function?


Solution

  • I finally found a way, not sure if it's best approach though

    (defn capitalize [s]
      (if (> (count s) 0)
        (str (Character/toUpperCase (.charAt s 0))
             (subs s 1))
        s))
    
    (defn myfunc [m] (assoc m :name (capitalize (get m :name))))
    
    (defn- emit-class!
      [[class fields]]
      (let [vals {:view?   (:view? class)
          :type    (if (:view? class) "View" "Object")
          :package (:package class)
          :name    (str (:dollar-name class) Icepick/SUFFIX)
          :target  (:dotted-name class)
          :parent  (if-let [parent (:qualified-parent-name class)]
                     (str parent Icepick/SUFFIX)
                     (if (:view? class) "View" "Object"))
          :fields  fields
          :cap     (map myfunc fields)}
    
        file-name (str (:package class) "." (:dollar-name class) Icepick/SUFFIX)
        file-object (file-object file-name (:element class))]
    
         (doto (.openWriter file-object)
              (.write (mustache/render-string template vals))
              (.flush)
              (.close))))