Logical task combinators

When I had to use Erlang for the very first time it was a brand new language and I was in collage. Back then Erlang did not have a logical not function so we had to define our own in order to make our code readable. At least given the knowledge me and my lab partner had at the time. So the other week I had a deja vu moment since I had a Task<bool> that I wanted to negate. At first it felt real weird and I ended up solving my problem in a different way. But then I started to think.

The concept of logical task combinators actually made sense. The logical not of Task<bool> may not be the most useful method, but logical and and or for several Task<bool> actually made a lot of sense to me.

You are probably familiar with how most programming languages evaluate logical expressions. The fact that there is a predefined order of evaluation of logical expressions lets you express things in a simple way and combine expressions that depend on each other such as if (foo != null && foo.IsBar). But when it comes to logically combine a number of Task<bool> I decided left to right evaluation is not important. What you really care about is getting a result as fast as possible which is why I think the logical Or of tasks should result in a completed task as soon as there is one task that completes with a true result. And similar for logical And and a task resulting in false.

For failures I think it is reasonable to assume the first failure should fail your logical Or/And. And if you agree with me that these things actually would be useful you don't need to write them. I've already added them to my toolbox for your pleasure.

No comments:

Post a Comment