@@ -230,6 +230,23 @@ def scan(self, scanner: Callable[[TState, TSource], TState], state: TState) -> I
230
230
"""
231
231
return Seq (itertools .accumulate (self , scanner , initial = state )) # type: ignore
232
232
233
+ def skip (self , count : int ) -> Seq [TSource ]:
234
+ """Returns a sequence that skips N elements of the underlying
235
+ sequence and then yields the remaining elements of the sequence.
236
+
237
+ Args:
238
+ count: The number of items to skip.
239
+ """
240
+ return Seq (pipe (self , skip (count )))
241
+
242
+ def take (self , count : int ) -> Seq [TSource ]:
243
+ """Returns the first N elements of the sequence.
244
+
245
+ Args:
246
+ count: The number of items to take.
247
+ """
248
+ return Seq (pipe (self , take (count )))
249
+
233
250
@classmethod
234
251
def unfold (cls , generator : Callable [[TState ], Option [Tuple [TSource , TState ]]], state : TState ) -> Iterable [TSource ]:
235
252
"""Returns a list that contains the elements generated by the
@@ -677,6 +694,20 @@ def singleton(item: TSource) -> Seq[TSource]:
677
694
return Seq ([item ])
678
695
679
696
697
+ def skip (count : int ) -> Projection [Any , Any ]:
698
+ """Returns a sequence that skips N elements of the underlying
699
+ sequence and then yields the remaining elements of the sequence.
700
+
701
+ Args:
702
+ count: The number of items to skip.
703
+ """
704
+
705
+ def _skip (source : Iterable [TSource ]) -> Iterable [TSource ]:
706
+ return (n for i , n in enumerate (source ) if i >= count )
707
+
708
+ return _skip
709
+
710
+
680
711
def take (count : int ) -> Projection [Any , Any ]:
681
712
"""Returns the first N elements of the sequence.
682
713
@@ -688,18 +719,7 @@ def take(count: int) -> Projection[Any, Any]:
688
719
"""
689
720
690
721
def _take (source : Iterable [TSource ]) -> Iterable [TSource ]:
691
- n = count
692
-
693
- def gen ():
694
- nonlocal n
695
- for x in source :
696
- if n > 0 :
697
- yield x
698
- n -= 1
699
- else :
700
- break
701
-
702
- return gen ()
722
+ return (n for i , n in enumerate (source ) if i < count )
703
723
704
724
return _take
705
725
@@ -794,6 +814,7 @@ def _zip(source2: Iterable[TResult]) -> Iterable[Tuple[TSource, TResult]]:
794
814
"of_iterable" ,
795
815
"range" ,
796
816
"scan" ,
817
+ "skip" ,
797
818
"singleton" ,
798
819
"take" ,
799
820
"Projection" ,
0 commit comments