Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why were these common operations on collections renamed in Julia?

Tags:

julia

Many common operations on collections in Julia such as deleting an item from a Set were renamed recently, with the old functions deprecated.

For example,

del(IntSet(1,2,3), 1)

now pops up a warning

WARNING: del is deprecated, use delete! instead.

Some of the renamed functions:

@deprecate  push        push!
@deprecate  pop         pop!
@deprecate  grow        grow!
@deprecate  enqueue     unshift!
@deprecate  unshift     unshift!
@deprecate  shift       shift!
@deprecate  insert      insert!
@deprecate  del         delete!
@deprecate  del_all     empty!

Why were these renamed? Is appending a ! to functions that change the state of a collection now a convention?

like image 479
Ben Hamner Avatar asked Jan 17 '13 03:01

Ben Hamner


2 Answers

You can read the julia-dev thread here. Basically, it's simply changing to respect the rule described in the arrays documentation:

The last function, fill!, is different in that it modifies an existing array instead of constructing a new one. As a convention, functions with this property have names ending with an exclamation point. These functions are sometimes called "mutating" functions, or "in-place" functions.

FWIW I think this is a good idea, at least for Base.

like image 114
DSM Avatar answered Oct 31 '22 22:10

DSM


The use of ! was always an explicit convention to indicate mutation, it was just not properly enforced until recently. For push, one could easily argue that it's not essential. But using push! instead of push makes it clear that mutation is happening in this case in precisely the same way that mutation would happen when using sort!, which is very different from sort.

This exclamation-mark convention exists in Scheme and Ruby and probably several other languages. It doesn't exist in a language like R which wouldn't allow one to perform the mutation without recourse to the underlying guts of the language.

like image 32
John Myles White Avatar answered Oct 31 '22 22:10

John Myles White