clojure - 根据键值

时间:2017-02-15 21:36:59

标签: clojure

我想在棋盘游戏中代表Clojure中2D位置+邻居的图表。我正在使用将位置映射到邻居矢量的地图:

{[0 0] [[0 1] [1 0] [1 1]]}

我编写了一些可以为任何规模的电路板生成邻居图的函数:

(defn positions [size]
  (for [x (range 0 size) y (range 0 size)] [x y]))

(defn neighbors [size [x y]]
  (filter (fn [[x y]]
        (and (>= x 0) (< x size) (>= y 0) (< y size)))
      (-> []
          (conj [(inc x) y])
          (conj [(dec x) y])
          (conj [x (inc y)])
          (conj [x (dec y)])
          (conj [(inc x) (inc y)])
          (conj [(dec x) (dec x)]))))

(defn board-graph
  [size]
  (reduce (fn [map position] (assoc map
                                    position
                                    (neighbors size position)))
          {}
          (positions size)))

这很好用:

(board-graph 2)
=> {[0 0] ([1 0] [0 1] [1 1]), [0 1] ([1 1] [0 0]), [1 0] ([0 0] [1 1] [0 0]), [1 1] ([0 1] [1 0] [0 0])}

然而我现在想要添加到这个额外的虚拟邻居&#39;对于董事会边缘的每个董事会职位,.e.g:TOP,:BOTTOM,:LEFT,:RIGHT。所以我想:

(board-graph 2)
=> {[0 0] (:LEFT :TOP [1 0] [0 1] [1 1]), [0 1] (:LEFT :BOTTOM [1 1] [0 0]), [1 0] (:RIGHT :TOP [0 0] [1 1] [0 0]), [1 1] (:RIGHT :BOTTOM [0 1] [1 0] [0 0])}

到目前为止,这是我的尝试,但它没有正常工作,看起来真的过于复杂了:

(defn- filter-keys
  [pred map]
  (into {}
        (filter (fn [[k v]] (pred k)) map)))


(defn board-graph
  [size]
  (let [g (reduce (fn [map position] (assoc map
                                            position
                                            (neighbors size position)))
                  {}
                  (positions size))]
    (merge g
           (reduce-kv #(assoc %1 %2 (conj %3 :TOP)) {}
                      (filter-keys (fn [[x y]] (= y 0)) g))
           (reduce-kv #(assoc %1 %2 (conj %3 :BOTTOM)) {}
                      (filter-keys (fn [[x y]] (= y (dec size))) g))
           (reduce-kv #(assoc %1 %2 (conj %3 :LEFT)) {}
                      (filter-keys (fn [[x y]] (= x 0)) g))
           (reduce-kv #(assoc %1 %2 (conj %3 :RIGHT)) {}
                      (filter-keys (fn [[x y]] (= x (dec size))) g)))))

我基本上想要构建我的地图,再次检查它,并且某些键根据键的内容更新相关的值。如果不诉诸国家,我无法找到一个好方法! 有没有更惯用的方法呢?

2 个答案:

答案 0 :(得分:2)

您可以在构建返回向量的线程表达式中添加更多表达式。为了使这个读取更好,我将它从线程第一个宏->切换到“线程为”宏as->,它将符号(在本例中)c绑定到每个步骤的线程值,以便我可以在某些阶段的条件表达式中使用它:

(defn neighbors [size [x y]]
  (filter (fn [[x y]]
            (and (>= x 0) (< x size) (>= y 0) (< y size)))
          (as-> [] c
              (conj c [(inc x) y])
              (conj c [(dec x) y])
              (conj c [x (inc y)])
              (conj c [x (dec y)])
              (conj c [(inc x) (inc y)])
              (conj c [(dec x) (dec x)])
              (if (zero? x)  (conj c :TOP) c)
              (if (= size x) (conj c :BOTTOM) c)
              (if (zero? y)  (conj c :LEFT) c)
              (if (= size y) (conj c :RIGHT) c))))

每个条件表达式要么返回更新版本,要么在未满足条件时将其传递给未更改版本。

答案 1 :(得分:0)

这对我有用,但它与你原来写的不太接近。

(defn positions [size]
  (for [x (range 0 size) y (range 0 size)] [x y]))

(defn neighbors [n [x y]]
  (let [left (if (zero? x)
               :LEFT
               [(dec x) y])
        right (if (= x (dec n))
                :RIGHT
                [(inc x) y])
        up (if (zero? y)
             :TOP
             [x (dec y)])
        down (if (= y (dec n))
               :BOTTOM
               [x (inc y)])]
    (list left right up down)))

(defn board-graph [n]
  (let [keys (positions n)
        vals (map (partial neighbors n) keys)]
    (zipmap keys vals)))

然后

(clojure-scratch.core/board-graph 2)
=>
{[1 1] ([0 1] :RIGHT [1 0] :BOTTOM),
 [1 0] ([0 0] :RIGHT :TOP [1 1]),
 [0 1] (:LEFT [1 1] [0 0] :BOTTOM),
 [0 0] (:LEFT [1 0] :TOP [0 1])}
编辑:正如亚瑟指出的那样,如果你想要的话,这并不能处理对角线。