walk

walk traverses a data structure down to a given level of depth and applies a given function to all leaf nodes. With a depth argument supplied, WALK will travel to the level of depth supplied and execute the function argument. Without the depth argument, the function argument will be applied to all leaf nodes.

Syntax

Arity: 2

walk(arg1, arg2)

Arguments

Description

arg1

Function to apply to all leaf nodes within arg2.

arg2

A collection.

Arity: 3

walk(arg1, arg2, arg3)

Arguments

Description

arg1

Function to apply to all leaf nodes within arg3.

arg2

Integer representing the maximum level of depth.

arg3

A collection.

Examples

Example 1: Transform to uppercase some values in a data structure

Given the following data structure:

{
"users": {
"user_one": {
"first_name": "Napoleon",
"last_name": "Bonaparte",
"meta": {
"server": "oceania",
"activity": "high"
}
},
"user_two": {
"first_name": "Don",
"last_name": "Quixote",
"meta": {
"server": "europe",
"activity": "low"
}
}
}
}

Let's say we wanted to uppercase the names of some users from the data structure above, we can use walk:

walk(UPPER, 2, users)

This returns the following:

{
"users": {
"user_one": {
"first_name": "NAPOLEON",
"last_name": "BONAPARTE",
"meta": {
"server": "oceania",
"activity": "high"
}
},
"user_two": {
"first_name": "DON",
"last_name": "QUIXOTE",
"meta": {
"server": "europe",
"activity": "low"
}
}
}
}

Example 2: Using two arguments

We can also use walk with two arguments. Given the following data structure:

{
"users": {
"user_one": {
"first_name": "NAPOLEON",
"last_name": "BONAPARTE",
"meta": {
"server": "OCEANIA",
"activity": "HIGH"
}
},
"user_two": {
"first_name": "DON",
"last_name": "QUIXOTE",
"meta": {
"server": "EUROPE",
"activity": "LOW"
}
}
}
}

We could lowercase all user information in the above list with the following:

walk(LOWER, users)

This returns the following:

{
"users": {
"user_one": {
"first_name": "napoleon",
"last_name": "bonaparte",
"meta": {
"server": "oceania",
"activity": "high"
}
},
"user_two": {
"first_name": "don",
"last_name": "quixote",
"meta": {
"server": "europe",
"activity": "low"
}
}
}
}