forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.go
More file actions
30 lines (26 loc) · 712 Bytes
/
reverse.go
File metadata and controls
30 lines (26 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package linq
// Reverse inverts the order of the elements in a collection.
//
// Unlike OrderBy, this sorting method does not consider the actual values
// themselves in determining the order. Rather, it just returns the elements in
// the reverse order from which they are produced by the underlying source.
func (q Query) Reverse() Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
items := []interface{}{}
for item, ok := next(); ok; item, ok = next() {
items = append(items, item)
}
index := len(items) - 1
return func() (item interface{}, ok bool) {
if index < 0 {
return
}
item, ok = items[index], true
index--
return
}
},
}
}