whitequark / ast

A library for working with Abstract Syntax Trees.
MIT License
195 stars 25 forks source link

each and first #10

Closed rubydesign closed 9 years ago

rubydesign commented 9 years ago

I seem to be writing a lot of node.to_a.each would you accept an each on node?

Similarly, the multi-assignment works well for multiples . Often i have just a single value, so the choice seems to be node.to_a.first or (slightly more expressive) node.children.first Both of which seem long, so i already have a first function on node, do you want that ?

whitequark commented 9 years ago

You can do child, = *node. If this is not in the docs, it should be.

When do you use to_a.each?

rubydesign commented 9 years ago

hmm, i didn't know you could do that. Frankly looks weird, rather use my first.

each off course to go over children. you're saying i shouldn't have to. And maybe when i switch to the instance variable an your processor i won't. But now i still do and it seemed a sensible thing to have in there. But i seem to be receiving a more is less attitude , yes ? Always surprises me in ruby projects, ruby being known for having 7 ways of doing anything, but ok, i get it.

whitequark commented 9 years ago

you're saying i shouldn't have to

I'm not. I am specifically asking what your use case is, because I have no idea what is it. I.e. children are usually heterogeneous; what common action over them is desirable?

rubydesign commented 9 years ago

ok, maybe the question was slightly ill formed then. Because the answer to your question was so obvious.

If you're asking why i need to iterate. Other than off course, because i have a collection. To process them.

whitequark commented 9 years ago

Like if you have a (list) or a (hash)? Yeah, that's reasonable. I would accept a PR with each.

rubydesign commented 9 years ago

sort of, i have an s(:expression_list , exp1 , exp2...) which is inside all statements, so common. But i'll try that Processor with a stack now. The code i'm talking about is temporarily here btw of almost nothing

rubydesign commented 9 years ago

So, that processor / instance approach worked. Quite like it even, so in total the switch to ast has bee great :-)