[racket] thread-through ?
On Feb 20, 2015, at 6:56 PM, Don Green <infodeveloperdon at gmail.com> wrote:
> I looked in racket documentation and discussion archives for a thread-through function as illustrated below but without success.
> Any suggestions where I should look? OR please explain that line below only.
> Looks like it is declaring thread-safe variables x e and any others I care to list. Is that correct?
>
> ;Use a macro:
> (thread-through x e …)
> ==
> (let* ([x e] …) x)
> ;——————————
If you have this macro definition:
(define-simple-macro
(thread-through x e ...)
(let* ([x e] ...) x))
Then you can do things like this: (contrived example)
(thread-through
lst '(0 1 2 3 4 5 6 7 8 9 10)
(map (λ (x) (list x (modulo (expt 11 x) 13))) lst)
(sort lst < #:key second)
(map first lst)) ; '(0 7 4 2 3 5 9 8 10 1 6)
And this: (adapted from an example in http://docs.racket-lang.org/guide/set_.html#%28part._using-set%21%29)
(thread-through
tree 0
(list tree 1 tree)
(list tree 2 tree)
(list tree 3 tree)) ; '(((0 1 0) 2 (0 1 0)) 3 ((0 1 0) 2 (0 1 0)))
And this: (adapted from http://pkg-build.racket-lang.org/doc/rackjure/index.html#%28part._.Threading_macros%29)
(thread-through
x #"foobar"
(bytes-length x)
(number->string x 16)
(string->bytes/utf-8 x)) ; #"6"
It has nothing to do with thread-safe variables.
It is similar in spirit to ~> from rackjure, or -> from clojure, or ~> or thrush+ from point-free, if you want to look at those.