JavaScript Iterator Class

后端 未结 7 1345
挽巷
挽巷 2020-12-29 07:55

Do you know a JavaScript library that implements a generic Iterator class for collections (be it Arrays or some abstract Enumerable) with a full set of features, like the Go

7条回答
  •  -上瘾入骨i
    2020-12-29 08:18

    Ok, the enumerable pattern is not a real iterator then.

    Is this (below) useful for you? It conforms to the sematics you gave at least. As usual there are tradeoffs to be made here and there, and I didn't think very hard when deciding this time :).
    And maybe you would like to be able to send in a number or two and iterate over a range in that way. But this could maybe be a start (there's support for iterating over hashes, arrays and strings).

    It's a whole demo page which runs itself and does some debug output, but the (possibly) interesting stuff is in the

    window.npup = (function() {
        [...]
    })();
    

    spot.

    Maybe it is just me who doesn't get it at all, but what would you use such a java-like Iterator for in a real situation?

    Best /npup

    
    
    untitled
    
    
    
        

    提交回复
    热议问题