Giter VIP home page Giter VIP logo

Comments (3)

ruby0x1 avatar ruby0x1 commented on July 21, 2024

Interesting! Sequences are evaluated lazily, which seems to be involved.
If you resolve the map sequence first via toList it prints twice only, since the where is only on the list

var bar = [1, 2].map{|x| Foo.new(x) }.toList.where{|x| x.num > 0}.toList

from wren.

mhermier avatar mhermier commented on July 21, 2024

from wren.

pedro-w avatar pedro-w commented on July 21, 2024

I thought this was surprising too - as mhermier says it is because of the way WhereSequence calls iteratorValue on its 'upstream' sequence (at least) once to find a value that satisfies the filter and once to actually get the value (see below). Specifically iteratorValue calls back though the chain every time, in this case calling the map function on the way to the source list [1,2]

If you have two where clauses then it prints 'Creating foo' six times

var bar = [1, 2].
 map{|x| Foo.new(x) }.
 where{|x| x.num > 0}.
 where{|x| x.num < 10}.
 toList

An option would be to have WhereSequence's iterator memoise the last result but assuming you don't want to change the code, would it be possible to make this a bit clearer in the iterator protocol docs?
Thanks!

wren/src/vm/wren_core.wren

Lines 168 to 182 in c2a75f1

class WhereSequence is Sequence {
construct new(sequence, fn) {
_sequence = sequence
_fn = fn
}
iterate(iterator) {
while (iterator = _sequence.iterate(iterator)) {
if (_fn.call(_sequence.iteratorValue(iterator))) break
}
return iterator
}
iteratorValue(iterator) { _sequence.iteratorValue(iterator) }
}

from wren.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.