@@ -167,7 +167,84 @@ func main() {
167167 func (x int ) string { return fmt .Sprintf ("[%d]" , x * 10 ) },
168168 ).ToSlice ()
169169 fmt .Printf ("Input: %v\n " , numbers8 )
170- fmt .Printf ("Skip(3) -> Where(x < 8) -> Take(3) -> Select: %v\n " , combined )
170+ fmt .Printf ("Skip(3) -> Where(x < 8) -> Take(3) -> Select: %v\n \n " , combined )
171+
172+ // Пример 14: SelectWithIndex - метод с индексом
173+ fmt .Println ("Example 14: SelectWithIndex (method with index)" )
174+ numbers9 := []int {1 , 2 , 3 , 4 , 5 }
175+ indexed := glinq .From (numbers9 ).
176+ SelectWithIndex (func (x int , idx int ) int { return x * idx }).
177+ ToSlice ()
178+ fmt .Printf ("Input: %v\n " , numbers9 )
179+ fmt .Printf ("SelectWithIndex(x * idx): %v\n \n " , indexed )
180+
181+ // Пример 15: SelectWithIndex - функция с индексом (разные типы)
182+ fmt .Println ("Example 15: SelectWithIndex function (different types with index)" )
183+ numbers10 := []int {10 , 20 , 30 }
184+ indexedStrings := glinq .SelectWithIndex (
185+ glinq .From (numbers10 ),
186+ func (x int , idx int ) string { return fmt .Sprintf ("Value_%d_at_Index_%d" , x , idx ) },
187+ ).ToSlice ()
188+ fmt .Printf ("Input: %v\n " , numbers10 )
189+ fmt .Printf ("SelectWithIndex to string: %v\n \n " , indexedStrings )
190+
191+ // Пример 16: SelectWithIndex с Where
192+ fmt .Println ("Example 16: Where + SelectWithIndex" )
193+ numbers11 := []int {1 , 2 , 3 , 4 , 5 , 6 }
194+ filteredIndexed := glinq .SelectWithIndex (
195+ glinq .From (numbers11 ).
196+ Where (func (x int ) bool { return x % 2 == 0 }),
197+ func (x int , idx int ) string {
198+ return fmt .Sprintf ("Even[%d]=%d" , idx , x )
199+ },
200+ ).ToSlice ()
201+ fmt .Printf ("Input: %v\n " , numbers11 )
202+ fmt .Printf ("Where(even) -> SelectWithIndex: %v\n \n " , filteredIndexed )
203+
204+ // Пример 17: Aggregate - сумма
205+ fmt .Println ("Example 17: Aggregate - Sum" )
206+ numbers12 := []int {1 , 2 , 3 , 4 , 5 }
207+ sum := glinq .From (numbers12 ).Aggregate (0 , func (acc , x int ) int { return acc + x })
208+ fmt .Printf ("Input: %v\n " , numbers12 )
209+ fmt .Printf ("Aggregate sum: %d\n \n " , sum )
210+
211+ // Пример 18: Aggregate - произведение
212+ fmt .Println ("Example 18: Aggregate - Product" )
213+ numbers13 := []int {2 , 3 , 4 }
214+ product := glinq .From (numbers13 ).Aggregate (1 , func (acc , x int ) int { return acc * x })
215+ fmt .Printf ("Input: %v\n " , numbers13 )
216+ fmt .Printf ("Aggregate product: %d\n \n " , product )
217+
218+ // Пример 19: Aggregate - конкатенация строк
219+ fmt .Println ("Example 19: Aggregate - String concatenation" )
220+ words := []string {"Hello" , " " , "World" , "!" }
221+ concatenated := glinq .From (words ).Aggregate ("" , func (acc , x string ) string { return acc + x })
222+ fmt .Printf ("Input: %v\n " , words )
223+ fmt .Printf ("Aggregate concatenation: '%s'\n \n " , concatenated )
224+
225+ // Пример 20: Aggregate с фильтрацией
226+ fmt .Println ("Example 20: Aggregate with Where filter" )
227+ numbers14 := []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }
228+ sumOfEvens := glinq .From (numbers14 ).
229+ Where (func (x int ) bool { return x % 2 == 0 }).
230+ Aggregate (0 , func (acc , x int ) int { return acc + x })
231+ fmt .Printf ("Input: %v\n " , numbers14 )
232+ fmt .Printf ("Sum of even numbers: %d\n \n " , sumOfEvens )
233+
234+ // Пример 21: Aggregate с кастомным типом
235+ fmt .Println ("Example 21: Aggregate with custom type" )
236+ type Point struct {
237+ X , Y int
238+ }
239+ points := []Point {{1 , 2 }, {3 , 4 }, {5 , 6 }}
240+ totalPoint := glinq .From (points ).Aggregate (
241+ Point {0 , 0 },
242+ func (acc , p Point ) Point {
243+ return Point {acc .X + p .X , acc .Y + p .Y }
244+ },
245+ )
246+ fmt .Printf ("Input points: %+v\n " , points )
247+ fmt .Printf ("Aggregate sum: Point{X:%d, Y:%d}\n \n " , totalPoint .X , totalPoint .Y )
171248
172249 fmt .Println ("\n === End of Examples ===" )
173250}
0 commit comments