<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">There’s also the <font face="Courier" class="">thrush</font> function from the point-free package, which uses the argument order of the threading macro while providing the functional style of compose.</div><div class=""><br class=""></div><div class=""><a href="http://pkg-build.racket-lang.org/doc/point-free/index.html?q=thrush#(def._((lib._point-free/main..rkt)._thrush))" class="">http://pkg-build.racket-lang.org/doc/point-free/index.html?q=thrush#%28def._%28%28lib._point-free%2Fmain..rkt%29._thrush%29%29</a></div><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 14, 2015, at 15:35, Sean Kanaley <<a href="mailto:skanaley@gmail.com" class="">skanaley@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">If "thread-through macro" refers to "<a href="http://www.greghendershott.com/2013/05/the-threading-macro.html" class="">http://www.greghendershott.com/2013/05/the-threading-macro.html</a>" then I recommend a functional alternative called "compose":<div class=""><br class=""></div><div class=""><div class="">(define print/cdr</div><div class=""> (match-lambda</div><div class=""> [(cons x xs) (print x) xs]))</div><div class=""><br class=""></div><div class="">(define print-two</div><div class=""> (compose print/cdr print/cdr))</div></div><div class=""><br class=""></div><div class="">The result is chained through naturally since the input and output type of the lower level function are equal.</div><div class=""><br class=""></div><div class="">For any number of printings</div><div class=""><br class=""></div><div class=""><div class="">(define (print-n n)</div><div class=""> (for/fold ([print-n identity])</div><div class=""> ([i n])</div><div class=""> (compose print/cdr print-n)))</div></div><div class=""><br class=""></div><div class="">(define print-two (print-n 2))</div><div class=""><br class=""></div><div class="">And for any number of anythings</div><div class=""><br class=""></div><div class=""><div class="">(define (iterate f n)</div><div class=""> (for/fold ([chain identity])</div><div class=""> ([i n])</div><div class=""> (compose f chain)))</div><div class=""><br class=""></div><div class="">(define print-two (iterate print/cdr 2))</div></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Mar 12, 2015 at 2:25 PM, Don Green <span dir="ltr" class=""><<a href="mailto:infodeveloperdon@gmail.com" target="_blank" class="">infodeveloperdon@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="">;Design A:<br class=""></div>;Rating: 1 out of 10<br class="">;Poor because it uses set!<br class=""><div class=""><div class="">(define print-two <br class=""> (lambda (f)<br class=""> (print (first f))<br class=""> (set! f (rest f))<br class=""> (print (first f))<br class=""> (set! f (rest f))<br class=""> f))<br class=""><br class="">(void (print-two '(1 2))) ;=> 12<br class="">;-------------------------<br class=""><div class=""><div class="">;Design B:<br class=""></div><div class="">;Rating: 2 out of 10<br class=""></div>;Better because it nests expressions to avoid using set!<br class=""></div><div class="">;Poor because it less readable.<br class="">(define print-two<br class=""> (lambda (f)<br class=""> (print (first f))<br class=""> (print (first (rest f)))<br class=""> f))<br class=""><br class="">(void (print-two '(1 2))) ;=> 12<br class=""></div><div class="">When called in situations that allow one expression only, enclose call within a 'begin' expression.<br class=""></div><div class="">;-------------------------<br class=""></div><div class="">;Design C:<br class=""></div><div class="">;Rating: 3 out of 10<br class=""></div>;Is this an even better design because it is more readable than nesting expressions as in Design B above?<br class=""><div class="">(define (print-two f)<br class=""> (let* ([_ (print (first f))]<br class=""> [f (rest f)]<br class=""> [_ (print (first f))]<br class=""> [f (rest f)])<br class=""> f))<br class=""></div>(void (print-two '(1 2))) ;=> 12<br class="">;-------------------------<br class=""></div><div class="">My questions are: <br class="">"Can you recommend a better method?"<br class=""></div><div class="">"Can you recommend a better method using 'define with lambda'?"<br class="">"Does your better method use a macro?"<br class=""></div><div class="">"Does your better method use a thread-through macro?" If so, could you please provide the definition of the thread-through macro.<br class=""></div><div class="">THANKS!<br class=""></div><div class=""><br class=""></div></div></div>
<br class="">____________________<br class="">
Racket Users list:<br class="">
<a href="http://lists.racket-lang.org/users" target="_blank" class="">http://lists.racket-lang.org/users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
____________________<br class=""> Racket Users list:<br class=""> <a href="http://lists.racket-lang.org/users" class="">http://lists.racket-lang.org/users</a><br class=""></div></blockquote></div><br class=""></body></html>