5 ms·
JavaScript Generators, Meet XPath
- k__ 6y agowill the spread operator really evaluate the generator lazily?
- Slackwise 6y agoPretty sure it will not. Nothing is lazy in JS by default: > One caveat of spreading iterables: JavaScript creates an array out of the elements of the iterable. That might be very wasteful for extremely large collections. For example, if we spread a large collection just to find an element in the collection, it might have been wiser to iterate over the element using its iterator directly. > And if we have an infinite collection, spreading is going to fail outright. http://raganwald.com/2015/02/17/lazy-iteratables-in-javascript.html http://raganwald.com/2015/02/17/lazy-iteratables-in-javascri...
- matt-attack 6y agoSteelbrain’s use of the async for loop is indeed lazy.
- steelbrain 6y ago> Pretty sure it will not. Nothing is lazy in JS by default: It really depends on the logic written. You can solve the same thing in many different ways, the way chosen by the author doesn't seem to be a lazy one. Not the language's fault. It's simply doing what is asked of it. Eg, see https://news.ycombinator.com/item?id=24265507 https://news.ycombinator.com/item?id=24265507
- k__ 6y agoYes, I thought so too. If they spread anyway, they could leave the generator away in the first place.
- k__ 6y agoI was just confused, why use a generator when you spread it anyway.
- contravariant 6y agoI don't think any language has a lazy spread operator. Haskell is the only language I know off lazy enough to conceivably have one, but it doesn't accept arbitrary lists of arguments (unless you count having an actual list as an argument, which is the better option if you want laziness really).
- steelbrain 6y agoGreat idea! Couple of things I would change here: - Instead of modifying prototype, which would pollute the environment for any other modules to work with - use regular old functions - Instead of spreading the iterable to an array and then calling `.forEach` on it, I would just iterate the source generator directly, it would look like: for (const node of xpath(el, 'xpath')) { node.textContent = node.textContent.replace("Hello", "Greetings") }
- nsonha 6y agoThis is not very hacker news worthy to be frank. Seems like a typical medium article. Not using prototype and maybe convert that into a higher order function (not just xpath) is a good start.
- ajkjk 6y agoSeems plenty HN-worthy to me. It's up to the community to decide by voting.
- baron816 6y agoI'd like to take this opportunity to plug my npm package `fromable`(https://github.com/baron816/fromable https://github.com/baron816/fromable) which makes it easy an efficient to transform iterables and output the result to any collection. It's kind of a `Array.from` meets transducers.
- zeronone 6y agoI wonder if the risk of memory leak increases when using the generators.
- sktguha 6y agoI honestly don't see much the value of generators here. we can simply return all the results directly from the xpath function that you created. Are there any use case where you would want to stream the elements one by one ? you have got all the results in memory anyways inside the xpath function
- Akronymus 6y agoTo me, it seems to support lazy evaluation much better with a generator. Additionally composability seems improved, due to all functions being able to be written in a way where they themselves become generators, so you apply all steps only to each result you retrieve. Potentially saving quite a bit of execution time, if you don't need the full collection.
- gridlockd 6y ago> you have got all the results in memory anyways inside the xpath function Not necessarily, that's an implementation detail. The result of an XPath query is not an array, it's some object that accepts integer indices for lookup. If you were to return an array, there would be no way to break out of iteration early without paying for every lookup. For something like XPath, breaking out early is a likely scenario.
- whereistimbo 6y agoXPath works best with XHTML
- mumblerino 6y agoNo it doesn’t. XPath works on the nodes. How you get to the nodes is irrelevant.
- ricardobeat 6y agoGenerators probably only add overhead here since there is nothing asynchronous happening. You can just as well wrap the first version in a function and return an array: xpath("//text()[contains(., 'Hello')]").forEach(...)
- gridlockd 6y agoGenerators/Iterators are not just for asynchronous code. Generators may be a bit slower than iterating over an array, but arrays use more memory and put more pressure on the garbage collector. Your mileage will vary.
- deleted 6y ago[deleted]
- ricardobeat 6y agoTrue, though I suspect the impact in the case of a node list is negligible; in quick tests I couldn't detect any meaningful difference even while looping through 10k DOM elements.
- mumblerino 6y agoFor the love of god stop extending the prototype for no good reason. Just use a regular function. element.xpath(abc) is in no way better than xpath(element, abc)
- gridlockd 6y agoPlease, using for loops is okay, especially if you're not actually writing functional code. [...document.xpath("//text()[contains(., 'Hello')]")] .forEach(node => node.textContent = node.textContent.replace("Hello", "Greetings")); versus for (const node of document.xpath("//text()[contains(., 'Hello')]"){ node.textContent = node.textContent.replace("Hello", "Greetings"); } No allocation of a pointless array, no redundant iteration, fewer "[]", "()" and no "=>" or "...". No downside whatsoever!
- RussianCow 6y ago> No downside whatsoever! That's not entirely true if you need to support IE, in which case Babel (or whatever compiler you're using) will generate extra code for the `for...of` loop. But even then, I would still recommend using it for the reasons you mentioned.