Queue/Definition: Difference between revisions

Content added Content deleted
(jq)
Line 1,803: Line 1,803:
this.dequeue = this.pop;
this.dequeue = this.pop;
}</lang>
}</lang>
=={{header|jq}}==
Note that since jq is a purely functional language, the entity
representing a queue must be presented as an input to any function
that is to operate on it.
The definition of pop as given below is idiomatic in jq but implies
that popping an empty queue yields [null, []] rather than an error. An
alternative definition, pop_or_error, is also given to illustrate
how an error condition can be generated.
<lang jq># An empty queue:
def fifo: [];

def push(e): [e] + .;

def pop: [.[0], .[1:]];

def pop_or_error: if length == 0 then error("pop_or_error") else pop end;

def empty: length == 0;</lang>
'''Examples''':
<lang jq>fifo | pop # produces [null,[]]

fifo
| push(42) # enqueue
| push(43) # enqueue
| pop # dequeue
| .[0] # the value
# produces 43

fifo|push(1) as $q1
| fifo|push(2) as $q2
| [($q1|pop|.[0]), ($q2|pop|.[0])]
# produces: [1, 2]</lang>

=={{header|LabVIEW}}==
=={{header|LabVIEW}}==
{{VI solution|LabVIEW_Queue_Definition.png}}
{{VI solution|LabVIEW_Queue_Definition.png}}



=={{header|Lasso}}==
=={{header|Lasso}}==