Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
digging out data in style with puppet 4.5.0
$data = {
persons => {
'Henrik' => {
mother => 'Anna-Greta',
father => 'Bengt',
},
'Anna-Greta' => {
mother => 'Margareta',
father => 'Harald',
children => ['Henrik', 'Annika']
},
'Bengt' => {
mother => 'Maja',
father => 'Ivar'
},
'Maja' => {
children => ['Bengt', 'Greta', 'Britta', 'Helge']
},
}
}
function custom_family_search(String $mother) {
# 1. start by finding the mother's children and pick the first
$data.dig('persons', $mother, 'children', 0)
# 2. Get the father of the child (needs to be looked up since
# $x here is just the name of the person).
.then |$x| { $data.dig('persons', $x, 'father') }
# 3. Look up the siblings of found father, and return those
# as well as the father (needed to eliminate father in
# the next step. ($x is father from previous step).
.then |$x| { [$data.dig('persons', $data.dig('persons', $x, 'mother'), 'children'), $x] }
# 4. Eliminate father from siblings
# Previous step is never undef since we construct an array,
# but the first slot in the array may be undef, or something that
# is not an array. Thus, we don't need the conditional 'then'
# function, and can instad use the 'with' function.
# We then use a 'case' to match the 'happy' path where the
# name of the father is 'subtracted'/removed
# from the array of siblings. The 'sad' path produces
# 'undef' and lets the next step deal with it.
#
.with |$x| { case $x {
[Array[String], String] : { $x[0] - $x[1] }
default : { undef }
}
}
# 5. we fail if we did not get a result
#
.lest || { fail("Could not find aunts and uncles...") }
# Function returns the value of the last call in the chain
# which will be ['Bengt', 'Greta', 'Britta', 'Helge']
}
notice custom_family_search('Anna-Greta')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment