Monday, March 2, 2015

Golang and Lazy implementation

These tools are pretty helpful for me: "github.com/noypi/fp"

Exmaples;

LazyInAsync1() below waits for inputs from qLazy; and outputs from LazyInAsync1 are provided by q1.

 func fb(x int) int {  
      switch x {  
      case 0:  
           return 0  
      case 1:  
           return 1  
      default:  
           return fb(x-1) + fb(x-2)  
      }  
      return 0  
 }  
 // wraps a function and is executed later when input is available  
 func WrapALazyFunctionSample() {  
      log.Println("+WrapAFunctionSample()")  
      defer log.Println("-WrapAFunctionSample()")  
      as := []int{26, 27, 29, 0, 1, 2, 26, 27, 29, 0, 1, 2, 26, 27, 29, 0, 1, 2}  
      // range will concurrently execute each  
      qLazy := Range(func(a, i AnyVal) (ret AnyVal, skip bool) {  
           ret = &Tuple2{  
                A: a,  
                B: i,  
           }  
           return  
      }, as)  
      q1 := LazyInAsync1(func(x AnyVal) (ret AnyVal, skip bool) {  
           ret = fb(x.(*Tuple2).A.(int))  
           return  
      }, qLazy)  
      // print results  
      for a := range q1.Q() {  
           log.Printf("ret=%d\n", a)  
      }  
 }  


See github.com/noypi/examples for more.

In addition, futures and list comprehension for golang from github.com/noypi/fp can give you ideas.