skip.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package help
  2. // Skip bypasses a specified number of elements in a collection and then returns
  3. // the remaining elements.
  4. func (q Query) Skip(count int) Query {
  5. return Query{
  6. Iterate: func() Iterator {
  7. next := q.Iterate()
  8. n := count
  9. return func() (item interface{}, ok bool) {
  10. for ; n > 0; n-- {
  11. item, ok = next()
  12. if !ok {
  13. return
  14. }
  15. }
  16. return next()
  17. }
  18. },
  19. }
  20. }
  21. // SkipWhile bypasses elements in a collection as long as a specified condition
  22. // is true and then returns the remaining elements.
  23. //
  24. // This method tests each element by using predicate and skips the element if
  25. // the result is true. After the predicate function returns false for an
  26. // element, that element and the remaining elements in source are returned and
  27. // there are no more invocations of predicate.
  28. func (q Query) SkipWhile(predicate func(interface{}) bool) Query {
  29. return Query{
  30. Iterate: func() Iterator {
  31. next := q.Iterate()
  32. ready := false
  33. return func() (item interface{}, ok bool) {
  34. for !ready {
  35. item, ok = next()
  36. if !ok {
  37. return
  38. }
  39. ready = !predicate(item)
  40. if ready {
  41. return
  42. }
  43. }
  44. return next()
  45. }
  46. },
  47. }
  48. }
  49. // SkipWhileT is the typed version of SkipWhile.
  50. //
  51. // - predicateFn is of type "func(TSource)bool"
  52. //
  53. // NOTE: SkipWhile has better performance than SkipWhileT.
  54. func (q Query) SkipWhileT(predicateFn interface{}) Query {
  55. predicateGenericFunc, err := newGenericFunc(
  56. "SkipWhileT", "predicateFn", predicateFn,
  57. simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
  58. )
  59. if err != nil {
  60. panic(err)
  61. }
  62. predicateFunc := func(item interface{}) bool {
  63. return predicateGenericFunc.Call(item).(bool)
  64. }
  65. return q.SkipWhile(predicateFunc)
  66. }
  67. // SkipWhileIndexed bypasses elements in a collection as long as a specified
  68. // condition is true and then returns the remaining elements. The element's
  69. // index is used in the logic of the predicate function.
  70. //
  71. // This method tests each element by using predicate and skips the element if
  72. // the result is true. After the predicate function returns false for an
  73. // element, that element and the remaining elements in source are returned and
  74. // there are no more invocations of predicate.
  75. func (q Query) SkipWhileIndexed(predicate func(int, interface{}) bool) Query {
  76. return Query{
  77. Iterate: func() Iterator {
  78. next := q.Iterate()
  79. ready := false
  80. index := 0
  81. return func() (item interface{}, ok bool) {
  82. for !ready {
  83. item, ok = next()
  84. if !ok {
  85. return
  86. }
  87. ready = !predicate(index, item)
  88. if ready {
  89. return
  90. }
  91. index++
  92. }
  93. return next()
  94. }
  95. },
  96. }
  97. }
  98. // SkipWhileIndexedT is the typed version of SkipWhileIndexed.
  99. //
  100. // - predicateFn is of type "func(int,TSource)bool"
  101. //
  102. // NOTE: SkipWhileIndexed has better performance than SkipWhileIndexedT.
  103. func (q Query) SkipWhileIndexedT(predicateFn interface{}) Query {
  104. predicateGenericFunc, err := newGenericFunc(
  105. "SkipWhileIndexedT", "predicateFn", predicateFn,
  106. simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(bool))),
  107. )
  108. if err != nil {
  109. panic(err)
  110. }
  111. predicateFunc := func(index int, item interface{}) bool {
  112. return predicateGenericFunc.Call(index, item).(bool)
  113. }
  114. return q.SkipWhileIndexed(predicateFunc)
  115. }