Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: Append an element to an array of custom types

Tags:

arrays

julia

Appending an element to an array in Julia works like this:

v = Array{Int32, 1}(0)
append!(v, 1)
append!(v, 2)
println(v)  # prints: Int32[1,2]

When I try this with a custom type

type Node
    label::String
    value::Int32
end
nodes = Array{Node, 1}(0)
append!(nodes, Node("a", 42))

I get the following error:

ERROR: LoadError: MethodError: no method matching length(::Node)

I assume I have to 'implement' the length method but do not know how.

like image 986
Michael Avatar asked Nov 17 '16 10:11

Michael


People also ask

How do I add elements to an array in Julia?

Julia allows adding new elements in an array with the use of push! command. Elements in an array can also be added at a specific index by passing the range of index values in the splice! function.

How do you fill an array in Julia?

Create an array filled with the value x . For example, fill(1.0, (10,10)) returns a 10x10 array of floats, with each element initialized to 1.0 . If x is an object reference, all elements will refer to the same object. fill(Foo(), dims) will return an array filled with the result of evaluating Foo() once.

How do you create a vector in Julia?

Creating a Vector A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])

How do you create a matrix in Julia?

Julia provides a very simple notation to create matrices. A matrix can be created using the following notation: A = [1 2 3; 4 5 6]. Spaces separate entries in a row and semicolons separate rows. We can also get the size of a matrix using size(A).

What is an an array in Julia?

An Array is an ordered set of elements which are often specified with squared brackets having comma-separated items. We can create arrays that are − In Julia, arrays are actually mutable type collections which are used for lists, vectors, tables, and matrices.

What is the difference between 1D and 2D arrays in Julia?

It represents a type of list that can be accessed by subsequent memory locations. A 1D array in Julia is known as a Vector. Unlike other types of arrays, Vectors allow to add elements from front or back resulting in resizing the array. A 2D array or 2 dimensional array is a representation of items in the form of rows and columns.

How do I get the first “n” items in a Julia array?

To get the first “n” items in a Julia array, we add “n” as an optional second argument to first (). If we want the first three planets, for example, we’d input: The ability to add an integer as a second argument to the first () function—to specify how many leading items we want from the array—was added in Julia 1.6.4.

How to delete an element from a 1D array in Julia?

Julia allows to delete an element from a 1D array by using predefined function pop!. This function will simply remove the last element of the array and reduce the array size by 1. To delete first element of the array, one can use popfirst! function.


2 Answers

The append! command doesn't do what you think it does. You're thinking of the push! command.

The append! command appends two arrays together. Both arguments need to be arrays:

julia> append!(nodes, [Node("a", 42)])
1-element Array{Node,1}:
 Node("a",42)

No length implementing necessary
(that error was just telling you it tried to read the length of your array for the second argument and found something that was not an array.)

like image 114
Tasos Papastylianou Avatar answered Nov 02 '22 12:11

Tasos Papastylianou


Try this

Base.append!(x::Array{Node,1}, val::Node) = push!(x, val)

then you get

append!(nodes, Node("a", 42))
1-element Array{Node,1}:
 Node("a",42)

you've got to explicitly create a function for this particular type as append! or any of the Base functions sometimes (or perhaps always I havent checked) wont accept Any

like image 43
isebarn Avatar answered Nov 02 '22 12:11

isebarn