Post by Alexander D. KnauthI 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?
(thread-through x e …)
==
(let* ([x e] …) x)
;——————————
(define-simple-macro
(thread-through x e ...)
(let* ([x e] ...) x))
I probably should start with a simpler example. This:
(thread-through x 1 (add1 x))
Will evaluate to the same thing as:
(add1 1)
This:
(thread-through x 1 (add1 x) (number->string x))
Will evaluate to the same thing as:
(number->string (add1 1))
This:
(thread-through x ‘foo (symbol->string x) (string->list x) (reverse x) (list->string x))
Will evaluate to the same thing as:
(list->string (reverse (string->list (symbol->string ‘foo))))
And If you wanted to do something like this:
(map first
(sort (map (λ (x) (list x (modulo (expt 11 x) 13)))
'(0 1 2 3 4 5 6 7 8 9 10))
< #:key second))
Post by Alexander D. KnauthThen 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 if you wanted to do something like this:
(let ([tree 0])
(set! tree (list tree 1 tree))
(set! tree (list tree 2 tree))
(set! tree (list tree 3 tree))
tree)
Post by Alexander D. KnauthAnd 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)))
Or if you wanted to do something like this:
(string->bytes/utf-8 (number->string (bytes-length #”foobar”) 16))
Post by Alexander D. KnauthAnd 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.
____________________
http://lists.racket-lang.org/users
____________________
Racket Users li