Search code examples
groovyfunctional-programminggroovy-console

fpiglet take infinite list freezes


Assuming:

import static fpig.common.functions.FromOperators.*
import static fpig.groovylist.asfunlist.functions.InAndOutOfFunLists.*
import static fpig.funlist.functions.BaseFL.*
import static fpig.funlist.functions.Infinity.*

I can't work out why this works:

def res= filter{it%7==0} << filter(SMALLER(20)) << naturalNumbers()
funlistOutTake(1) << res

and this works:

def res= filter{it%7==0} << filter(SMALLER(50)) << naturalNumbers()
funlistOutTake(2) << res

but this freezes:

def res= filter{it%7==0} << filter(SMALLER(20)) << naturalNumbers()
funlistOutTake(2) << res

I'm using the groovy console for this and fpiglet 0.0.1-SNAPSHOT

As a side note, when this freezes, interrupt script doesn't work, does anyone know why?


Solution

  • I have checked in a fix.

    The issue was with how funlistOutTake(2) worked. It wanted to evaluate tail of the lazy list on the last (second) record. Another words, this code:

    def res= filter{it%7==0} << naturalNumbers()
    funlistOutTake(2) << res
    

    would have evaluated all natural numbers up to 21.

    Just (obvious) side-note:

    def res= filter{it%7==0} << filter(SMALLER(20)) << naturalNumbers()
    funlistOutTake(3) << res
    

    has no chance of working because to get third element on the lazy evaluated 'res' list the second filter: filter(SMALLER(20)) ends up being invoked forever in hope that eventually it will find third number (>19) which can be evaluated by the first filter.

    Note, take(n) had the same issue and now should be fixed.