Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Julia, how do you create an array of unique, empty mutable objects?

Tags:

arrays

julia

The helpstring for fill, says:

help?> fill
search: fill fill! finally findall filter filter! filesize filemode FileSyntax FileSchema isfile CSVFile @__FILE__ CSVFileSyntax fieldtype fieldname

  fill(x, dims)

  Create an array filled with the value x. For example, fill(1.0, (5,5)) returns a 5×5 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.

Note that last paragraph:

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.

So I was wondering, how does one construct an Array of n unique objects?

E.g. say I want an array of 3 empty, separate dictionaries.


Related: Creating an Array of Arrays in Julia

like image 733
NHDaly Avatar asked Jul 08 '26 16:07

NHDaly


1 Answers

The best I can come up with is to use a comprehension:

julia> ds = [Dict() for _ in 1:3]
2-element Array{Dict{Any,Any},1}:
 Dict()
 Dict()
 Dict()

Is this the best approach? Thanks!

like image 144
NHDaly Avatar answered Jul 11 '26 10:07

NHDaly