Jump to content

Singly-linked list/Traversal: Difference between revisions

→‎{{header|jq}}: make the map function behave like jq's built-in map
(→‎{{header|jq}}: empty => remove)
(→‎{{header|jq}}: make the map function behave like jq's built-in map)
Line 996:
def items:
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 more than one item, onlyall theare firstadded is retainedseparately.
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'''
<lang jq>{
2,472

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.