A clj snippet #6
Today we look at another threading macro which is a natural extension to the ones shipped with
condp-> works essentially just like
cond-> except that the expression is also threaded through the test expressions. This is useful when you want to conditionally thread expressions but the conditions are dependent on the current value being threaded instead of something known upfront.
One way to implement
condp-> is as follows:
One could have also used the standard
-> thread first macro instead of writing an explicit
thread-first function inside the macro. It follows a rather mundane example
If you want to use the
condp-> there is a commons library providing
condp-> as well as
condp->>. Interestingly their implementation is just a little different, but makes use of the standard
One interesting difference is that in the first version the expanded code grows exponentially with the number of test/form pairs. The later implementation remains linear with respect to test/form pairs. As the macro expansion happens before compile time this shouldn't make much of a difference. It would still be interesting if the former version has some performance degradation with respect to the later if the number of tests/forms exceeds some threshold.