Reputation: 5944
the code following code does not work because re-find accepts a string as the first argument and a regex as the second.
(-> "hello"
.toUpperCase
(re-find #".$"))
The code would work if I wrapped re-find like this:
(defn re-find2 [s r]
(re-find r s))
If I replace re-find with re-find2 now, I get what I expected: a capitalized "O".
How could I solve this without wrapping re-find?
Upvotes: 3
Views: 819
Reputation: 4235
As mentioned, you can use thread-last if all your functions only have one argument or take the result as the last argument. However, if you also have functions which have multiple arguments and you must pass the result in a specific position which cannot be handled by -> or ->>, then possibly the as-> macro would be your best bet.
This macro allows you to specify a binding for the result from each function call. As the result now has a name, you can control where that parameter is positioned. Have a look at this blog post on threading macros
Upvotes: 6
Reputation: 6086
For this specific case you could use the thread-last macro instead.
(->> "hello" .toUpperCase (re-find #".$"))
If you really need to switch between passing as first and last argument you can chain the threading macros:
(-> "hello" .toUpperCase (->> (re-find #".$")))
Upvotes: 4