package ranges import ( "surdeus.su/core/gods/iters" cnts "golang.org/x/exp/constraints" ) type Range[V cnts.Ordered] struct { start, step, end V } // Returns the new Range strucucture with corresponding start, step and end. // If the values can never reach the end then the function will return nil. func New[V cnts.Ordered](start, step, end V) *Range[V] { return &Range[V]{ start, step, end, } } func (r *Range[V]) Chan() iters.PairChan[int, V] { less := func(a, b V) bool { return a < b } more := func(a, b V) bool { return a > b } var compare func(a, b V) bool start, step, end := r.start, r.step, r.end if start < end { compare = less } else { compare = more } c := make(iters.PairChan[int, V]) go func(){ j := 0 for i := start ; compare(i, end) ; i += step { c <- iters.Pair[int, V]{ K:j, V:i, } j++ } close(c) }() return c }