Re: Avoiding nested ifs...

2016-05-27 Thread John Szakmeister
On Thu, May 26, 2016 at 5:41 PM, Erik Assum wrote: > Not being good at reading other peoples mind, I’ll give my guess as to what > Timothy was trying to suggest: > > If you define your input as a map with keys such as: > > {:type :switched ; can be :switched :dual or :something > :pos 54} > > Yo

Re: Avoiding nested ifs...

2016-05-27 Thread hiskennyness
On Thursday, May 26, 2016 at 10:50:24 AM UTC-4, John Szakmeister wrote: > > I'm very much a fan of bailing out early in imperative > programming as it helps to avoid a bunch of nested if conditions > that are to follow and read. This typically comes up when > checking arguments to ensure that

Re: Avoiding nested ifs...

2016-05-27 Thread John Szakmeister
On Thu, May 26, 2016 at 7:09 PM, Mark Engelberg wrote: > On Thu, May 26, 2016 at 1:29 PM, John Szakmeister > wrote: >> >> >> Yeah, cond is definitely useful here, but not in general. >> > > cond is useful in general, just not the cond that is built-in to Clojure. Sorry, I didn't mean it that way

Re: Avoiding nested ifs...

2016-05-26 Thread Mark Engelberg
On Thu, May 26, 2016 at 1:29 PM, John Szakmeister wrote: > > Yeah, cond is definitely useful here, but not in general. > > cond *is* useful in general, just not the cond that is built-in to Clojure. About 5 years ago, Christophe Grand pointed out in a blog post that cond, augmented with a few ex

Re: Avoiding nested ifs...

2016-05-26 Thread Erik Assum
Not being good at reading other peoples mind, I’ll give my guess as to what Timothy was trying to suggest: If you define your input as a map with keys such as: {:type :switched ; can be :switched :dual or :something :pos 54} You can make something like: (defmulti verify-pos-for :type) (def

Re: Avoiding nested ifs...

2016-05-26 Thread John Szakmeister
(sorry for the repeat Nathan, this was meant to go to the list too) On Thu, May 26, 2016 at 1:43 PM, Nathan Davis wrote: > First off, I should say that you should first consider alternative > approaches before considering the options below. For example, cond seems > well-suited for this particul

Re: Avoiding nested ifs...

2016-05-26 Thread John Szakmeister
On Thu, May 26, 2016 at 2:12 PM, Timothy Baldridge wrote: > 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 o

Re: Avoiding nested ifs...

2016-05-26 Thread John Szakmeister
On Thu, May 26, 2016 at 1:47 PM, Sean Corfield wrote: [snip] > 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-d

Re: Avoiding nested ifs...

2016-05-26 Thread Michael Ball
I use a little short circuited threading macro to deal with this. Here's a gist with the macro named short-> and how I would write out your first 2 validation steps. https://gist.github.com/mikeball/10cc64fe97c119671918fb2d1d8b4118 The new spec stuff looks really interesting, haven't had a ch

Re: Avoiding nested ifs...

2016-05-26 Thread Timothy Baldridge
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

Re: Avoiding nested ifs...

2016-05-26 Thread Sean Corfield
On 5/26/16, 7:50 AM, "John Szakmeister" 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

Re: Avoiding nested ifs...

2016-05-26 Thread Nathan Davis
First off, I should say that you should first consider alternative approaches before considering the options below. For example, cond seems well-suited for this particular case: (defn verify-position[pos type] (cond (nil? pos) true (or (> pos 90) (< pos 0)) false

Re: Avoiding nested ifs...

2016-05-26 Thread Gary Trakhman
Ah, yea, having a 'data spec' specifically for validation is above and beyond what I meant, which was having your domain contained within a single data structure that can be validated using a number of possible techniques. That validation would be separate from the code that's pulling it apart down

Re: Avoiding nested ifs...

2016-05-26 Thread John Szakmeister
On Thu, May 26, 2016 at 10:55 AM, Gary Trakhman wrote: > I think the idiomatic way to handle this in clojure is to do all your > validation upfront on a single descriptive data structure, perhaps in a > single function, then bail early. That has the added advantage of being > able to report multi

Avoiding nested ifs...

2016-05-26 Thread John Szakmeister
I'm very much a fan of bailing out early in imperative programming as it helps to avoid a bunch of nested if conditions that are to follow and read. This typically comes up when checking arguments to ensure that they're valid (in range, of acceptable values, etc). One of the major stumbling block

Re: Avoiding nested ifs...

2016-05-26 Thread Gary Trakhman
I think the idiomatic way to handle this in clojure is to do all your validation upfront on a single descriptive data structure, perhaps in a single function, then bail early. That has the added advantage of being able to report multiple errors, instead of just the first, and is well supported by