Reputation:
This seems like a really easy thing, but believe it or not my google search hasn't brought me anywhere. I want to be able to convert a set to a vector. I have tried the following:
sett=Set{Int64}([1,2,3])
vecc=convert(Vector{Int64},sett)
but it appears Julia doesn't know how to do these conversions.
One obvious thing I can do is using comprehensions e.g.
vecc=[el for el in sett]
but is there not a more elegant way?
Cheers in advance.
Upvotes: 3
Views: 395
Reputation: 16045
You can use the splat operator :
vecc = [sett...]
Though, while this is (to me) more elegant than using list comprehension, one should avoid splatting for performance concerns, even with relatively small collections (as it requires at least one allocation per element!).
So if concerned about performance, you should definitely use collect()
or stick with list comprehension :
#items | collect(set) |
[i for i in set] |
[set...] |
---|---|---|---|
10^1 | 64.934 ns (1 alloc.) | 66.734 ns (1 alloc.) | 856.716 ns (12 alloc.) |
10^3 | 2.511 μs (1 alloc.) | 2.256 μs (1 alloc.) | 83.900 μs (2233 alloc.) |
10^6 | 9.627 ms (2 alloc.) | 10.181 ms (2 alloc.) | 102.752 ms (2999281 alloc.) |
Upvotes: 1
Reputation: 12654
Splatting can be problematic from a performance perspective. Instead, use
vecc = collect(sett)
Upvotes: 2