Singly-linked list/Traversal: Difference between revisions
→{{header|jq}}: empty => remove
(/* {{header|jq}} def items:) |
(→{{header|jq}}: empty => remove) |
||
Line 991:
For context see [[Singly-linked_list/Element_definition#jq]].
Here we define a "map" filter as well as a traversal filter. The "map" filter is similar to the built-in `map` in that it can be used to remove items as per the comment below.
<lang jq>
# Produce a stream of the items in the input SLL.
Line 997:
while(.; .next) | .item;
# If f evaluates to empty at any item, that item is removed;
# if f evaluates to more than one item, only the first is retained.
def map_singly_linked_list(f):
def m:
if
then [.item | f ] as $i
m;</lang>▼
| if $i == [] then .next | m # i.e., remote it and continue
else .item = $i[0] # only pick the first
| (.next |= m)
end
else .
end;
m;
'''Examples'''
<lang jq>{
|