->, which violates the evaluation when detecting a zero / false return

I want the input stream through a series of functions, which makes the β†’ operator. However, if any of the functions returns nil / false, I would like to break the evaluation and return an error message. How to do this, is there some kind of statement / macro that provides this functionality?

+7
source share
2 answers

Try the following: -?> From the documentation:

 (-?> "foo" .toUpperCase (.substring 1)) returns "OO" (-?> nil .toUpperCase (.substring 1)) returns nil 

If you use the macro -> for the second example, you will definitely get a NullPointerException .

+11
source

There is also maybe-m monad in clojure.algo.monads . Being part of the monads structure, it is heavier than the macro -?> , So it makes sense to use maybe-m if you use monads anyway or if your computational graph is more complex than a simple chain of functions.

Unlike streaming macros, a domonad composition can handle several argument functions that take arguments from the previous several steps of the calculation:

 (domonad maybe-m [a 1 b nil c (* ab)] c) 

In this example (* ab) will not be evaluated, since b is zero. The whole expression returns nil instead of throwin an exception to the attempt to multiply by nil.

+3
source

All Articles