Re: Threading Macro

Eduardo Bellani <[email protected]>
Newsgroups gmane.lisp.scheme.bigloo
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On 07/31/2013 10:22 AM, [email protected] wrote:
> Hi Joseph,
> 
>> I have recently come a cross a number of references to Clojure's 
>> threading macros, -> and ->>. These macros have the form: ((or ->
>> ->>) x form ...). The value of x is spliced in as the first or
>> last argument, respectively, of form with its result being
>> likewise spliced into the next form and so on. From the examples
>> I have seen, these macros can be useful, but instead of
>> replicating these macros exactly in bigloo, I decided to create a
>> generalized version called T> where _ is used to indicate where
>> values are to be spliced in. T> has the form: (T> x form1 ...) In
>> form1 and subsequent forms the _ character is used to indicate 
>> where x should be spliced.
>> 
>> Examples:
>> 
>> (T> 4 (+ _ 3) (* 3 _)) => 21
>> 
>> (T> '( 1 2 3 4) (map odd? _) (map (lambda (x) (* x 2)) _))  => (2
>> 6)
>> 
>> The definition of T> is attached. I would be interested in any
>> comments or suggestions you may have.
> I don't understand your example. I would have replaced MAP with
> FILTER, as in:
> 
> (T> '( 1 2 3 4) (filter odd? _) (map (lambda (x) (* x 2)) _))
> 
> What are the concrete use cases they have in Clojure for this
> macro? It vaguely reminds me of the monadic threads as one may find
> in Ocaml.
> 

- From what I understand, the purpose of the threading macros in clojure
is to increase readability of code, as to simulate an imperative code.
In clojure, for instance

(+ (- (* 3 4) 20) 10)

becomes

(-> (* 3 4)
    (- ,, 20)
    (+ ,, 10))


- -- 
Eduardo Belani
"The urge to save humanity is almost always a false front for the urge
to rule."
Mencken, H.L.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlH5E+0ACgkQSbLl0kCTjGmVCgCfVRHk9t++Yaj0vmRgMri+1v1P
DJoAoIM/lQohkc37iZaxRPIE+eNKrtvU
=sK56
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.