walk

walk (all lowercase) 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.

Function category: Special

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

Let's say we were 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"
}
}
}
}

Example 1: Using three arguments

Let's say we wanted to change to uppercase the names of some users from the data structure above, use the following function.

# Transform to uppercase with a depth of 2
walk(UPPER, 2, users)
# Returns the following 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"
}
}
}
}

Example 2: Using two arguments

We can also use walk with two arguments.

# Response with values in uppercase
{
"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 change to lowercase all user information in the above list using the following function.

# Transform all to lowercase
walk(LOWER, users)
# Returns with all values in lowercase
{
"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"
}
}
}
}