Giter Site home page Giter Site logo

Comments (2)

cschomburg avatar cschomburg commented on August 25, 2024

I tried to find a workaround for my iterators, but I noticed it does not only affect coroutines. I think it is a problem with tail calls in the iterator function, maybe the stack gets confused.

This example results in an endless loop:

local get_val = function(i)
        if i == 1 then return i, "a" end
        if i == 2 then return i, "b" end
        return nil
end

local my_next = function(invariant, i)
        i = (i or 0) + 1
        print("next:", i)
        return get_val(i)
end

for k, v in my_next do
        print("for:", k, v)
end

Output:

next:   1
for:    1       1
for:    1       1
for:    1       1
... and so on

It looks like the for-loop tries to call the inner function get_val and not the iterator my_next. It does not happen if we inline the get_val or eliminate the tail call by saving its results:

local get_val = function(i)
        if i == 1 then return i, "a" end
        if i == 2 then return i, "b" end
        return nil
end

local my_next = function(invariant, i)
        i = (i or 0) + 1
        print("next:", i)
        local a, b = get_val(i)
        return a, b
end

for k, v in my_next do
        print("for:", k, v)
end

Works correctly:

next:   1
for:    1       a
next:   2
for:    2       b
next:   3

from gopher-lua.

yuin avatar yuin commented on August 25, 2024

Thank you again, @xconstruct .
I've fixed the bug in dd0fd53 thanks to your detailed report πŸ‘

from gopher-lua.

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.