Generalizing -> & ->>

142 views
Skip to first unread message

AndrewC.

unread,
Oct 29, 2009, 4:35:07 PM10/29/09
to Clojure
Here's a macro that generalizes the two 'threading' macros -> and ->>.

It works by using the idea of 'nesting marker' (from Oz) to specify
where the result of the previous form should be nested in the
subsequent form.

(defmacro -$>
"Threads the expr through the forms. Inserts x into
the first form at the position marked by the $ symbol.
If the second form is not a list then it behaves as ->.
If there are more forms, inserts the first form into the second
form at the position marked by the $ symbol, etc."
([x form] (if (seq? form)
(let [split (split-with (partial (complement =) '$)
form)]
`(~@(first split) ~x ~@(rest (second split))))
(list form x)))
([x form & more] `(-$> (-$> ~x ~form) ~@more)))

If this is any use to anyone (esp. anyone who has a CA) then consider
it public domain.

Any improvements appreciated!

Daniel Werner

unread,
Oct 31, 2009, 9:13:12 AM10/31/09
to Clojure
On Oct 29, 9:35 pm, "AndrewC." <mr.bl...@gmail.com> wrote:
> Here's a macro that generalizes the two 'threading' macros -> and ->>.

There have been multiple discussions on this group where similar
operators have been proposed, with some implementations very closely
matching this one. If the demand is so high, maybe it is finally time
to add one of the proposals to clojure.contrib.core (or even
clojure.core)?

Personally, I prefer the syntax of Andrew's implementation to the ones
posted in the past.

John Harrop

unread,
Oct 31, 2009, 9:37:38 AM10/31/09
to clo...@googlegroups.com
The syntax, perhaps; the names are, at least to me, completely meaningless, unlike e.g. comp, partial, and friends from clojure.core that do similar things. 

Sean Devlin

unread,
Nov 2, 2009, 10:13:02 AM11/2/09
to Clojure
This is slightly unrealted, but how does one pronounce ->, ->> and the
like? Is this documented?

On Oct 31, 8:37 am, John Harrop <jharrop...@gmail.com> wrote:
> On Sat, Oct 31, 2009 at 9:13 AM, Daniel Werner <
>

Alex Osborne

unread,
Nov 2, 2009, 8:43:19 PM11/2/09
to clo...@googlegroups.com
Sean Devlin wrote:
> This is slightly unrealted, but how does one pronounce ->, ->> and the
> like? Is this documented?

The doc-strings usually give you a nice hint. I usually use "thread"
for -> and "thread last" for ->>. The actual symbols I think of as
"arrow" and "double arrow".

Then -?> in contrib is "short-circuiting thread". Not sure about the
symbol, perhaps "questionable arrow"? ;-)

AndrewC.

unread,
Nov 3, 2009, 7:57:39 AM11/3/09
to Clojure
I'd probably call the macro I wrote 'nest' if it weren't for the
precedent set by the other two.

Laurent PETIT

unread,
Nov 12, 2009, 6:03:33 PM11/12/09
to clo...@googlegroups.com
2009/11/3 Alex Osborne <a...@meshy.org>:
The question mark ? is there to "mimic" (somewhat) what one can find
in OO languages such as groovy (I think it's groovy, is it ?) :

someObject.?propA.?prop2

where .? will check if the object is null before trying to get a
property (or method) on it. If null : returns null, if not null,
returns the property etc.

Initially i wanted to name it ->? but the final ? is by convention
reserved for predicates, so Rich suggested -?> (and also .?. for the
.. equivalent).

Wilson MacGyver

unread,
Nov 13, 2009, 12:59:39 AM11/13/09
to clo...@googlegroups.com
Yes, it's groovy, and it's "?." It's called safe navigation operator

http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en



--
Omnem crede diem tibi diluxisse supremum.

Laurent PETIT

unread,
Nov 13, 2009, 1:27:49 AM11/13/09
to clo...@googlegroups.com
Oh yes, thanks for refreshing my memory.
And indeed it makes sense to place the question mark in the "questioned" side :)

2009/11/13 Wilson MacGyver <wmac...@gmail.com>:

Roman Roelofsen

unread,
Dec 3, 2009, 10:31:48 AM12/3/09
to clo...@googlegroups.com
Are there any plans to add -$> to core or contrib?

2009/11/13 Laurent PETIT <lauren...@gmail.com>:

Stephen C. Gilardi

unread,
Dec 3, 2009, 10:41:23 AM12/3/09
to clo...@googlegroups.com

On Dec 3, 2009, at 10:31 AM, Roman Roelofsen wrote:

Are there any plans to add -$> to core or contrib?

The rules on contrib are that the work must be original to the author. Even with Andrew's disclaimer that it be considered public domain, he would still need a contributor agreement in place to get this incorporated into contrib.

I haven't tried using -$> yet, but based on its description it seems like it would be a useful addition to contrib.

--Steve

Meikel Brandmeyer

unread,
Dec 3, 2009, 11:02:32 AM12/3/09
to Clojure
Hi,

On Dec 3, 4:41 pm, "Stephen C. Gilardi" <squee...@mac.com> wrote:

> > Are there any plans to add -$> to core or contrib?
>
> The rules on contrib are that the work must be original to the author. Even with Andrew's disclaimer that it be considered public domain, he would still need a contributor agreement in place to get this incorporated into contrib.
>
> I haven't tried using -$> yet, but based on its description it seems like it would be a useful addition to contrib.

There is also an old discussion about this: pipe, let->, pipe-as, and
what not

http://groups.google.com/group/clojure/browse_thread/thread/66ff0b89229be894/c3d4a6dae45d4852?q=pipe+group:clojure#c3d4a6dae45d4852

Sincerely
Meikel

Wojciech Kaczmarek

unread,
Dec 3, 2009, 3:30:55 PM12/3/09
to clo...@googlegroups.com
On Thu, Dec 3, 2009 at 17:02, Meikel Brandmeyer <m...@kotka.de> wrote:
>
>>> Are there any plans to add -$> to core or contrib?
>>
>> The rules on contrib are that the work must be original to the author. Even with Andrew's disclaimer that it be considered public domain, he would still need a contributor agreement in place to get this incorporated into contrib.
>>
>> I haven't tried using -$> yet, but based on its description it seems like it would be a useful addition to contrib.
>
> There is also an old discussion about this: pipe, let->, pipe-as, and
> what not
>
> http://groups.google.com/group/clojure/browse_thread/thread/66ff0b89229be894/c3d4a6dae45d4852?q=pipe+group:clojure#c3d4a6dae45d4852

+1 for pipe-as in contrib.

But frankly I'd be glad seeing *any* of these macros in contrib.macros
(this one seems quite abandoned with 3 macros only, huh?) or wherever
in contrib. Let the contributors decide & go with it, that will push
it to use, for good.

I wrote a bunch of similar stuff but not so general; it arises from a
need to control order of passed forms while sticking to 'threading'
style, as this style helps in grasping how data is transformed (btw
it's a thought raised a couple of times, see eg. recent blog of Tim
Bray cited here).

It'd be very cool to see these threading augmentations standarized a bit.

cheers
Wojtek

John Harrop

unread,
Dec 3, 2009, 11:17:53 PM12/3/09
to clo...@googlegroups.com
On Thu, Dec 3, 2009 at 10:41 AM, Stephen C. Gilardi <sque...@mac.com> wrote:
On Dec 3, 2009, at 10:31 AM, Roman Roelofsen wrote:
Are there any plans to add -$> to core or contrib?
The rules on contrib are that the work must be original to the author. Even with Andrew's disclaimer that it be considered public domain, he would still need a contributor agreement in place to get this incorporated into contrib.

Why?

(Having to use paper mail is especially a high hurdle for what is otherwise a purely online activity.)

Meikel Brandmeyer

unread,
Dec 4, 2009, 6:51:41 AM12/4/09
to Clojure
Hi,

On 4 Dez., 05:17, John Harrop <jharrop...@gmail.com> wrote:
> > The rules on contrib are that the work must be original to the author. Even
> > with Andrew's disclaimer that it be considered public domain, he would still
> > need a contributor agreement in place to get this incorporated into contrib.
>
> Why?
>
> (Having to use paper mail is especially a high hurdle for what is otherwise
> a purely online activity.)

Because that are the rules. Eg. in Germany there is no public domain
(not allowed by the law). So "public domain" should not be an
argument. Rich wants to be sure, that there will not be any legal
issues later on. Understandable, I think. (And a good idea, BTW)

Sincerely
Meikel

John Harrop

unread,
Dec 4, 2009, 7:33:09 AM12/4/09
to clo...@googlegroups.com
On Fri, Dec 4, 2009 at 6:51 AM, Meikel Brandmeyer <m...@kotka.de> wrote:
Hi,

On 4 Dez., 05:17, John Harrop <jharrop...@gmail.com> wrote:
> > The rules on contrib are that the work must be original to the author. Even
> > with Andrew's disclaimer that it be considered public domain, he would still
> > need a contributor agreement in place to get this incorporated into contrib.
>
> Why?
>
> (Having to use paper mail is especially a high hurdle for what is otherwise
> a purely online activity.)

Because that are the rules.

Circular argument. Those are the rules because those are the rules? That doesn't satisfy me any more than it does my three-year-old.
 
Eg. in Germany

I'm not in Germany.
 
Rich wants to be sure, that there will not be any legal
issues later on. Understandable, I think. (And a good idea, BTW)

The problem is that it is an unreasonably high barrier to entry. There MUST be an electronic-only way (and it must not require a cell phone, CC#, &c.) if the full potential of this community is to be unleashed upon clojure-contrib. In particular, there should be a way to participate pseudonymously for those people that (unlike myself) value their privacy sufficiently not to want to even post here under their real names.

Miron Brezuleanu

unread,
Dec 4, 2009, 8:57:24 AM12/4/09
to clo...@googlegroups.com
Hello,
Remember SCO & IBM & Novell & Linux & many others? I assume this is
why many open source projects today require CAs.


--
Miron Brezuleanu

John Harrop

unread,
Dec 4, 2009, 9:04:14 AM12/4/09
to clo...@googlegroups.com
Interesting, but does not address the needs mentioned in the quoted paragraph. 

Richard Newman

unread,
Dec 4, 2009, 2:49:03 PM12/4/09
to clo...@googlegroups.com
> The problem is that it is an unreasonably high barrier to entry.
> There MUST be an electronic-only way (and it must not require a cell
> phone, CC#, &c.) if the full potential of this community is to be
> unleashed upon clojure-contrib. In particular, there should be a way
> to participate pseudonymously for those people that (unlike myself)
> value their privacy sufficiently not to want to even post here under
> their real names.

Raising the barrier to entry stops people pseudonymously contributing
code they don't own. If you're not even willing to stand behind it
with your own name, and someone sues Rich, he has little recourse.
"Yeah, some dude named Gecko45 contributed this library. I don't know
his real name or where he lives, sorry.".

I'm sure Rich would be happy to avoid posting a contributor's name if
they didn't want it public.

Yes, this makes it more difficult for someone to just email a fix for
a small bug. However, I quite understand Rich's reasons for doing it.
I don't think it creates an unreasonably high barrier to entry, and I
don't see why there MUST be an electronic-only way. If people don't
want to submit a CA, they're welcome to put their libraries on GitHub
like everyone else.

Brendan Ribera

unread,
Dec 4, 2009, 1:28:43 PM12/4/09
to Clojure
> I'm not in Germany.

I presume the concern isn't about you, but rather about the Germans
who can't consider the code as free to use. I imagine the idea is to
make the source truly open, everywhere.


> The problem is that it is an unreasonably high barrier to entry. There MUST
> be an electronic-only way (and it must not require a cell phone, CC#, &c.)
> if the full potential of this community is to be unleashed upon
> clojure-contrib. In particular, there should be a way to participate
> pseudonymously for those people that (unlike myself) value their privacy
> sufficiently not to want to even post here under their real names.

You have to weigh your desire for ease and privacy against your desire
for the source to be open everywhere. I'd bet that some countries
don't honor electronic signatures as legally binding. Similarly, I'd
doubt that a pseudonymous entity can be considered a copyright holder.
Again, this falls back on the public domain issue.

Naturally, I'm not a lawyer. I just like to throw around theories.

Cliff Wells

unread,
Dec 4, 2009, 5:23:58 PM12/4/09
to clo...@googlegroups.com
What isn't clear to me is exactly what a piece of paper provides that an
electronic form doesn't (aside from inconvenience). I don't see any
requirement for notarization, registered mail, etc, so the paper is
simply worth a bit less than it was before it was printed on. Any
contributor in fear of culpability could resort to simple denial. I am
unable to see why someone shouldn't be able to receive a signed PDF via
email and achieve a similar level of confidence that the signor was
legitimate.

Regards,
Cliff

Ivan Sagalaev

unread,
Dec 4, 2009, 5:38:36 PM12/4/09
to clo...@googlegroups.com
Cliff Wells wrote:
> I am
> unable to see why someone shouldn't be able to receive a signed PDF via
> email and achieve a similar level of confidence that the signor was
> legitimate.

BTW Canonical does exactly that[1]. I've just recently signed their CCA
which consisted of downloading a PDF from the site and sending it back
to them with the words like "I agree with this".

[1]: http://www.canonical.com/contributors

David Brown

unread,
Dec 5, 2009, 1:03:04 AM12/5/09
to clo...@googlegroups.com
On Sat, Dec 05, 2009 at 01:38:36AM +0300, Ivan Sagalaev wrote:
>Cliff Wells wrote:
>> I am
>> unable to see why someone shouldn't be able to receive a signed PDF via
>> email and achieve a similar level of confidence that the signor was
>> legitimate.
>
>BTW Canonical does exactly that[1]. I've just recently signed their CCA
>which consisted of downloading a PDF from the site and sending it back
>to them with the words like "I agree with this".

It's going to depend a lot on who is interpreting the laws and what
decisions they make. I've done contributor agreements via scanned
documents and via fax. I don't think I've yet actually mailed one
(although that may change if I contribute something to Clojure).

David

Konrad Hinsen

unread,
Dec 5, 2009, 7:14:01 AM12/5/09
to clo...@googlegroups.com
On 4 Dec 2009, at 23:23, Cliff Wells wrote:

> What isn't clear to me is exactly what a piece of paper provides
> that an
> electronic form doesn't (aside from inconvenience). I don't see any

A clear legal status all over the world. A signed statement on paper
is recognized everywhere. The legal status of electronic messages,
even if signed by some suitable technique, varies wildly from one
country to the other.

It would be nice if open software could somehow escape from the legal
hassle surrounding copyright law, but I don't think this is a
realistic idea.

Konrad.


Meikel Brandmeyer

unread,
Dec 5, 2009, 3:56:30 AM12/5/09
to clo...@googlegroups.com
Hi,

Am 04.12.2009 um 13:33 schrieb John Harrop:

>> Eg. in Germany
>
> I'm not in Germany.

No. You are not. And I am not in the US. So what? This should just be an example, that legal things are not easy. And local differences in laws should be taken into account. (Knowing them is too much, but simply saying "public domain" is not enough...)

> In particular, there should be a way to participate pseudonymously for those people that (unlike myself) value their privacy sufficiently not to want to even post here under their real names.

Since I like turning in circles: these are the rules. If people want to contribute they'll have to do that with their real names. This was Rich's decision. And that's it. They can post on the list whatever pseudonym they like, or in the #clojure channel. But official contributions have to be signed. Why is that a problem?

Sincerely
Meikel

John Harrop

unread,
Dec 5, 2009, 7:34:51 AM12/5/09
to clo...@googlegroups.com
On Fri, Dec 4, 2009 at 2:49 PM, Richard Newman <holy...@gmail.com> wrote:
> The problem is that it is an unreasonably high barrier to entry.
> There MUST be an electronic-only way (and it must not require a cell
> phone, CC#, &c.) if the full potential of this community is to be
> unleashed upon clojure-contrib. In particular, there should be a way
> to participate pseudonymously for those people that (unlike myself)
> value their privacy sufficiently not to want to even post here under
> their real names.

Raising the barrier to entry stops people pseudonymously contributing
code they don't own.

But it also stops people pseudonymously contributing code they do own.
 
Yes, this makes it more difficult for someone to just email a fix for
a small bug.

And therein lies the problem. Most open source projects have a very low barrier to simply emailing a patch for a minor issue, and they benefit tremendously by it. This one does not, at least for now.

However, I quite understand Rich's reasons for doing it.

I still don't. Plenty of open source projects apparently are willing to take the risks Rich is apparently afraid of. Why not this one?
 
I don't think it creates an unreasonably high barrier to entry

And I do.
 
I don't see why there MUST be an electronic-only way.

Because that's how people participate in open source projects!!

Requiring snail mail, phone calls, or similarly for anything creates an impedance mismatch.

Rich Hickey

unread,
Dec 5, 2009, 9:10:34 AM12/5/09
to Clojure


On Dec 5, 7:34 am, John Harrop <jharrop...@gmail.com> wrote:
> On Fri, Dec 4, 2009 at 2:49 PM, Richard Newman <holyg...@gmail.com> wrote:
> > > The problem is that it is an unreasonably high barrier to entry.
> > > There MUST be an electronic-only way (and it must not require a cell
> > > phone, CC#, &c.) if the full potential of this community is to be
> > > unleashed upon clojure-contrib. In particular, there should be a way
> > > to participate pseudonymously for those people that (unlike myself)
> > > value their privacy sufficiently not to want to even post here under
> > > their real names.
>
> > Raising the barrier to entry stops people pseudonymously contributing
> > code they don't own.
>
> But it also stops people pseudonymously contributing code they do own.
>

I don't want code from someone unwilling to put their real name on it.

> > Yes, this makes it more difficult for someone to just email a fix for
> > a small bug.
>
> And therein lies the problem. Most open source projects have a very low
> barrier to simply emailing a patch for a minor issue, and they benefit
> tremendously by it. This one does not, at least for now.
>
> However, I quite understand Rich's reasons for doing it.
>
>

Well, they have been described in detail here.

>
> I still don't. Plenty of open source projects apparently are willing to take
> the risks Rich is apparently afraid of. Why not this one?
>

The problems of open source projects that didn't do a good job of
provenance, and then wanted to switch or expand license options, are
well known.

> > I don't think it creates an unreasonably high barrier to entry
>
> And I do.
>

"I don't ... I do ... I don't ... I do"

This is a clear sign this discussion has run its course. Everyone
please give this a rest.

> > I don't see why there MUST be an electronic-only way.
>
> Because that's how people participate in open source projects!!
>
> Requiring snail mail, phone calls, or similarly for anything creates an
> impedance mismatch.

When you write your own language, you can make different choices, and
I'm sure you'll expect people to abide by them. And good luck getting
someone to try your pseudonymously written language.

Rich

MarkSwanson

unread,
Dec 5, 2009, 8:57:43 PM12/5/09
to Clojure
Just a thought: maybe this is a sign a clearer message should be
placed on the Clojure contributing page. In any case, it can't hurt to
be more clear can it?

FYI I've submitted a couple of small patches to this group and read
the contributors agreement and the SCA FAQ. After going through this
process I was still under the impression it was OK to just submit
patches into the public domain. Rich even 'applied' the tiny 'patch'.

F.E. Rich makes things perfectly clear:

I don't want code from someone unwilling to put their real name on
it.

and

The problems of open source projects that didn't do a good job of
provenance, and then wanted to switch or expand license options, are
well known.

I think 'someone' should seriously think about putting these on the
contributors page. There is no way to misinterpret these statements.
If these statements (or something similar) are on the contributors
page I suspect it's unlikely this issue will ever be revisited. I do
not think strong clear statements like this would dissuade anyone from
contributing.

Btw, I have been trying to track 'someone' down for several decades.
If 'anyone' knows who this specific person is please let me know. :)

Cheers.

Cliff Wells

unread,
Dec 7, 2009, 7:24:03 PM12/7/09
to clo...@googlegroups.com
What I'm saying is that a signed document, whether digital or on paper,
provides little in the way of forgery prevention. Therefore, without
witnesses (e.g. a notary) or a sworn admission of signing, a signature
is largely inconsequential. Since the Clojure CAS requires nothing of
the sort, it is no better than a digital signature, regardless of
jurisdiction. Clearly the decision is up to Rich (it's his project,
anyone who doesn't like it could easily start a clojure-community
project with relaxed requirements), but frankly the current requirements
are rather pointless since they provide no real protections.

Regards,
Cliff


Dmitry Kakurin

unread,
Dec 13, 2009, 8:29:12 AM12/13/09
to Clojure
Thanks for a great idea Andrew!
I was slightly annoyed by the lack of consistency in parameter
ordering of Clojure collections API (cons vs conj, get/assoc vs filter/
map/take).
IMHO the input collection should ALWAYS be the last param. Then simple
->> macro would be sufficient in most cases.
Out of curiosity (and because apparently your code is not open-
source :-) I've re-written it as:

(defmacro -$> [ & forms ] (reduce #(replace {'$ %1} %2) forms))

It does not support your non-seq entries in the pipeline, but I
personally don't want them.
I'm guessing they are to be able to write something like
(-$> 1 (* 3 $) inc)
instead of
(-$> 1 (* 3 $) (inc $))
or do you have a better use for it?

Thanks again,
- Dmitry

On Oct 29, 12:35 pm, "AndrewC." <mr.bl...@gmail.com> wrote:
> Here's a macro that generalizes the two 'threading' macros -> and ->>.
>
> It works by using the idea of 'nesting marker' (from Oz) to specify
> where the result of the previous form should be nested in the
> subsequent form.
>
> (defmacro -$>
>   "Threads the expr through the forms. Inserts x into
>   the first form at the position marked by the $ symbol.
>   If the second form is not a list then it behaves as ->.
>   If there are more forms, inserts the first form into the second
>   form at the position marked by the $ symbol, etc."
>   ([x form] (if (seq? form)
>               (let [split (split-with (partial (complement =) '$)
> form)]
>                 `(~@(first split) ~x ~@(rest (second split))))
>               (list form x)))
>   ([x form & more] `(-$> (-$> ~x ~form) ~@more)))
>
> If this is any use to anyone (esp. anyone who has a CA) then consider
> it public domain.
>
> Any improvements appreciated!
Reply all
Reply to author
Forward
0 new messages