4 ms·
Thanks for the article. There's a class of problems that I wouldn't know how to solve using the constrains you propose. Given { a: { b: { c: 4 } }, d: 2 }
by pygy_ 9y ago
Thanks for the article. There's a class of problems that I wouldn't know how to solve using the constrains you propose.
Given
{ a: { b: { c: 4 } }, d: 2 }
return
{ 'a.b.c': 4, d: 2 }
My go to solution uses mutable state, self-recursion, and intermediate variables:
function flatten(source) {
var result = {}
_flatten(source, null, result)
return result
}
where `_flatten(source, prefix, result)` is self-recursive (`prefix` accumulates the keys) and populates `result[prefix]=value` when value is a string. `result` is conceptually an IO monad that's passed along.
I guess the general problem would be traverse a tree of arbitrary depth and accumulate structured data on the go. Prominent examples would be compilers.
Any hints on where to start?