Gython can compile simple generator functions where yield can be used three times, once before the loop, in the loop, and after. Work still needs to be done on the state machine transformer to support more complex yield returns.
Gython input
def fib( n:int ) -> int: int a = 0 int b = 1 int c = 0 for x in range(n): yield a c = b b = a+b a = c yield -1 def main(): arr = []int() for n in fib(20): arr.append( n )
Golang output
type fib struct { a int c int b int n int __iter_end int __iter_start int __iter_index int __done__ int } func (self *fib) __init__(n int) { self.n = n; self.a = 0; self.b = 1; self.c = 0; self.__iter_start = 0; self.__iter_index = 0; self.__iter_end = self.n; self.__done__ = 0; } func (self *fib) next() int { var __yield_return__ int if (( self.__iter_index ) < self.__iter_end) { __yield_return__ = self.a; self.c = self.b; self.b = (self.a + self.b); self.a = self.c; self.__iter_index += 1; return __yield_return__ } else { self.__done__ = 1; __yield_return__ = -1; return __yield_return__ } } func __new__fib( n int ) *fib { ob := fib{} ob.__init__(n) return &ob } func main() { arr := &[]int{}; __genfib := __new__fib(20) for __genfib.__done__ != 1 { n := __genfib.next() __0 := append(*arr,n); *arr = __0; } }
No comments:
Post a Comment