Singly-linked list/Traversal: Difference between revisions

Content added Content deleted
(→‎{{header|jq}}: empty => remove)
(→‎{{header|jq}}: make the map function behave like jq's built-in map)
Line 996: Line 996:
def items:
def items:
while(.; .next) | .item;
while(.; .next) | .item;

def to_singly_linked_list(s):
reduce ([s]|reverse[]) as $item (null; {$item, next:.});


# If f evaluates to empty at any item, that item is removed;
# 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.
# if f evaluates to more than one item, all are added separately.
def map_singly_linked_list(f):
def map_singly_linked_list(f): to_singly_linked_list( items | f );</lang>
def m:
if . == null then null
elif has("item")
then [.item | f ] as $i
| 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;
</lang>
'''Examples'''
'''Examples'''
<lang jq>{
<lang jq>{