I would suggest reviewing your data model a bit. One of the problems you are experiencing is that you are overloading the inputs to your function. And since they are overloaded you have to create a state machine of sorts to parse out if the data is valid. Another approach is to use namespaced keys in a map then dispatching on the contents of the map. This removes a lot of ambiguity in the code.
Then you can use something like clojure.spec to validate your data: (s/def :pos.switch/value #{0 90}) (s/def :pos.slider/value (set (range 90))) (s/def :switch/change (s/or :nil nil? :switch (s/keys :req [:pos.switch/value]) :slider (s/keys :req [:pos.slider/value]))) (s/valid? :switch/change {:pos.switch/value 42}) ; => false (s/valid? :switch/change {:pos.slider/value 42}) ; => true (s/conform :switch/change {:pos.switch/value 0}) ; => [:switch {:pos.switch/value 0}] (s/conform :switch/change nil) ; => [:nil nil] (s/conform :switch/change {:pos.slider/dual 0}) ; => :clojure.spec/invalid (s/exercise :switch/change) ; => Generates 10 example data sets And as you see from this example, you can use specs to dispatch, validate and even generate test data for your application. On Thu, May 26, 2016 at 11:47 AM, Sean Corfield <s...@corfield.org> wrote: > On 5/26/16, 7:50 AM, "John Szakmeister" <jszakmeis...@gmail.com on behalf > of j...@szakmeister.net> wrote: > >def verify_position(pos, type): > > # It's acceptable to have a None value--it just means don't > > # change the position for the axis. > > if pos is None: > > return True > > > > # Anything outside our limits is invalid. > > if (pos > 90) or (pos < 0): > > return False > > > > if type == 'switched' and pos not in (0, 90): > > # Switched axes only allow 0 and 90, and nothing in > > # between. > > return False > > > > if type == 'dual': > > # We can't control the value on this axis, so anything > > # other than None is invalid. > > return False > > > > return True > > (defn verify-position [pos type] > (cond (nil? pos) true > (or (> pos 90) (< pos 0)) false > (and (= type “switched”) > (not (in-range pos 0 90))) false > (= type “dual”) false > :else true)) > > (or :else (do-stuff-to-verified-position pos type) if that branch is more > complex?) > > A pattern that I’ve also started using is something like this: > > (defn guarded-process [data] > (some-> data > first-guard > second-guard > (another-guard :with “parameters”) > (process-the-data 1 2 3 4))) > > That works well for a process that operates on non-nil data and then you > write the guards to return the data if it’s valid or nil if it isn’t. > > Sean Corfield -- (904) 302-SEAN > An Architect's View -- http://corfield.org/ > > "If you're not annoying somebody, you're not really alive." > -- Margaret Atwood > > > > > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with > your first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.