(setv result (-> (+ 1 3 88)
(/ 2)
(- 8))) (setv result (-> (+ 1 3 88)
(/ 2)
(- 8)))While I see the awesomeness in threading macros, I'm usually left pondering a while on where the result actually goes in the next s-expression. How long did it take you to get used to that? Would you prefer someting like `as->` for readability?
You most certainly can have explicit variable to thread through expressions, like this:
(it-> (form1 it) (form2 3 it 4) ...)
where it means "previous expression". I think there's a Clojure library which already implements this. You can use that if you want.This:
(- (bytes-ref (string->bytes/utf-8 (symbol->string 'abc)) 1) 2)
Becomes this:
(~> 'abc
symbol->string
string->bytes/utf-8
(bytes-ref 1)
(- 2))
https://docs.racket-lang.org/threading/index.html> For example the threading macros are ~> and ~>> (using ~ instead of -) because Racket already uses -> for contracts. Plus as Danny Yoo pointed out to me, ~ is more "thready".
http://docs.racket-lang.org/rackjure/index.html#%28part._.Ba...
More thready indeed :-p
What do you advise in the case the expression tree is a little bit more complex?
For example: (setv result (- (/ (+ 1 3 88) 2) (* 8 (+ 3 2)))
(setv e0 (-> (+ 1 3 88) (/ 2)))
(setv e1 (-> (+ 3 2) (* 8)))
(setv result (- e0 e1)) (setv e0 (-> (+ 1 3 88) (/ 2)))
e1 (-> (+ 3 2) (* 8))
result (- e0 e1))
like setq and setf in CL. (setv result (- (/ (+ 1 3 88)
2)
(* 8
(+ 3 2)))
Another possibility is to use a (let ...) to give nested values temporary names, like: (setv result (let ((first (/ (+ 1 3 88) 2))
(second (* 8 (+ 3 2))))
(- first second)))
It helps if "first" and "second" have meaningful names like "velocity" or "force" that can describe what they are.