@@ -86,6 +86,30 @@ Example
86
86
87
87
----
88
88
89
+ instancemethod ``__getitem__[TDefault](__index_and_default) ``
90
+ ---------------------------------------------------------------
91
+
92
+ Parameters
93
+ - `__index_and_default ` (``Tuple[int, TDefault] ``)
94
+
95
+ Returns
96
+ - ``Union[TSource_co, TDefault] ``
97
+
98
+ Returns the element at specified index in the sequence or returns the default value if it does not
99
+ exist. Prefers calling `__getitem__() ` on the wrapped iterable if available, otherwise, calls
100
+ `self.element_at() `.
101
+
102
+ Example
103
+ .. code-block :: python
104
+
105
+ >> > def gen ():
106
+ ... yield 1 ; yield 10 ; yield 100
107
+
108
+ >> > Enumerable(gen())[3 , 1000 ]
109
+ 1000
110
+
111
+ ----
112
+
89
113
instancemethod ``__getitem__(index) ``
90
114
---------------------------------------
91
115
@@ -496,6 +520,35 @@ Example
496
520
497
521
----
498
522
523
+ instancemethod ``chunk(size) ``
524
+ --------------------------------
525
+
526
+ Parameters
527
+ - `size ` (``int ``)
528
+
529
+ Returns
530
+ - ``Enumerable[MutableSequence[TSource_co]] ``
531
+
532
+ Splits the elements of a sequence into chunks of size at most the provided size. Raises
533
+ `InvalidOperationError ` if `size ` is less than 1.
534
+
535
+ Example
536
+ .. code-block :: python
537
+
538
+ >> > def source (i ):
539
+ ... while True :
540
+ ... yield i
541
+ ... i *= 3
542
+
543
+ >> > en = Enumerable(source(1 )).chunk(4 ).take(3 )
544
+ >> > for chunk in en:
545
+ ... print (chunk)
546
+ [1 , 3 , 9 , 27 ]
547
+ [81 , 243 , 729 , 2187 ]
548
+ [6561 , 19683 , 59049 , 177147 ]
549
+
550
+ ----
551
+
499
552
instancemethod ``concat(second) ``
500
553
-----------------------------------
501
554
@@ -641,6 +694,25 @@ Example
641
694
642
695
----
643
696
697
+ instancemethod ``distinct_by(key_selector) ``
698
+ ----------------------------------------------
699
+
700
+ Parameters
701
+ - `key_selector ` (``Callable[[TSource_co], object] ``)
702
+
703
+ Returns
704
+ - ``Enumerable[TSource_co] ``
705
+
706
+ Returns distinct elements from the sequence where "distinctness" is determined by the value
707
+ returned by the selector.
708
+
709
+ Example
710
+ >>> ints = [1 , 4 , 5 , 6 , 4 , 3 , 1 , 99 ]
711
+ >>> Enumerable(ints).distinct_by(lambda x : x // 2 ).to_list()
712
+ [1, 4, 6, 3, 99]
713
+
714
+ ----
715
+
644
716
instancemethod ``element_at(index) ``
645
717
--------------------------------------
646
718
@@ -653,6 +725,8 @@ Returns
653
725
Returns the element at specified index in the sequence. `IndexOutOfRangeError ` is raised if
654
726
no such element exists.
655
727
728
+ If the index is negative, it means counting from the end.
729
+
656
730
This method always uses a generic list element-finding method (O(n)) regardless the
657
731
implementation of the wrapped iterable.
658
732
@@ -665,6 +739,9 @@ Example
665
739
>> > Enumerable(gen()).element_at(1 )
666
740
10
667
741
742
+ >> > Enumerable(gen()).element_at(- 1 )
743
+ 100
744
+
668
745
----
669
746
670
747
instancemethod ``element_at[TDefault](index, __default) ``
@@ -680,6 +757,8 @@ Returns
680
757
Returns the element at specified index in the sequence. Default value is returned if no
681
758
such element exists.
682
759
760
+ If the index is negative, it means counting from the end.
761
+
683
762
This method always uses a generic list element-finding method (O(n)) regardless the
684
763
implementation of the wrapped iterable.
685
764
@@ -731,6 +810,27 @@ Example
731
810
732
811
----
733
812
813
+ instancemethod ``except_by[TKey](second, key_selector) ``
814
+ ----------------------------------------------------------
815
+
816
+ Parameters
817
+ - `second ` (``Iterable[TKey] ``)
818
+ - `key_selector ` (``Callable[[TSource_co], TKey] ``)
819
+
820
+ Returns
821
+ - ``Enumerable[TSource_co] ``
822
+
823
+ Produces the set difference of two sequences: self - second, according to a key selector that
824
+ determines "distinctness".
825
+
826
+ Example
827
+ >>> first = [(16 , ' x' ), (9 , ' y' ), (12 , ' d' ), (16 , ' t' )]
828
+ >>> second = [' y' , ' d' ]
829
+ >>> Enumerable(first).except_by(second, lambda x : x[1 ]).to_list()
830
+ [(16, 'x'), (16, 't')]
831
+
832
+ ----
833
+
734
834
instancemethod ``first() ``
735
835
----------------------------
736
836
@@ -1026,6 +1126,26 @@ Example
1026
1126
1027
1127
----
1028
1128
1129
+ instancemethod ``intersect_by[TKey](second, key_selector) ``
1130
+ -------------------------------------------------------------
1131
+
1132
+ Parameters
1133
+ - `second ` (``Iterable[TKey] ``)
1134
+ - `key_selector ` (``Callable[[TSource_co], TKey] ``)
1135
+
1136
+ Returns
1137
+ - ``Enumerable[TSource_co] ``
1138
+
1139
+ Produces the set intersection of two sequences: self * second according to a
1140
+ specified key selector.
1141
+
1142
+ Example
1143
+ >>> strs = [' +1' , ' -3' , ' +5' , ' -7' , ' +9' , ' -11' ]
1144
+ >>> Enumerable(strs).intersect_by([1 , 2 , 3 , 5 , 9 ], lambda x : abs (int (x))).to_list()
1145
+ ['+1', '-3', '+5', '+9']
1146
+
1147
+ ----
1148
+
1029
1149
instancemethod ``join[TInner, TKey, TResult](inner, outer_key_selector, inner_key_selector, result_selector) ``
1030
1150
----------------------------------------------------------------------------------------------------------------
1031
1151
@@ -1237,6 +1357,43 @@ Example
1237
1357
1238
1358
----
1239
1359
1360
+ instancemethod ``max_by[TSupportsLessThan](key_selector) ``
1361
+ ------------------------------------------------------------
1362
+
1363
+ Parameters
1364
+ - `key_selector ` (``Callable[[TSource_co], TSupportsLessThan] ``)
1365
+
1366
+ Returns
1367
+ - ``TSource_co ``
1368
+
1369
+ Returns the maximal element of the sequence based on the given key selector. Raises
1370
+ `InvalidOperationError ` if there is no value.
1371
+
1372
+ Example
1373
+ >>> strs = [' aaa' , ' bb' , ' c' , ' dddd' ]
1374
+ >>> Enumerable(strs).max_by(len )
1375
+ 'dddd'
1376
+
1377
+ ----
1378
+
1379
+ instancemethod ``max_by[TKey](key_selector, __comparer) ``
1380
+ -----------------------------------------------------------
1381
+
1382
+ Parameters
1383
+ - `key_selector ` (``Callable[[TSource_co], TKey] ``)
1384
+ - `__comparer ` (``Callable[[TKey, TKey], int] ``)
1385
+
1386
+ Returns
1387
+ - ``TSource_co ``
1388
+
1389
+ Returns the maximal element of the sequence based on the given key selector and the comparer.
1390
+ Raises `InvalidOperationError ` if there is no value.
1391
+
1392
+ Such comparer takes two values and return positive ints when lhs > rhs, negative ints
1393
+ if lhs < rhs, and 0 if they are equal.
1394
+
1395
+ ----
1396
+
1240
1397
instancemethod ``min[TSupportsLessThan]() ``
1241
1398
---------------------------------------------
1242
1399
@@ -1294,6 +1451,38 @@ resulting values. Returns the default one if there is no value.
1294
1451
1295
1452
----
1296
1453
1454
+ instancemethod ``min_by[TSupportsLessThan](key_selector) ``
1455
+ ------------------------------------------------------------
1456
+
1457
+ Parameters
1458
+ - `key_selector ` (``Callable[[TSource_co], TSupportsLessThan] ``)
1459
+
1460
+ Returns
1461
+ - ``TSource_co ``
1462
+
1463
+ Returns the minimal element of the sequence based on the given key selector. Raises
1464
+ `InvalidOperationError ` if there is no value.
1465
+
1466
+ ----
1467
+
1468
+ instancemethod ``min_by[TKey](key_selector, __comparer) ``
1469
+ -----------------------------------------------------------
1470
+
1471
+ Parameters
1472
+ - `key_selector ` (``Callable[[TSource_co], TKey] ``)
1473
+ - `__comparer ` (``Callable[[TKey, TKey], int] ``)
1474
+
1475
+ Returns
1476
+ - ``TSource_co ``
1477
+
1478
+ Returns the minimal element of the sequence based on the given key selector and the comparer.
1479
+ Raises `InvalidOperationError ` if there is no value.
1480
+
1481
+ Such comparer takes two values and return positive ints when lhs > rhs, negative ints
1482
+ if lhs < rhs, and 0 if they are equal.
1483
+
1484
+ ----
1485
+
1297
1486
instancemethod ``of_type[TResult](t_result) ``
1298
1487
-----------------------------------------------
1299
1488
@@ -1926,6 +2115,30 @@ Example
1926
2115
1927
2116
----
1928
2117
2118
+ instancemethod ``take(__index) ``
2119
+ ----------------------------------
2120
+
2121
+ Parameters
2122
+ - `__index ` (``slice ``)
2123
+
2124
+ Returns
2125
+ - ``Enumerable[TSource_co] ``
2126
+
2127
+ Produces a subsequence defined by the given slice notation.
2128
+
2129
+ This method currently is identical to `elements_in() ` when it takes a slice.
2130
+
2131
+ Example
2132
+ .. code-block :: python
2133
+
2134
+ >> > def gen ():
2135
+ ... yield 1 ; yield 10 ; yield 100 ; yield 1000 ; yield 10000
2136
+
2137
+ >> > Enumerable(gen()).take(slice (1 , 3 )).to_list()
2138
+ [10 , 100 ]
2139
+
2140
+ ----
2141
+
1929
2142
instancemethod ``take_last(count) ``
1930
2143
-------------------------------------
1931
2144
@@ -2089,6 +2302,26 @@ Example
2089
2302
2090
2303
----
2091
2304
2305
+ instancemethod ``union_by(second, key_selector) ``
2306
+ ---------------------------------------------------
2307
+
2308
+ Parameters
2309
+ - `second ` (``Iterable[TSource_co] ``)
2310
+ - `key_selector ` (``Callable[[TSource_co], object] ``)
2311
+
2312
+ Returns
2313
+ - ``Enumerable[TSource_co] ``
2314
+
2315
+ Produces the set union of two sequences: self + second according to a specified key
2316
+ selector.
2317
+
2318
+ Example
2319
+ >>> en = Enumerable([1 , 9 , - 2 , - 7 , 14 ])
2320
+ >>> en.union_by([15 , 2 , - 26 , - 7 ], abs ).to_list()
2321
+ [1, 9, -2, -7, 14, 15, -26] # abs(-2) == abs(2)
2322
+
2323
+ ----
2324
+
2092
2325
instancemethod ``where(predicate) ``
2093
2326
-------------------------------------
2094
2327
@@ -2306,6 +2539,9 @@ Returns
2306
2539
2307
2540
Produces a subsequence defined by the given slice notation.
2308
2541
2542
+ This method always uses a generic list slicing method regardless the implementation of the
2543
+ wrapped iterable.
2544
+
2309
2545
Example
2310
2546
.. code-block :: python
2311
2547
@@ -2330,6 +2566,8 @@ Returns
2330
2566
2331
2567
Produces a subsequence with indices that define a slice.
2332
2568
2569
+ This method always uses a generic list slicing method regardless the implementation of the
2570
+ wrapped iterable.
2333
2571
2334
2572
Example
2335
2573
.. code-block :: python
0 commit comments