Should shift(a) be shift!(a) ?

137 views
Skip to first unread message

Miguel Bazdresch

unread,
May 31, 2012, 11:28:52 AM5/31/12
to juli...@googlegroups.com
If I understand correctly, functions that modify their arguments are
identified by names ending in !.

If that is the case, shouldn't all deqeue standard library functions,
such as shift, push, and pop, end with ! ? What's different about
append!()?

-- mb

Stefan Karpinski

unread,
May 31, 2012, 12:23:57 PM5/31/12
to juli...@googlegroups.com
Mostly just tradition -- people expect push, pop, etc. to mutate their argument, so renaming them with ! seems unnecessary and possibly annoying. I'd be open to being more consistent about it though.

John Myles White

unread,
May 31, 2012, 12:35:39 PM5/31/12
to juli...@googlegroups.com, juli...@googlegroups.com
I really like this tradition. The verb sort describes an action performed on an existing object; if I wanted a sorted copy, I would call that function, sorted(), not sort().

 -- John

Miguel Bazdresch

unread,
May 31, 2012, 1:13:02 PM5/31/12
to juli...@googlegroups.com
I just wanted to make sure I was understanding things correctly.
Personally I'd go for consistency but I won't complain if you decide
to keep the traditional names.

-- mb

John Cowan

unread,
May 31, 2012, 1:57:19 PM5/31/12
to juli...@googlegroups.com
On Thu, May 31, 2012 at 12:23 PM, Stefan Karpinski <ste...@karpinski.org> wrote:
> Mostly just tradition -- people expect push, pop, etc. to mutate their
> argument, so renaming them with ! seems unnecessary and possibly annoying.
> I'd be open to being more consistent about it though.

I would prefer consistency, which means ease of learning -- but of
course, I'm a Schemer. More to the point, there could be both
functional, immutable stacks and mutable ones.

--
GMail doesn't have rotating .sigs, but you can see mine at
http://www.ccil.org/~cowan/signatures

fork

unread,
May 31, 2012, 2:30:37 PM5/31/12
to juli...@googlegroups.com
John Cowan <johnwcowan@...> writes:

>
> On Thu, May 31, 2012 at 12:23 PM, Stefan Karpinski <stefan@...> wrote:
> > Mostly just tradition -- people expect push, pop, etc. to mutate their
> > argument, so renaming them with ! seems unnecessary and possibly annoying.
> > I'd be open to being more consistent about it though.

I vote for consistency, especially when it only requires a single character of
extra typing.

Julia has the chance to clear out some bad traditions, and should take that
opportunity whenever possible. (And I don't think "sorted" should be use as a
non-mutating function name ever -- yuck.)

Stefan Karpinski

unread,
May 31, 2012, 2:56:17 PM5/31/12
to juli...@googlegroups.com
On Thu, May 31, 2012 at 1:57 PM, John Cowan <johnw...@gmail.com> wrote:
On Thu, May 31, 2012 at 12:23 PM, Stefan Karpinski <ste...@karpinski.org> wrote:
> Mostly just tradition -- people expect push, pop, etc. to mutate their
> argument, so renaming them with ! seems unnecessary and possibly annoying.
> I'd be open to being more consistent about it though.

I would prefer consistency, which means ease of learning -- but of
course, I'm a Schemer.  More to the point, there could be both
functional, immutable stacks and mutable ones.

That's an interesting point. We could definitely provide a library of purely functional versions of common data structures like Clojure does. In such cases, having push(a,x) be a non-mutating version of push!(a,x) would be a sensible, meaningful thing that one could use.

I think I'm fine with renaming push to push! and so on. In which case, we should either make push raise a helpful error or making it a non-mutating version of push!. Jeff, Viral — thoughts?

Patrick O'Leary

unread,
May 31, 2012, 2:56:59 PM5/31/12
to juli...@googlegroups.com
On Thursday, May 31, 2012 1:30:37 PM UTC-5, fw wrote:
Julia has the chance to clear out some bad traditions, and should take that
opportunity whenever possible. (And I don't think "sorted" should be use as a
non-mutating function name ever -- yuck.)

"sort" is imperative ("sort this!"), where as "sorted" can imply a view ("this is sorted"). This is also consistent with Python's use, where sort(x) sorts x, but sorted(x) is a generator which yields the sorted values of x without modifying x.

fork

unread,
May 31, 2012, 3:10:44 PM5/31/12
to juli...@googlegroups.com
Patrick O'Leary <patrick.oleary@...> writes:


> "sort" is imperative ("sort this!"), where as "sorted" can imply a view ("this
is sorted"). This is also consistent with Python's use, where sort(x) sorts x,
but sorted(x) is a generator which yields the sorted values of x without
modifying x.

I understand what you are saying, but I think the "!" suffix makes it nicely
explicit, and I don't think the "ed" signifies "non-mutable generator" (at least
not for me). Also, while Python is awesome, being consistent with it doesn't
seem that important to me (I think they also have more limitations with
characters they can use); far more important to maintain internal consistency
and discoverability.

But its all good. I am just the peanut gallery at this point.


Stefan Karpinski

unread,
May 31, 2012, 3:11:31 PM5/31/12
to juli...@googlegroups.com
Our convention for this is that the capitalized version produces an iterable object that provides whatever transformation lazily. So Sort(x) would produce a Sort object which, when iterated, yields the elements of x in sorted order. So the capital versions (i.e. iterable types) are lazy; the lowercase versions like sort tend to default to just creating an iterable object, but will generally construct a sorted object immediately.

This convention just fell out of the way these things have to be implemented anyway: zip needs to have an iterable type that holds the things that are being iterated in zipped fashion. That type is called Zip.

Stefan Karpinski

unread,
May 31, 2012, 3:12:35 PM5/31/12
to juli...@googlegroups.com
Being consistent with Matlab is more important to us here since most Matlab code ports pretty trivially. In this case, I don't think that Matlab has push or pop, so that's not really a concern.

John Cowan

unread,
May 31, 2012, 3:12:47 PM5/31/12
to juli...@googlegroups.com
On Thu, May 31, 2012 at 2:56 PM, Patrick O'Leary
<patrick...@gmail.com> wrote:

> "sort" is imperative ("sort this!"), where as "sorted" can imply a view
> ("this is sorted").

I agree: I'd go with "sort!" and "sorted".

Stefan Karpinski

unread,
May 31, 2012, 3:22:09 PM5/31/12
to juli...@googlegroups.com
While sorted is kind of nice linguistically, I think that expecting people to inflect words is dangerous. Not everyone is a native English speaker and some of these things are pretty irregular. If we're going to go this route, I'd prefer to stick with `foo` and `foo!` for the non-mutating and mutating versions of things consistently.

Miguel Bazdresch

unread,
May 31, 2012, 3:30:23 PM5/31/12
to juli...@googlegroups.com
If you should decide to go this route, I'd also prefer to have foo() and foo!().

-- mb

Stefan Karpinski

unread,
May 31, 2012, 4:03:59 PM5/31/12
to juli...@googlegroups.com
Created an issue to track this decision: https://github.com/JuliaLang/julia/issues/907.

Patrick O'Leary

unread,
May 31, 2012, 5:11:20 PM5/31/12
to juli...@googlegroups.com
This is also perfectly fine, just thought I'd offer some justification for "sorted". I'd point out that Guido isn't a native English speaker, though he's likely to have learned it pretty young. And of course being consistent with Python isn't a goal; I brought it up to establish that that convention is already in use. I have no objections to doing something else, and "!" isn't a bad choice in that regard.

david tweed

unread,
Jun 1, 2012, 7:49:38 AM6/1/12
to juli...@googlegroups.com
On Thursday, May 31, 2012 8:22:09 PM UTC+1, Stefan Karpinski wrote:
While sorted is kind of nice linguistically, I think that expecting people to inflect words is dangerous. Not everyone is a native English speaker and some of these things are pretty irregular. If we're going to go this route, I'd prefer to stick with `foo` and `foo!` for the non-mutating and mutating versions of things consistently.

In addition human consistency, this is one of those few cases where consistency could be helpful for machines as well: if I want to look at how a particular "conceptual function, ignoring it's mutability status", say sorting, is being used in a codebase it's easier for me (or an IDE) to form a regexp like "\Wsort(\!?)\(" than to have to deal with an irregular linguisitic pair of names. (Yes, I sometimes grep through code trying to find things/effects this way...)

Stefan Karpinski

unread,
Jun 1, 2012, 11:38:05 AM6/1/12
to juli...@googlegroups.com
On Fri, Jun 1, 2012 at 7:49 AM, david tweed <david...@gmail.com> wrote:
On Thursday, May 31, 2012 8:22:09 PM UTC+1, Stefan Karpinski wrote:
While sorted is kind of nice linguistically, I think that expecting people to inflect words is dangerous. Not everyone is a native English speaker and some of these things are pretty irregular. If we're going to go this route, I'd prefer to stick with `foo` and `foo!` for the non-mutating and mutating versions of things consistently.

In addition human consistency, this is one of those few cases where consistency could be helpful for machines as well: if I want to look at how a particular "conceptual function, ignoring it's mutability status", say sorting, is being used in a codebase it's easier for me (or an IDE) to form a regexp like "\Wsort(\!?)\(" than to have to deal with an irregular linguisitic pair of names. (Yes, I sometimes grep through code trying to find things/effects this way...)

Totally agreed.

Viral Shah

unread,
Jun 2, 2012, 12:44:48 AM6/2/12
to juli...@googlegroups.com

I prefer consistency in naming and behaviour as well. I was originally not a fan of the ! suffixes, but have come to like it. Of course, this is a programming style, and the message is only well enforced if used consistently.

-viral
 


 
Reply all
Reply to author
Forward
0 new messages