This is a creation in Article, where the information may have evolved or changed.
Continue on one of the ideas to solve the problem of delayed computing. The related keyword are apt to think of yield.
The introduction of Lazy Computing in C # can refer to articles such as "C # features that cannot be said-iterators (bottom) yield, and delay calculation of streams"
Http://www.cnblogs.com/yuyijq/archive/2008/07/19/1246609.html
The article mentions
The method of returning only one element at a time (reader jumps to part two below)
From a more formal point of view, a function is required, each call to remove the first element of the list, if the list is empty, returns false.
type iterator func()(bool,interface{})
Then, at the time of use, the For loop will determine the output of a bool return value of TRUE.
for ok, next := iter(); ok; ok, next = iter() { fmt.Println(next) }
Define a data structure that contains the methods and lists mentioned above
type Query struct{ list []interface{}}
ITER is a iterator type, and its responsibility is to remove only the first element of the list at a time, and to judge the case of an empty list.
func (q *Query)GetIterator() iterator{ i:=0 return func() (bool,interface{}){ if i < len(q.list) { ret := q.list[i] i++ return true,ret } return false,nil }}
The following methods are used to add data
func (q *Query)generator(data interface{}){ q.list = append(q.list,data)}
First time using the singleton mode on go: once.do
var m *Queryvar once sync.Oncefunc GeneratorInstance() *Query{ once.Do(func() { m = &Query {} }) return m}
Here's how to start checking this out
func quips(name string) iterator{ GeneratorInstance().generator("Hi " + name + "!") GeneratorInstance().generator("Hi" + " good day , R u ok?") GeneratorInstance().generator("en.. ok,good day") return GeneratorInstance().GetIterator()}func main() { iter := quips("Yk kang") for ok, next := iter(); ok; ok, next = iter() { fmt.Println(next) }}
After execution, the output is:
Hi Yk kang2!
Hi Good day, R u OK?
En.. Ok,good Day
Second, combine with the slice, let the note "explore one" inside from (). Select () supports deferred calculation
The essence is to transform from the image of the getiterator, and query does not save List,list from the input, query declares the method to return item
type Iterator func()(item interface{},ok bool)type Query struct{ Iterate func() Iterator}func From(in interface{}) Query{ src := reflect.ValueOf(in) len := src.Len() return Query{ Iterate:func() Iterator{ i:=0 return func() (item interface{},ok bool){ ok = i < len if ok{ item = src.Index(i).Interface() i++ } return } }, }} next := From(userArr).Iterate() for item,ok := next(); ok; item,ok = next() { fmt.Println(item) }
This will output normally
{1 A 12}
{2 B 7}
{3 C 15}
The parameter passed by the Select method is selector func (interface{}) interface{}, which is item,ok from the iterate method of the query returned in the method body, which is passed into the selector, So the Select method is query
func (q Query) Select(selector func(interface{}) interface{}) Query { return Query{ Iterate: func() Iterator { next := q.Iterate() return func() (item interface{}, ok bool) { var it interface{} it, ok = next() if ok { item = selector(it) } return } }, }}//使用 t := From(userArr).Select(func(c interface{}) interface{}{ return c.(User).Name }) next := t.Iterate() for item,ok := next(); ok; item,ok = next() { fmt.Println(item) }
According to the logic of this function chain, the Where method can also copy the Select