3 ms·
Indeed having a chain is often nicer than a lot of nested stuff. And when your base language is an abstract syntax tree, it becomes pretty easy to write a macro
by Jach 12y ago
Indeed having a chain is often nicer than a lot of nested stuff. And when your base language is an abstract syntax tree, it becomes pretty easy to write a macro to let you flatten a lot of things out (among a bunch of other neat things). If your complaint with a lisp is that there's too much nesting, you haven't seen enough lisp. Pretty much every lisp has an infix-math library somewhere, for the popular lisps there's probably at least a dozen, and they usually allow mixing in prefix-math as well because nuts to typing "a + b + c + d + e" when I can just type "(+ a b c d e)" or if I want to call functions anywhere in the middle. There's no reason you couldn't write your example using a threading macro in Clojure, assuming your functions are defined such that they take input explicitly as the last argument instead of reading a common stdin. (Or if they take it as the first argument, you use a different macro but the same syntax.) Your code just becomes
(->> (command) (grep "error") sort uniq (wc :-l)
Does anyone think (wc :-l (uniq (sort (grep "error" (command)))))
is a better option? And instead of typing that out manually, I just used the REPL.
user=> (use 'clojure.walk)
nil
user=> (macroexpand-all '(->> (command) (grep "error") sort uniq (wc :-l)))
(wc :-l (uniq (sort (grep "error" (command)))))
Even without macros you can still setup nice-looking chaining with generators and coroutines. "Too much nesting" seems like the wrong complaint.