Recursive search golf (JS only)

2

Sorry folks, but what I've got for you today is a golf but just limited by JS (or, to be strict, for a day of posting - an ES6 - the most modern set of it - if its ES7 - feel free to EDIT! use MOST MODERN features).

So if you afford that restriction - let's proceed!

Assume we have JSON root data structure r and some global ID g=0:

g=0;r={i:g++,x:0,c:[]}

This creates a root r of a data tree, indexed by unique index i, with a data payload x, where we (for simplicity) let x to be always zero, and child array c where c[j] just an object of same syntax, as r its own. Sorry for extra data x, indeed, we will don use it ever! Our goal is to do golfed ID lookup method!

My grandfather implementation was quite a mess... Just imagine, in his times people named functions with more than one symbol! Just like that:

find_subid = (id, childVec) => {
    for( child of childVec ) {
        found = find_id( id, child );
        if( found ) // 200 
            return found;
        }
        return null; // 404
    }
find_id = (id, parent) => (id == parent.i) ? parent : find_subid(id, parent.c);

Now I can call find_id(123,g) but.. I just feel that's too long to write today!

So we should fix his errors and do our own, faster, harder and shorter version in most possible modern JS as of the time you are reading this text!

xakepp35

Posted 2019-02-09T19:30:47.277

Reputation: 293

Question was closed 2019-02-09T20:07:06.477

4"json" is a specific textual way of representing javascript objects, it does not mean javascript objects themselves. – Sparr – 2019-02-09T19:57:52.957

1@Shaggy what is unclear? I am asking to golf some existing javascript code! – xakepp35 – 2019-02-09T20:54:18.057

2I believe the return null; needs to be after the for-loop, not inside. Your question could use some test cases and a clear specification how the json is allowed to look. – Laikoni – 2019-02-11T19:32:56.387

1I would consider this question as unclear as 1. You should fix up your sample code as mentioned above. 2. You should and some testcases so we can test our implementation. 3. You should include a clear description about what you want to implement, and define the "ID lookup method". – tsh – 2019-02-13T02:32:28.000

FWIW, I had i=>g=o=>o.i-i?o.c.flatMap(g):[o] for 32 bytes. – Arnauld – 2019-03-01T16:58:55.190

No answers