# Empty a queue

**URL:** https://discourse.julialang.org/t/empty-a-queue/36991
**Category:** General Usage
**Created:** [April 3, 2020, 10:19pm UTC](https://discourse.julialang.org/t/empty-a-queue/36991 "2020-04-03T22:19:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [April 3, 2020, 10:19pm UTC](https://discourse.julialang.org/t/empty-a-queue/36991/1 "2020-04-03T22:19:31Z")

</div>

Apparently, there is no way to empty! a queue.  
Doc for DataStructures.jl shows an empty! method for a stack, but not for a queue.

It is quite difficult to tell what sort of beast a queue is since it wraps dequeue.

When I display my queue in the REPL, the beginning looks like this:

```julia
Queue{NamedTuple}(Deque

```

What follows is an array (well, a vector) of the NamedTuples that have been enqueued.

The appears to get no way to get at that array.

What to do to empty it?

To move on, I switched to a vector as I don’t care about all the fancy semantics. It works.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 3, 2020, 10:31pm UTC](https://discourse.julialang.org/t/empty-a-queue/36991/2 "2020-04-03T22:31:47Z")

</div>

> [@lewis](#):
>
> The appears to get no way to get at that array.

You can iterate over it, so `collect(queue)` works to get a copy of the queue as an array.

Normally, you don’t want to poke around in the internals of some other package’s data structure.

> [@lewis](#):
>
> What to do to empty it?

`empty!(queue)` works.

> [@lewis](#):
>
> To move on, I switched to a vector as I don’t care about all the fancy semantics.

If you don’t need queue semantics, you definitely shouldn’t use a queue data structure.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [April 3, 2020, 10:37pm UTC](https://discourse.julialang.org/t/empty-a-queue/36991/3 "2020-04-03T22:37:56Z")

</div>

`empty!` is definitively defined for a Queue object:

> <https://github.com/JuliaCollections/DataStructures.jl/blob/774a2a86ede83d4a727ca8fb50404f04149595b3/src/queue.jl#L39>

---

<div class="post-metadata">

### Author: ![lewis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis/32/5217_2.png) [@lewis](https://discourse.julialang.org/u/lewis)
#### Post date: [April 3, 2020, 11:27pm UTC](https://discourse.julialang.org/t/empty-a-queue/36991/4 "2020-04-03T23:27:19Z")

</div>

I had a function for empty!(df::DataFrame). This blocked the use of empty! from DataStructures. Should I have done import Base.empty! ?

Just asking. The array is adequate to the purpose.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [April 4, 2020, 12:21am UTC](https://discourse.julialang.org/t/empty-a-queue/36991/5 "2020-04-04T00:21:59Z")

</div>

> [@lewis](#):
>
> Should I have done import Base.empty! ?

I believe so. If you create a custom empty! for your data type then I believe can can do `function Base.empty!()` to ensure you don’t hide the others.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 4, 2020, 1:39am UTC](https://discourse.julialang.org/t/empty-a-queue/36991/6 "2020-04-04T01:39:55Z")

</div>

> [@lewis](#):
>
> I had a function for empty!(df::DataFrame). This blocked the use of empty! from DataStructures. Should I have done import Base.empty! ?

No, because defining your own `Base.empty!(df::DataFrame)` would be [type piracy](https://docs.julialang.org/en/v1/manual/style-guide/index.html#Avoid-type-piracy-1), and could easily conflict e.g. if the `DataFrames` package decides to implement `Base.empty!`.

Either call your function something else, or call `Base.empty!(queue)` when you want to use the standard `empty!` function.
