Re: fetch & store
- Posted by jmduro Apr 12, 2018
- 1155 views
I have drastically reduced the size of my recursive functions for nested sequence management and I added a removal function.
include std/search.e include std/text.e -------------------------------------------------------------------------------- public function get_nested(object structure, sequence targetPath, object def=0, sequence currentPath={}) if equal(currentPath, targetPath) then return structure else integer digit = targetPath[length(currentPath)+1] if digit <= length(structure) then return get_nested(structure[digit], targetPath, def, currentPath&digit) else return def end if end if end function -------------------------------------------------------------------------------- public function set_nested(object structure, sequence targetPath, object x, sequence currentPath={}) integer digit = targetPath[length(currentPath)+1] for i = 1 to digit do if i > length(structure) then structure = append(structure, {}) end if if i = digit then if equal(currentPath&{i}, targetPath) then structure[i] = x else structure[i] = set_nested(structure[i], targetPath, x, currentPath&i) end if end if end for return structure end function -------------------------------------------------------------------------------- public function remove_nested(object structure, sequence targetPath, sequence currentPath={}) integer digit = targetPath[length(currentPath)+1] if digit <= length(structure) then if equal(currentPath&{digit}, targetPath) then structure = remove(structure, digit) else structure[digit] = remove_nested(structure[digit], targetPath, currentPath&digit) end if end if return structure end function ------------------------------------------------------------------------------
Jean-Marc