I thought that collect(1:5) returned [1,2,3,4,5], which is different.
1:5 is an iterator, so I do not see how == is possible. If you are, correct, I indeed
Have a very serious misunderstanding.
Gordon
I thought that collect(1:5) returned [1,2,3,4,5], which is different.
1:5 is an iterator, so I do not see how == is possible. If you are, correct, I indeed
Have a very serious misunderstanding.
Gordon
1:5
is just a clever way of representing [1, 2, 3, 4, 5]
without wasting memory. The point is that their behavior should be exactly the same.
This is the purpose of the AbstractVector type family.
This post might help clarify things: What is the difference between "==" and "===" comparison operators in Julia? - Stack Overflow. It’s a bit out of date (I don’t think ===
is still an alias for is
) but helps explain what ==
means.
I do not want to sound rude but this is something you can check in half a minute in the REPL, there is no reason to write a comment questioning it and create more noise instead of checking it first.
julia> 1:5 == [1, 2, 3, 4, 5]
true
julia> typeof(1:5)
UnitRange{Int64}
julia> typeof([1, 2, 3, 4, 5])
Array{Int64,1}
julia> 1 == 1.0
true
Objects of different types can be considered equal. And an iterator/range is semantically a collection, even if it is represented by a fixed size object.
I understand your point. However, notemthat you cannot domthe following g:
a= 1:5
b = a[2]
However,
a= [1,2,3,4,5]
b= a[2]
Is allowed. Therefore, their behavior is not the same. Would you suggest that they should be the same?
I would submitmthat 1:5 has the behavior of an iterator, which defines the equivalent of the next operator. Collect acts on 1:5 to return a vector. I do not know whether collect acts on more general iterators, go en that in principles, iterators can have infinite length.
Thanks, and I apologize. I do not have a computer at my disposal. I am working from an iPad. I’m will drop it.
I do not want to sound rude but this is something you can check in half a minute in the REPL, there is no reason to write a comment questioning it and create more noise instead of checking it first.
So I domhave a question: why not allow (1:5)[5] as an operation?
…
julia> (6:10)[3]
8
It is allowed.
Yes, you can. This works perfectly well. The thing you cannot do is mutate a
, it’s an Immutable array. Ranges are vectors. Just try
(1:5) isa AbstractVector
Maybe this can help you: Julia Online Compiler & Interpreter - Replit
The solution:
reshape(b,1,: )
permutedims
is probably easier