You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, thanks for this cool lib. Is there any possibility to access the path info of a node inside walk or visit?
Simple example:
constSimpleZipper=makeZipper(n=>!!n.children?.length,n=>n?.children,(parent,children)=>{console.log('make node',parent,children)return{ children, parent }});consts=SimpleZipper({children: ['a',{children: ['b','c']}]});console.log(s.value())// { children: [ 'a', { children: [ 'b', 'c' ] } ] }console.log(s.next().next().next().value())// 'b'// on a Zipper node, i get the parentItems as I would want them:console.log(s.next().next().next().path.parentItems)/* [ { children: [ 'a', { children: [ 'b', 'c' ] } ] }, // grandparent { children: [ 'b', 'c' ] } // parent] */
So far so good, but if I use preWalk, the passed node is not a Zipper, but just what would be returned from value():
constmapLeafs=mapFn=>makeZipper=>preWalk((node)=>{// ideally, node should be a Zipperconstt=makeZipper(node);// I tried creating a "subzipper" (without path info)if(t.isLeaf()){console.log('map',node)returnt.edit(item=>mapFn(item/*,path*/).item;// would need path here}returnnode;});constuppercase=mapLeafs(leaf=>leaf.toUpperCase())(SimpleZipper);constuppercased=uppercase(s).value();console.log(uppercased);// { children: [ 'A', { children: [ 'B', 'C' ] } ] }
For this case (🙊) the mapping works, but without the path, I have no structural info of the current node, which would be needed for many common operations (like accumulating parent values to a child).
constmakeReduceVisitor=fn=>onPre((item,state)=>{return({state: fn(state,item)})});constreduce=(fn,initialAcc,z)=>{const{ state }=visit([makeReduceVisitor(fn)],initialAcc,z);returnstate;}reduce((acc,node)=>{if(!node.children){// also no Zipper object herereturnacc+node}returnacc;},'',s)// yields 'abc'
Still no luck here, as the given node is also just the value.
Am I missing something? The only other solution I can think of is using pipe magic like described in the readme for arrays, but I think it would be much simpler to have a Zipper instead of just the value available to walk/visit.
Thank you for this refreshing lib, which would be the perfect candidate to reimplement my hierarchical music format :)
The text was updated successfully, but these errors were encountered:
Hello, thanks for this cool lib. Is there any possibility to access the path info of a node inside walk or visit?
Simple example:
So far so good, but if I use preWalk, the passed node is not a Zipper, but just what would be returned from
value()
:For this case (🙊) the mapping works, but without the path, I have no structural info of the current node, which would be needed for many common operations (like accumulating parent values to a child).
My second attempt was creating a reduce function like mentioned in the readme:
Still no luck here, as the given node is also just the value.
Am I missing something? The only other solution I can think of is using pipe magic like described in the readme for arrays, but I think it would be much simpler to have a Zipper instead of just the value available to walk/visit.
Thank you for this refreshing lib, which would be the perfect candidate to reimplement my hierarchical music format :)
The text was updated successfully, but these errors were encountered: