Queue/Definition: Difference between revisions

→‎{{header|Julia}}: A new entry for Julia
(→‎{{header|Julia}}: A new entry for Julia)
Line 1,897:
| [($q1|pop|.[0]), ($q2|pop|.[0])]
# produces: [1, 2]</lang>
 
=={{header|Julia}}==
Julia provides a variety of queue-like methods for vectors, making the solution to this task rather straightforward. Define a <tt>Queue</tt> in terms of a one dimensional array, and provide its methods using the appropriate vector operations. To adhere to Julia naming conventions, the queue operations are named "push!", "pop!" and "isempty" rather than "push", "pop" and "empty".
<lang Julia>
type Queue{T}
a::Array{T,1}
end
 
Queue() = Queue(Any[])
Queue(a::DataType) = Queue(a[])
Queue(a) = Queue(typeof(a)[])
 
Base.isempty(q::Queue) = isempty(q.a)
 
function Base.pop!{T}(q::Queue{T})
!isempty(q) || error("queue must be non-empty")
pop!(q.a)
end
 
function Base.push!{T}(q::Queue{T}, x::T)
unshift!(q.a, x)
return q
end
 
function Base.push!{T}(q::Queue{Any}, x::T)
unshift!(q.a, x)
return q
end
</lang>
 
{{out}}
It is easiest to use the REPL to show a <tt>Queue</tt> in action.
 
<pre>
julia> q = Queue()
Queue{Any}({})
 
julia> isempty(q)
true
 
julia> push!(q, 1)
Queue{Any}({1})
 
julia> isempty(q)
false
 
julia> push!(q, "two")
Queue{Any}({"two",1})
 
julia> push!(q, 3.0)
Queue{Any}({3.0,"two",1})
 
julia> push!(q, false)
Queue{Any}({false,3.0,"two",1})
 
julia> pop!(q)
1
 
julia> pop!(q)
"two"
 
julia> pop!(q)
3.0
 
julia> pop!(q)
false
 
julia> pop!(q)
ERROR: queue must be non-empty
in pop! at none:2
</pre>
 
=={{header|LabVIEW}}==