美文网首页
clojure上一个start最多的集合数据处理库-specte

clojure上一个start最多的集合数据处理库-specte

作者: 小马将过河 | 来源:发表于2019-06-22 12:03 被阅读0次

    正如站点介绍Clojure(Script)'s missing piece

    使用示例

    (require '[com.rpl.specter :refer :all])
    
    
    ;;; 1.1 clojure
    (map inc [1 2 3 4])
    
    (into #{} (map inc #{1 2 3 4}))
    
    ;;; 1.2 specter
    
    (transform ALL inc #{1 2 3 4} )
    (transform ALL inc '(1 2 3 4))
    
    ;;; 2 方便的操作嵌套数据结构
    
    ;;; 2.1 Clojure
    (defn apply-fn-to-map [f m]
      (into {}
            (for [[k v] m]
              [k (f v)]) ))
    
    
    (apply-fn-to-map inc {:a 1 :b 2})
    
    (map #(apply-fn-to-map inc %) [{:a 1 :b 1} {:c 1 :d 1}])
    
    
     ;;; 2.2 Specter
    (transform [MAP-VALS] inc {:a 1 :b 2})
    
    (transform [ALL MAP-VALS] inc [{:a 1 :b 1} {:c 1 :d 1}] )
    
    (transform [ALL MAP-VALS even?] inc [{:a 1 :b 2} {:c 3 :d 4}] )
    
    (transform [MAP-VALS MAP-VALS]   inc  {:a {:aa 1} :b {:ba -1 :bb 2}})  => {:a {:aa 2}, :b {:ba 0, :bb 3}}
    
    ;;; 工作原理
    
    [{:a 1 :b 2} {:c 3 :d 4}]
    
          ;;; ALL
    {:a 1 :b 2}
    {:c 3 :d 4}
    
     ;;; MAP-VAL
    1
    2
    3
    4
    
    ;;; even?
    2
    4
    
    ;;; inc
    3
    5
    
    
    ;;; 重建
    
    ;;; reverse even?
    
    1
    3
    3
    5
    
    
    ;;; reverse MAP-VAL
    {:a 1 :b 3}
    {:c 3 :d 5}
    
    
    ;;; reverse ALL
    [{:a 1 :b 3} {:c 3 :d 5}]
    
    ;;; 业务使用
    
    (def data {:result [{:hy-x                 45,
                         :ft-y                 36,
                         :immed-other-dispose  "涂抹药水2",
                         :later-other-reaction nil,
                         :cure-id              "1",
                         :reaction-time        "2019-05-28T14:01:00",
                         :ft-x                 34,
                         :immed-other-reaction "胳膊红肿3",
                         :list
                         [{:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "局部反应",
                           :list
                           [{:dict-value-name "手臂肿胀",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            230,
                             :dict-value-id   53,
                             :show            true}
                            {:dict-value-name "疼痛",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            231,
                             :dict-value-id   55,
                             :show            true}
                            {:dict-value-name "活动受限",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            232,
                             :dict-value-id   57,
                             :show            true}
                            {:dict-value-name "热",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            247,
                             :dict-value-id   54,
                             :show            true}
                            {:dict-value-name "瘙痒",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            248,
                             :dict-value-id   56,
                             :show            true}
                            {:dict-value-name "活动受限",
                             :dict-value-code "HUODONGSHOUXIAN",
                             :parent-id       52,
                             :group-code      "REACTION",
                             :dict-value-sort 5,
                             :dict-value-id   57,
                             :show            true}],
                           :dict-key-id   52}
                          {:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "全身非特异性反应",
                           :list
                           [{:dict-value-name "发热",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            233,
                             :dict-value-id   59,
                             :show            true}
                            {:dict-value-name "疲倦",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            234,
                             :dict-value-id   60,
                             :show            true}
                            {:dict-value-name "关节痛",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            235,
                             :dict-value-id   61,
                             :show            true}
                            {:dict-value-name "关节痛",
                             :dict-value-code "GUANJIETONG",
                             :parent-id       58,
                             :group-code      "REACTION",
                             :dict-value-sort 3,
                             :dict-value-id   61,
                             :show            true}],
                           :dict-key-id   58}
                          {:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "过敏性结膜炎症状",
                           :list
                           [{:dict-value-name "眼痒",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            256,
                             :dict-value-id   75,
                             :show            true}
                            {:dict-value-name "眼肿",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            237,
                             :dict-value-id   79,
                             :show            true}
                            {:dict-value-name "结膜充血",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            257,
                             :dict-value-id   77,
                             :show            true}
                            {:dict-value-name "畏光",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            258,
                             :dict-value-id   78,
                             :show            true}
                            {:dict-value-name "眼肿",
                             :dict-value-code "YANZHONG",
                             :parent-id       74,
                             :group-code      "REACTION",
                             :dict-value-sort 5,
                             :dict-value-id   79,
                             :show            true}],
                           :dict-key-id   74}
                          {:type          "DISPOSE",
                           :main-id       "1",
                           :dict-key-name "处理方式",
                           :list
                           [{:dict-value-name "抗组胺药",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            238,
                             :dict-value-id   87,
                             :show            true}
                            {:dict-value-name "β受体激动剂(雾化)",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            239,
                             :dict-value-id   90,
                             :show            true}
                            {:dict-value-name "糖皮质激素",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            263,
                             :dict-value-id   88,
                             :show            true}
                            {:dict-value-name "局部处理(冰敷)",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            264,
                             :dict-value-id   89,
                             :show            true}],
                           :dict-key-id   86}
                          {:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "哮喘症状",
                           :list
                           [{:dict-value-name "咳嗽",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            249,
                             :dict-value-id   64,
                             :show            true}
                            {:dict-value-name "胸闷",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            250,
                             :dict-value-id   65,
                             :show            true}
                            {:dict-value-name "咽痒不适",
                             :dict-value-code "YANYANGBUSHI",
                             :parent-id       62,
                             :group-code      "REACTION",
                             :dict-value-sort 5,
                             :dict-value-id   67,
                             :show            true}],
                           :dict-key-id   62}
                          {:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "鼻炎症状",
                           :list
                           [{:dict-value-name "鼻痒",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            251,
                             :dict-value-id   69,
                             :show            true}
                            {:dict-value-name "喷嚏",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            252,
                             :dict-value-id   70,
                             :show            true}
                            {:dict-value-name "流涕",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            253,
                             :dict-value-id   71,
                             :show            true}
                            {:dict-value-name "鼻塞",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            254,
                             :dict-value-id   72,
                             :show            true}
                            {:dict-value-name "鼻干",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            255,
                             :dict-value-id   73,
                             :show            true}
                            {:dict-value-name "鼻干",
                             :dict-value-code "BIGAN",
                             :parent-id       68,
                             :group-code      "REACTION",
                             :dict-value-sort 6,
                             :dict-value-id   73,
                             :show            true}],
                           :dict-key-id   68}
                          {:type          "REACTION",
                           :main-id       "1",
                           :dict-key-name "过敏性皮肤症状",
                           :list
                           [{:dict-value-name "荨麻疹",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            259,
                             :dict-value-id   81,
                             :show            true}
                            {:dict-value-name "湿疹",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            260,
                             :dict-value-id   82,
                             :show            true}
                            {:dict-value-name "皮肤瘙痒",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            261,
                             :dict-value-id   83,
                             :show            true}
                            {:dict-value-name "血管性水肿",
                             :create-time     "2019-05-01T14:02:25",
                             :id-2            262,
                             :dict-value-id   84,
                             :show            true}
                            {:dict-value-name "血管性水肿",
                             :dict-value-code "XUEGUANIXINGSHUIZHONG",
                             :parent-id       80,
                             :group-code      "REACTION",
                             :dict-value-sort 4,
                             :dict-value-id   84,
                             :show            true}],
                           :dict-key-id   80}],
                         :id                   1,
                         :later-other-dispose  nil,
                         :hy-y                 33,
                         :reaction-type        "IMMED"}
                        {:hy-x                 66,
                         :ft-y                 33,
                         :immed-other-dispose  "",
                         :later-other-reaction "大面积红点",
                         :cure-id              "1",
                         :reaction-time        "2019-05-21T10:03:00",
                         :ft-x                 22,
                         :immed-other-reaction "",
                         :list
                         [{:type          "REACTION",
                           :main-id       "2",
                           :dict-key-name "全身非特异性反应",
                           :list
                           [{:dict-value-name "发热",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            240,
                             :dict-value-id   59,
                             :show            true}
                            {:dict-value-name "关节痛",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            241,
                             :dict-value-id   61,
                             :show            true}
                            {:dict-value-name "关节痛",
                             :dict-value-code "GUANJIETONG",
                             :parent-id       58,
                             :group-code      "REACTION",
                             :dict-value-sort 3,
                             :dict-value-id   61}],
                           :dict-key-id   58}
                          {:type          "REACTION",
                           :main-id       "2",
                           :dict-key-name "哮喘症状",
                           :list
                           [{:dict-value-name "喘息",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            242,
                             :dict-value-id   63,
                             :show            true}
                            {:dict-value-name "气闭",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            243,
                             :dict-value-id   66,
                             :show            true}
                            {:dict-value-name "咽痒不适",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            244,
                             :dict-value-id   67,
                             :show            true}
                            {:dict-value-name "咽痒不适",
                             :dict-value-code "YANYANGBUSHI",
                             :parent-id       62,
                             :group-code      "REACTION",
                             :dict-value-sort 5,
                             :dict-value-id   67}],
                           :dict-key-id   62}
                          {:type          "DISPOSE",
                           :main-id       "2",
                           :dict-key-name "处理方式",
                           :list
                           [{:dict-value-name "糖皮质激素",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            245,
                             :dict-value-id   88,
                             :show            true}
                            {:dict-value-name "局部处理(冰敷)",
                             :create-time     "2019-05-03T14:03:56",
                             :id-2            246,
                             :dict-value-id   89,
                             :show            true}],
                           :dict-key-id   86}],
                         :id                   2,
                         :later-other-dispose  "抹药",
                         :hy-y                 88,
                         :reaction-type        "LATER"}]})
    
    ;;; 2.2.1 select
    
    (select [:result ALL :list ALL :list ALL :show ] data)
    
    ;;; 找到:dict-value-name
    
    (select [:result ALL :list ALL :list ALL (submap [:dict-value-name :show]) ] data)
    
    ;;; 使用transform做数据更新
    (transform [:result ALL :list ALL :list]
               (constantly "sdsdsdsdsdd")           data)
    
    ;;; 使用setval做数据更新
    
    ;;; 替换一个值
    
    (setval [:result  FIRST :hy-x ] "------" data)
    
    ;;; 替换一个collection的值
    
    (setval [:result ALL :list ALL ] "------" data)
    
    ;;; 插入一个新值
    
    (setval [:result FIRST :list FIRST :list (nthpath 1)  ] "------" data)
    
    

    Example 2: Append a sequence of elements to a nested vector

    (def data {:a [1 2 3]})
    
    ;; Manual Clojure
    (update data :a (fn [v] (into (if v v []) [4 5])))  => {:a [1 2 3 4 5]}
    
    ;; Specter
    (setval [:a END] [4 5] data)  => {:a [1 2 3 4 5]}
    
    (transform [ALL :a even?]
                  inc
                  [{:a 1} {:a 2} {:a 4} {:a 3}])
    [{:a 1} {:a 3} {:a 5} {:a 3}]
    

    相关文章

      网友评论

          本文标题:clojure上一个start最多的集合数据处理库-specte

          本文链接:https://www.haomeiwen.com/subject/ssbmqctx.html