From 989a8fe91dc598f5ea5f2c74f3451a60699a8145 Mon Sep 17 00:00:00 2001 From: Simon Brooke <simon@journeyman.cc> Date: Sat, 30 May 2020 23:46:55 +0100 Subject: [PATCH] #2: done --- src/walkmap/path.clj | 17 +++++++++-------- src/walkmap/polygon.clj | 13 +++++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/walkmap/path.clj b/src/walkmap/path.clj index b4ca67a..92222a4 100644 --- a/src/walkmap/path.clj +++ b/src/walkmap/path.clj @@ -25,13 +25,12 @@ (defn path "Return a path constructed from these `vertices`." [& vertices] - (if - (every? vertex? vertices) - {:vertices vertices :walkmap.id/id (keyword (gensym "path")) :kind :path} + (when-not (every? vertex? vertices) (throw (IllegalArgumentException. (str "Each item on path must be a vertex: " - (s/join " " (map kind-type vertices))))))) + (s/join " " (map kind-type (remove vertex? vertices))))))) + {:vertices vertices :walkmap.id/id (keyword (gensym "path")) :kind :path}) (defn polygon->path "If `o` is a polygon, return an equivalent path. What's different about @@ -41,10 +40,12 @@ If `o` is not a polygon, will throw an exception." [o] - (if - (polygon? o) - (assoc (dissoc o :vertices) :kind :path :vertices (concat (:vertices o) (list (first (:vertices o))))) - (throw (IllegalArgumentException. "Not a polygon!")))) + (when-not (polygon? o) + (throw (IllegalArgumentException. (str "Not a polygon: " (kind-type o))))) + (assoc (dissoc o :vertices) + :kind :path + ;; `concat` rather than `conj` because order matters. + :vertices (concat (:vertices o) (list (first (:vertices o)))))) (defn path->edges "if `o` is a path, a polygon, or a sequence of vertices, return a sequence of diff --git a/src/walkmap/polygon.clj b/src/walkmap/polygon.clj index 36357ab..b68db2d 100644 --- a/src/walkmap/polygon.clj +++ b/src/walkmap/polygon.clj @@ -1,6 +1,8 @@ (ns walkmap.polygon "Essentially the specification for things we shall consider to be polygons." - (:require [walkmap.vertex :refer [vertex?]])) + (:require [clojure.string :as s] + [walkmap.utils :refer [kind-type]] + [walkmap.vertex :refer [vertex?]])) (defn polygon? "True if `o` satisfies the conditions for a polygon. A polygon shall be a @@ -16,4 +18,11 @@ (:walkmap.id/id o) (or (nil? (:kind o)) (= (:kind o) :polygon))))) - +(defn polygon + [vertices] + (when-not (every? vertex? vertices) + (throw (IllegalArgumentException. + (str + "Each item on path must be a vertex: " + (s/join " " (map kind-type (remove vertex? vertices))))))) + {:vertices vertices :walkmap.id/id (keyword (gensym "poly")) :kind :polygon})