Clojure更新列表中的映射

时间:2017-01-14 15:08:48

标签: clojure clojure-java-interop

我尝试在this脚本中添加一些修改。给出了这种方法:

(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))))

据我所知,此代码fields是一个包含地图的列表。如果我用

打印内容
(doseq [fff fields
   [k v] fff]
  (info (str k " " fff)))

然后我得到了这个内容

{
: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"
}

我尝试做的是在fieldsCapitalize变量中添加另一个名为vals的值,其中列表中的地图完全相同,但仅name是资本化的。在此示例中,counterAlt应变为CounterAlt

我有一个工作大写的功能,但是我无法使用更新的地图创建另一个列表。在这个功能中实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我终于找到了一种方法,不确定它是否是最好的方法

(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))))