Queue/Usage: Difference between revisions

added Factor
No edit summary
(added Factor)
Line 827:
 
Crashing is the normal expected behavior in Erlang: let it crash, a supervisor will take responsibility of restarting processes, or the caller will take care of it. Only program for the successful cases.
 
=={{header|Factor}}==
For this task, we'll use Factor's <code>deque</code> vocabulary (short for double-ended queue). The <code>deque</code> class is a mixin, one of whose instances is <code>dlist</code> (double-linked list). Hence, the deque protocol works with double-linked lists. When using a deque as a queue, the convention is to queue elements with <code>push-front</code> and deque them with <code>pop-back</code>.
<lang factor>USING: combinators deques dlists kernel prettyprint ;
IN: rosetta-code.queue-usage
 
DL{ } clone { ! make new queue
[ [ 1 ] dip push-front ] ! push 1
[ [ 2 ] dip push-front ] ! push 2
[ [ 3 ] dip push-front ] ! push 3
[ . ] ! DL{ 3 2 1 }
[ pop-back drop ] ! pop 1 (and discard)
[ pop-back drop ] ! pop 2 (and discard)
[ pop-back drop ] ! pop 3 (and discard)
[ deque-empty? . ] ! t
} cleave</lang>
Alternatively, batch operations can be used.
<lang factor>USING: combinators deques dlists kernel prettyprint ;
IN: rosetta-code.queue-usage
 
DL{ } clone {
[ [ { 1 2 3 } ] dip push-all-front ] ! push all from sequence
[ . ] ! DL{ 3 2 1 }
[ [ drop ] slurp-deque ] ! pop and discard all
[ deque-empty? . ] ! t
} cleave</lang>
 
=={{header|Fantom}}==
1,808

edits