Sunday, July 13, 2014

Using Stream to simplify execution alternatives...

First thing to remember before using Stream to simplify execution alternatives : Stream(...) will evaluate all its arguments before the stream is created, but if you use the #:: notation, only the first one will be evaluated at stream creation.
Now using this to simplify conditions : So everything is working fine, and "oups" was not printed on the console.
But one of the best usage I found of this pattern, is when we want to get some information that may be available from several sources : This time we have a piece of code that is very easy to read !
Try to do the same job using only traditional "if-then-else" blocks, or pattern matching.

Traditional approach examples : Using if block suffer from a bug, the first fromX function returning a non empty result will be evaluated twice (we should have use a temporary value to keep the result of from evaluation). The pattern matching variant is very difficult to read.

No comments:

Post a Comment