Some common functions for arrays

If b = [1:7] and c = [100,200,300], then you can concatenate b and c with the following command:

append!(b, c) #> Now b is [1, 2, 3, 4, 5, 6, 7, 100, 200, 300] 

The array, b, is changed by applying this append! method—that's why it ends in an exclamation mark (!). This is a general convention.

A function whose name ends in a ! changes its first argument.

Likewise, push! and pop! append one element at the end, or take one away and return that, while the array is changed:

pop!(b) #> 300, b is now [1, 2, 3, 4, 5, 6, 7, 100, 200] 
push!(b, 42) # b is now [1, 2, 3, 4, 5, 6, 7, 100, 200, 42]  

If you want to do the same operations on the front of the array, use popfirst! and pushfirst! (formerly unshift! and shift! ...

Get Julia 1.0 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.