Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Queue-Definition/Clojure/queue-definition.clj
Normal file
30
Task/Queue-Definition/Clojure/queue-definition.clj
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
user=> (def empty-queue clojure.lang.PersistentQueue/EMPTY)
|
||||
#'user/empty-queue
|
||||
user=> (def aqueue (atom empty-queue))
|
||||
#'user/aqueue
|
||||
; Check if queue is empty
|
||||
user=> (empty? @aqueue)
|
||||
true
|
||||
; As with other Clojure data structures, you can add items using conj and into
|
||||
user=> (swap! aqueue conj 1)
|
||||
user=> (swap! aqueue into [2 3 4])
|
||||
user=> (pprint @aqueue)
|
||||
<-(1 2 3 4)-<
|
||||
; You can read the head of the queue with peek
|
||||
user=> (peek @aqueue)
|
||||
1
|
||||
; You can remove the head producing a new queue using pop
|
||||
user=> (pprint (pop @aqueue))
|
||||
<-(2 3 4)-<
|
||||
; pop returns a new queue, the original is still intact
|
||||
user=> (pprint @aqueue)
|
||||
<-(1 2 3 4)-<
|
||||
; you can treat a queue as a sequence
|
||||
user=> (into [] @aqueue)
|
||||
[1 2 3 4]
|
||||
; but remember that using rest or next converts the queue to a seq. Compare:
|
||||
user=> (-> @aqueue rest (conj 5) pprint)
|
||||
(5 2 3 4)
|
||||
; with:
|
||||
user=> (-> @aqueue pop (conj 5) pprint)
|
||||
<-(2 3 4 5)-<
|
||||
Loading…
Add table
Add a link
Reference in a new issue