Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Argument order rules of thumb
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stuart Sierra  
View profile  
 More options Apr 14 2008, 10:40 pm
From: Stuart Sierra <the.stuart.sie...@gmail.com>
Date: Mon, 14 Apr 2008 19:40:46 -0700 (PDT)
Local: Mon, Apr 14 2008 10:40 pm
Subject: Argument order rules of thumb
Hello everyone.  I thought I'd take advantage of a quiet couple of
days to ... stir up some trouble.  This is just a thought I had.  Take
it or leave it, flame it or flog it, whatever you like.

I have trouble remembering the order of arguments in the Clojure built-
in functions.  Things like cons/conj, assoc/find, etc.  So I thought
about it a bit and came up with some "rules of thumb" that seem
natural:

1. Variable arg lists have to go last;
2. The "operand," i.e. the object primarily being "operated upon,"
goes last, to make partial application easier and nested expressions
easier to read;
3. If a function/macro takes a collection, the collection goes last.

Skimming through the API, it seems like most of the built-in functions
fit these rules, with a few exceptions:
conj contains? find get nth nthrest select-keys subs subvec with-meta
set/project

Maybe it's already too late to suggest breaking changes in the core
language, or maybe Clojure, unlike its data structures, is still
somewhat mutable. :)  Whatever, it's just an idea.

-Stuart


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rich Hickey  
View profile  
 More options Apr 15 2008, 7:43 am
From: Rich Hickey <richhic...@gmail.com>
Date: Tue, 15 Apr 2008 04:43:38 -0700 (PDT)
Local: Tues, Apr 15 2008 7:43 am
Subject: Re: Argument order rules of thumb

On Apr 14, 10:40 pm, Stuart Sierra <the.stuart.sie...@gmail.com>
wrote:

Hmmm... Good thing those weren't the rules or assoc/dissoc couldn't
have become variadic. I know it might seem arbitrary, but it's
(usually :) not.

A better characterization (but not a rule, no promises) might be that
the sequence functions take the seq last, and the collection functions
take the collection first.

I think with #() the partial application story is always easy - no
matter what the argument order you often want to bind args other than
the first.

Rich


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rich Hickey  
View profile  
 More options Apr 17 2008, 10:07 am
From: Rich Hickey <richhic...@gmail.com>
Date: Thu, 17 Apr 2008 07:07:21 -0700 (PDT)
Local: Thurs, Apr 17 2008 10:07 am
Subject: Re: Argument order rules of thumb

On Apr 15, 7:43 am, Rich Hickey <richhic...@gmail.com> wrote:

I seem to have quashed the dialog with my contribution :(

Maybe this explanation will be more constructive.

One way to think about sequences is that they are read from the left,
and fed from the right:

<- [1 2 3 4]

Most of the sequence functions consume and produce sequences. So one
way to visualize that is as a chain:

map<- filter<-[1 2 3 4]

and one way to think about many of the seq functions is that they are
parameterized in some way:

(map f)<-(filter pred)<-[1 2 3 4]

So, sequence functions take their source(s) last, and any other
parameters before them, and partial allows for direct parameterization
as above. There is a tradition of this in functional languages and
Lisps.

Note that this is not the same as taking the primary operand last.
Some sequence functions have more than one source (concat,
interleave). When sequence functions are variadic, it is usually in
their sources.

I don't think variable arg lists should be a criteria for where the
primary operand goes. Yes, they must come last, but as the evolution
of assoc/dissoc shows, sometimes variable args are added later.

Ditto partial. Every library eventually ends up with a more order-
independent partial binding method. For Clojure, it's #().

What then is the general rule?

Primary collection operands come first.That way one can write -> and
its ilk, and their position is independent of whether or not they have
variable arity parameters. There is a tradition of this in OO
languages and CL (CL's slot-value, aref, elt - in fact the one that
trips me up most often in CL is gethash, which is inconsistent with
those).

So, in the end there are 2 rules, but it's not a free-for-all.
Sequence functions take their sources last and collection functions
take their primary operand (collection) first. Not that there aren't
are a few kinks here and there that I need to iron out (e.g. set/
select).

I hope that helps make it seem less spurious,

Rich


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart Sierra  
View profile  
 More options Apr 18 2008, 2:53 pm
From: Stuart Sierra <the.stuart.sie...@gmail.com>
Date: Fri, 18 Apr 2008 11:53:23 -0700 (PDT)
Local: Fri, Apr 18 2008 2:53 pm
Subject: Re: Argument order rules of thumb
On Apr 17, 10:07 am, Rich Hickey <richhic...@gmail.com> wrote:

> I seem to have quashed the dialog with my contribution :(

Not at all.  My original post was in the spirit of "throw stuff
against the wall and see what sticks."  It didn't stick with anyone,
and that's cool with me.

> So, in the end there are 2 rules, but it's not a free-for-all.
> Sequence functions take their sources last and collection functions
> take their primary operand (collection) first. Not that there aren't
> are a few kinks here and there that I need to iron out (e.g. set/
> select).

Thank you for this, it helps to know the reasoning behind things.  The
question was prompted by a function I wrote in seq-utils.clj in
clojure-contrib:

(defn includes? [value coll]
  (if (some (fn [x] (= x value)) coll)
    true false))

I wanted to have 'value' first, but I was afraid that was inconsistent
with (contains? map key).  But it might be ok if the convention is
sequences-last-collections-first.

Thanks,
-Stuart


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chouser  
View profile  
 More options Apr 18 2008, 3:06 pm
From: Chouser <chou...@gmail.com>
Date: Fri, 18 Apr 2008 15:06:52 -0400
Local: Fri, Apr 18 2008 3:06 pm
Subject: Re: Argument order rules of thumb
On Fri, Apr 18, 2008 at 2:53 PM, Stuart Sierra

<the.stuart.sie...@gmail.com> wrote:
>  Thank you for this, it helps to know the reasoning behind things.  The
>  question was prompted by a function I wrote in seq-utils.clj in
>  clojure-contrib:

>  (defn includes? [value coll]
>   (if (some (fn [x] (= x value)) coll)
>     true false))

I know you're talking about more general issues, but since you posted
this particular example, you might be interested in this conversation:

http://n01se.net/chouser/clojure-log/2008-04-11.html

Which includes this snippet from Rich: (some #{1} [1 2 3])

That returns true because an item in the set (1) is in the collection [1 2 3].

--Chouser


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart Sierra  
View profile  
 More options Apr 20 2008, 12:41 am
From: Stuart Sierra <the.stuart.sie...@gmail.com>
Date: Sat, 19 Apr 2008 21:41:49 -0700 (PDT)
Local: Sun, Apr 20 2008 12:41 am
Subject: Re: Argument order rules of thumb
On Apr 18, 3:06 pm, Chouser <chou...@gmail.com> wrote:

> I know you're talking about more general issues, but since you posted
> this particular example, you might be interested in this conversation:

> http://n01se.net/chouser/clojure-log/2008-04-11.html

> Which includes this snippet from Rich: (some #{1} [1 2 3])

> That returns true because an item in the set (1) is in the collection [1 2 3].

So sets are callable?!  That's trippy, man.  But it makes sense with
the callable maps & vectors.  I love how Clojure is more "functional"
that most "functional languages."  Thanks, Chouser.

-Stuart


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »