Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 删除给定midje测试中的冗余代码_Clojure - Fatal编程技术网

Clojure 删除给定midje测试中的冗余代码

Clojure 删除给定midje测试中的冗余代码,clojure,Clojure,在下面的代码中,有很多冗余代码,我在左侧对所有三个事实执行相同的操作。在右手边我也有同样的挡块。我如何处理这种冗余 (facts "Data is grouped by the given keys." (fact "The grouping keys are a subset of the :month/:day keys in data" (group-data data :day) => (fn [d]

在下面的代码中,有很多冗余代码,我在左侧对所有三个事实执行相同的操作。在右手边我也有同样的挡块。我如何处理这种冗余

(facts "Data is grouped by the given keys."
       (fact "The grouping keys are a subset of the :month/:day keys in data"
             (group-data data :day) => (fn [d]
                                           (let [months (map :day data)
                                                 grouped-by-keys (keys d)]
                                             (subset? (set grouped-by-keys) (set months)))))
       (fact "All the grouping keys are distinct"
             (group-data data :day) => (fn [d]
                                           (let [months (map :day data)
                                                 grouped-by-keys (keys d)]
                                             (apply distinct? grouped-by-keys))))
       (fact "All the distinct keys from data are present in grouping keys"
             (group-data data :day) => (fn [d]
                                           (let [months (map :day data)
                                                 grouped-by-keys (keys d)]
                                             (empty? (difference (set months) (set grouped-by-keys)))))))
宏可以帮助:

(defmacro day-facts [facts]
  (let [f (fn [[name expr]]
            `(~'fact ~name
                     (~'group-data ~'data :day) ~'=> (fn [~'d]
                                                       (let [~'months (map :day ~'data)
                                                             ~'grouped-by-keys (keys ~'d)]
                                                         ~expr))))]
    `(~'facts "Data is grouped by the given keys."
              ~@(map f facts))))


(day-facts [["The grouping keys are a subset of the :month/:day keys in data" (subset? (set grouped-by-keys) (set months))]
            ["All the grouping keys are distinct" (apply distinct? grouped-by-keys)]
            ["All the distinct keys from data are present in grouping keys" (empty? (difference (set months) (set grouped-by-keys)))]])
宏可以帮助:

(defmacro day-facts [facts]
  (let [f (fn [[name expr]]
            `(~'fact ~name
                     (~'group-data ~'data :day) ~'=> (fn [~'d]
                                                       (let [~'months (map :day ~'data)
                                                             ~'grouped-by-keys (keys ~'d)]
                                                         ~expr))))]
    `(~'facts "Data is grouped by the given keys."
              ~@(map f facts))))


(day-facts [["The grouping keys are a subset of the :month/:day keys in data" (subset? (set grouped-by-keys) (set months))]
            ["All the grouping keys are distinct" (apply distinct? grouped-by-keys)]
            ["All the distinct keys from data are present in grouping keys" (empty? (difference (set months) (set grouped-by-keys)))]])