Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Queue-Definition/Jq/queue-definition.jq
Normal file
35
Task/Queue-Definition/Jq/queue-definition.jq
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# Input: an object
|
||||
# Output: the updated object with .emit filled in from `update|emit`.
|
||||
# `emit` may produce a stream of values, which need not be strings.
|
||||
def observe(update; emit):
|
||||
def s(stream): reduce stream as $_ (null;
|
||||
if $_ == null then .
|
||||
elif . == null then "\($_)"
|
||||
else . + "\n\($_)"
|
||||
end);
|
||||
.emit as $x
|
||||
| update
|
||||
| .emit = s($x // null, emit);
|
||||
|
||||
|
||||
def fifo: {queue: []};
|
||||
|
||||
# Is the input an object that represents the empty queue?
|
||||
def isempty:
|
||||
type == "object"
|
||||
and (.queue | length == 0); # so .queue == null and .queue == [] are equivalent
|
||||
|
||||
def push(e): .queue += [e];
|
||||
|
||||
def pop: if isempty then empty else .item = .queue[0] | .queue |= .[1:] end;
|
||||
|
||||
def pop_or_error: if isempty then error("pop_or_error") else pop end;
|
||||
|
||||
# Examples
|
||||
# fifo | pop // "nothing" # produces the string "nothing"
|
||||
|
||||
fifo
|
||||
| observe(push(42); "length after pushing: \(.queue | length)" )
|
||||
| observe(push(43); "length after pushing: \(.queue | length)" )
|
||||
| pop # dequeue
|
||||
| .emit, .item
|
||||
Loading…
Add table
Add a link
Reference in a new issue