selectmany.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package help
  2. // SelectMany projects each element of a collection to a Query, iterates and
  3. // flattens the resulting collection into one collection.
  4. func (q Query) SelectMany(selector func(interface{}) Query) Query {
  5. return Query{
  6. Iterate: func() Iterator {
  7. outernext := q.Iterate()
  8. var inner interface{}
  9. var innernext Iterator
  10. return func() (item interface{}, ok bool) {
  11. for !ok {
  12. if inner == nil {
  13. inner, ok = outernext()
  14. if !ok {
  15. return
  16. }
  17. innernext = selector(inner).Iterate()
  18. }
  19. item, ok = innernext()
  20. if !ok {
  21. inner = nil
  22. }
  23. }
  24. return
  25. }
  26. },
  27. }
  28. }
  29. // SelectManyT is the typed version of SelectMany.
  30. //
  31. // - selectorFn is of type "func(TSource)Query"
  32. //
  33. // NOTE: SelectMany has better performance than SelectManyT.
  34. func (q Query) SelectManyT(selectorFn interface{}) Query {
  35. selectManyGenericFunc, err := newGenericFunc(
  36. "SelectManyT", "selectorFn", selectorFn,
  37. simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(Query))),
  38. )
  39. if err != nil {
  40. panic(err)
  41. }
  42. selectorFunc := func(inner interface{}) Query {
  43. return selectManyGenericFunc.Call(inner).(Query)
  44. }
  45. return q.SelectMany(selectorFunc)
  46. }
  47. // SelectManyIndexed projects each element of a collection to a Query, iterates
  48. // and flattens the resulting collection into one collection.
  49. //
  50. // The first argument to selector represents the zero-based index of that
  51. // element in the source collection. This can be useful if the elements are in a
  52. // known order and you want to do something with an element at a particular
  53. // index, for example. It can also be useful if you want to retrieve the index
  54. // of one or more elements. The second argument to selector represents the
  55. // element to process.
  56. func (q Query) SelectManyIndexed(selector func(int, interface{}) Query) Query {
  57. return Query{
  58. Iterate: func() Iterator {
  59. outernext := q.Iterate()
  60. index := 0
  61. var inner interface{}
  62. var innernext Iterator
  63. return func() (item interface{}, ok bool) {
  64. for !ok {
  65. if inner == nil {
  66. inner, ok = outernext()
  67. if !ok {
  68. return
  69. }
  70. innernext = selector(index, inner).Iterate()
  71. index++
  72. }
  73. item, ok = innernext()
  74. if !ok {
  75. inner = nil
  76. }
  77. }
  78. return
  79. }
  80. },
  81. }
  82. }
  83. // SelectManyIndexedT is the typed version of SelectManyIndexed.
  84. //
  85. // - selectorFn is of type "func(int,TSource)Query"
  86. //
  87. // NOTE: SelectManyIndexed has better performance than SelectManyIndexedT.
  88. func (q Query) SelectManyIndexedT(selectorFn interface{}) Query {
  89. selectManyIndexedGenericFunc, err := newGenericFunc(
  90. "SelectManyIndexedT", "selectorFn", selectorFn,
  91. simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(Query))),
  92. )
  93. if err != nil {
  94. panic(err)
  95. }
  96. selectorFunc := func(index int, inner interface{}) Query {
  97. return selectManyIndexedGenericFunc.Call(index, inner).(Query)
  98. }
  99. return q.SelectManyIndexed(selectorFunc)
  100. }
  101. // SelectManyBy projects each element of a collection to a Query, iterates and
  102. // flattens the resulting collection into one collection, and invokes a result
  103. // selector function on each element therein.
  104. func (q Query) SelectManyBy(selector func(interface{}) Query,
  105. resultSelector func(interface{}, interface{}) interface{}) Query {
  106. return Query{
  107. Iterate: func() Iterator {
  108. outernext := q.Iterate()
  109. var outer interface{}
  110. var innernext Iterator
  111. return func() (item interface{}, ok bool) {
  112. for !ok {
  113. if outer == nil {
  114. outer, ok = outernext()
  115. if !ok {
  116. return
  117. }
  118. innernext = selector(outer).Iterate()
  119. }
  120. item, ok = innernext()
  121. if !ok {
  122. outer = nil
  123. }
  124. }
  125. item = resultSelector(item, outer)
  126. return
  127. }
  128. },
  129. }
  130. }
  131. // SelectManyByT is the typed version of SelectManyBy.
  132. //
  133. // - selectorFn is of type "func(TSource)Query"
  134. // - resultSelectorFn is of type "func(TSource,TCollection)TResult"
  135. //
  136. // NOTE: SelectManyBy has better performance than SelectManyByT.
  137. func (q Query) SelectManyByT(selectorFn interface{},
  138. resultSelectorFn interface{}) Query {
  139. selectorGenericFunc, err := newGenericFunc(
  140. "SelectManyByT", "selectorFn", selectorFn,
  141. simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(Query))),
  142. )
  143. if err != nil {
  144. panic(err)
  145. }
  146. selectorFunc := func(outer interface{}) Query {
  147. return selectorGenericFunc.Call(outer).(Query)
  148. }
  149. resultSelectorGenericFunc, err := newGenericFunc(
  150. "SelectManyByT", "resultSelectorFn", resultSelectorFn,
  151. simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
  152. )
  153. if err != nil {
  154. panic(err)
  155. }
  156. resultSelectorFunc := func(outer interface{}, item interface{}) interface{} {
  157. return resultSelectorGenericFunc.Call(outer, item)
  158. }
  159. return q.SelectManyBy(selectorFunc, resultSelectorFunc)
  160. }
  161. // SelectManyByIndexed projects each element of a collection to a Query,
  162. // iterates and flattens the resulting collection into one collection, and
  163. // invokes a result selector function on each element therein. The index of each
  164. // source element is used in the intermediate projected form of that element.
  165. func (q Query) SelectManyByIndexed(selector func(int, interface{}) Query,
  166. resultSelector func(interface{}, interface{}) interface{}) Query {
  167. return Query{
  168. Iterate: func() Iterator {
  169. outernext := q.Iterate()
  170. index := 0
  171. var outer interface{}
  172. var innernext Iterator
  173. return func() (item interface{}, ok bool) {
  174. for !ok {
  175. if outer == nil {
  176. outer, ok = outernext()
  177. if !ok {
  178. return
  179. }
  180. innernext = selector(index, outer).Iterate()
  181. index++
  182. }
  183. item, ok = innernext()
  184. if !ok {
  185. outer = nil
  186. }
  187. }
  188. item = resultSelector(item, outer)
  189. return
  190. }
  191. },
  192. }
  193. }
  194. // SelectManyByIndexedT is the typed version of SelectManyByIndexed.
  195. //
  196. // - selectorFn is of type "func(int,TSource)Query"
  197. // - resultSelectorFn is of type "func(TSource,TCollection)TResult"
  198. //
  199. // NOTE: SelectManyByIndexed has better performance than
  200. // SelectManyByIndexedT.
  201. func (q Query) SelectManyByIndexedT(selectorFn interface{},
  202. resultSelectorFn interface{}) Query {
  203. selectorGenericFunc, err := newGenericFunc(
  204. "SelectManyByIndexedT", "selectorFn", selectorFn,
  205. simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(Query))),
  206. )
  207. if err != nil {
  208. panic(err)
  209. }
  210. selectorFunc := func(index int, outer interface{}) Query {
  211. return selectorGenericFunc.Call(index, outer).(Query)
  212. }
  213. resultSelectorGenericFunc, err := newGenericFunc(
  214. "SelectManyByIndexedT", "resultSelectorFn", resultSelectorFn,
  215. simpleParamValidator(newElemTypeSlice(new(genericType), new(genericType)), newElemTypeSlice(new(genericType))),
  216. )
  217. if err != nil {
  218. panic(err)
  219. }
  220. resultSelectorFunc := func(outer interface{}, item interface{}) interface{} {
  221. return resultSelectorGenericFunc.Call(outer, item)
  222. }
  223. return q.SelectManyByIndexed(selectorFunc, resultSelectorFunc)
  224. }