Singly-linked list/Traversal: Difference between revisions
Content added Content deleted
Line 1,000: | Line 1,000: | ||
'''test_list | traverse''' and '''test_list | traverse2''' produce identical results: a stream of the "car" values. |
'''test_list | traverse''' and '''test_list | traverse2''' produce identical results: a stream of the "car" values. |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Works with gojq, the Go implementation of jq''' |
|||
For context see [[Singly-linked_list/Element_definition#jq]]. |
|||
Here we define a "map" filter as well as two two traversal filters. |
|||
<lang jq> |
|||
# Traverse the input SLL, applying the filter f to each item. |
|||
def traverse(f): |
|||
def t: |
|||
select(has("item")) | (.item|f), (.next|t); |
|||
t; |
|||
# Produce a stream of the items in the input SLL. |
|||
def traverse: traverse(.); |
|||
def map_singly_linked_list(f): |
|||
def m: |
|||
if has("item") then (.item |= f) | (.next |= m) |
|||
else . end; |
|||
m; |
|||
</lang> |
|||
'''Examples''' |
|||
<lang jq>{ |
|||
"item": 1, |
|||
"next": { |
|||
"item": 2, |
|||
"next": null |
|||
} |
|||
} |
|||
| reduce traverse as $item (null; .+$item), |
|||
traverse(- .), |
|||
map_singly_linked_list(- .) |
|||
</lang>{{out}} |
|||
<pre> |
|||
3 |
|||
-1 |
|||
-2 |
|||
{ |
|||
"item": -1, |
|||
"next": { |
|||
"item": -2, |
|||
"next": null |
|||
} |
|||
} |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |