Skip to content
Myr

walk/1

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by pkoppstein · committed by David Tolnay · parent c426d49 · Browse files · 2 files+38 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−0 removed

  • other:+38 added−0 removed

2 files+38 added−0 removed

modified:docs/content/3.manual/manual.yml+27 added
1520 unmodified lines
1521 - 21522 - 41523 - 1615241525 - title: "`walk(f)`"1526 body: |15271528 The `walk(f)` function applies f recursively to every1529 component of the input entity. When an array is1530 encountered, f is first applied to its elements and then to1531 the array itself; when an object is encountered, f is first1532 applied to all the values and then to the object. In1533 practice, f will usually test the type of its input, as1534 illustrated in the following examples. The first example1535 highlights the usefulness of processing the elements of an1536 array of arrays before processing the array itself. The second1537 example shows how all the keys of all the objects within the1538 input can be considered for alteration.15391540 examples:1541 - program: 'walk(if type == "array" then sort else . end)'1542 input: '[[4, 1, 7], [8, 5, 2], [3, 6, 9]]'1543 output:1544 - '[[1,4,7],[2,5,8],[3,6,9]]'15451546 - program: 'walk( if type == "object" then with_entries( .key |= sub( "^_+"; "") ) else . end )1547 input: '[ { "_a": { "__b": 2 } } ]'1548 output:1549 - '[{"a":{"b":2}}]'155015511552 - title: "`..`"1553 body: |15541555 Short-hand for `recurse` without arguments. This is intended
1350 unmodified lines
modified:builtin.c+11 added
1638 unmodified lines
1639 " end"1640 " else .[2]"1641 " end"1642 " end;",16431644 // # Apply f to composite entities recursively, and to atoms1645 "def walk(f):"1646 " . as $in"1647 " | if type == \"object\" then"1648 " reduce keys[] as $key"1649 " ( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f"1650 " elif type == \"array\" then map( walk(f) ) | f"1651 " else f"1652 " end;",1653};16541655#undef LIBM_DDD_NO1656#undef LIBM_DD_NO1657#undef LIBM_DDD1658#undef LIBM_DD
43 unmodified lines