@@ -230,6 +230,23 @@ def scan(self, scanner: Callable[[TState, TSource], TState], state: TState) -> I
230230 """
231231 return Seq (itertools .accumulate (self , scanner , initial = state )) # type: ignore
232232
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+
233250 @classmethod
234251 def unfold (cls , generator : Callable [[TState ], Option [Tuple [TSource , TState ]]], state : TState ) -> Iterable [TSource ]:
235252 """Returns a list that contains the elements generated by the
@@ -677,6 +694,20 @@ def singleton(item: TSource) -> Seq[TSource]:
677694 return Seq ([item ])
678695
679696
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+
680711def take (count : int ) -> Projection [Any , Any ]:
681712 """Returns the first N elements of the sequence.
682713
@@ -688,18 +719,7 @@ def take(count: int) -> Projection[Any, Any]:
688719 """
689720
690721 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 )
703723
704724 return _take
705725
@@ -794,6 +814,7 @@ def _zip(source2: Iterable[TResult]) -> Iterable[Tuple[TSource, TResult]]:
794814 "of_iterable" ,
795815 "range" ,
796816 "scan" ,
817+ "skip" ,
797818 "singleton" ,
798819 "take" ,
799820 "Projection" ,
0 commit comments