11from __future__ import annotations
22
3- from dataclasses import dataclass
3+ from itertools import islice
44from typing import (
55 Any ,
66 Callable ,
1616 overload ,
1717)
1818
19- from pipedata .core .itertools import take_next , take_up_to_n
20-
2119TStart = TypeVar ("TStart" )
2220TEnd = TypeVar ("TEnd" )
2321TOther = TypeVar ("TOther" )
2422
2523
2624def _identity (input_iterator : Iterator [TEnd ]) -> Iterator [TEnd ]:
27- while (element := take_next (input_iterator )) is not None :
28- yield element
25+ yield from input_iterator
2926
3027
3128class CountingIterator (Iterator [TStart ]):
@@ -48,38 +45,31 @@ def get_count(self) -> int:
4845 return self ._count
4946
5047
51- class CountedFunc (Generic [TStart , TEnd ]):
48+ class ChainLink (Generic [TStart , TEnd ]):
5249 def __init__ (
5350 self ,
5451 func : Callable [[Iterator [TStart ]], Iterator [TEnd ]],
5552 ) -> None :
5653 self ._func = func
57- self ._counting_input : Optional [CountingIterator [TStart ]] = None
58- self ._counting_output : Optional [CountingIterator [TEnd ]] = None
54+ self ._input : Optional [CountingIterator [TStart ]] = None
55+ self ._output : Optional [CountingIterator [TEnd ]] = None
5956
6057 @property
6158 def __name__ (self ) -> str : # noqa: A003
6259 return self ._func .__name__
6360
6461 def __call__ (self , input_iterator : Iterator [TStart ]) -> Iterator [TEnd ]:
65- self ._counting_input = CountingIterator (input_iterator )
66- self ._counting_output = CountingIterator (self ._func (self ._counting_input ))
67- return self ._counting_output
62+ self ._input = CountingIterator (input_iterator )
63+ self ._output = CountingIterator (self ._func (self ._input ))
64+ return self ._output
6865
6966 def get_counts (self ) -> Tuple [int , int ]:
7067 return (
71- 0 if self ._counting_input is None else self ._counting_input .get_count (),
72- 0 if self ._counting_output is None else self ._counting_output .get_count (),
68+ 0 if self ._input is None else self ._input .get_count (),
69+ 0 if self ._output is None else self ._output .get_count (),
7370 )
7471
7572
76- @dataclass
77- class StepCount :
78- name : str
79- inputs : int
80- outputs : int
81-
82-
8373class Chain (Generic [TStart , TEnd ]):
8474 @overload
8575 def __init__ (
@@ -106,11 +96,11 @@ def __init__(
10696 ],
10797 ) -> None :
10898 self ._previous_steps = previous_steps
109- self ._func = CountedFunc (func )
99+ self ._func = ChainLink (func )
110100
111101 def __call__ (self , input_iterator : Iterator [TStart ]) -> Iterator [TEnd ]:
112102 if self ._previous_steps is None :
113- func = cast (CountedFunc [TStart , TEnd ], self ._func )
103+ func = cast (ChainLink [TStart , TEnd ], self ._func )
114104 return func (input_iterator )
115105
116106 return self ._func (self ._previous_steps (input_iterator )) # type: ignore
@@ -133,9 +123,7 @@ def filter(self, func: Callable[[TEnd], bool]) -> Chain[TStart, TEnd]: # noqa:
133123 """
134124
135125 def new_action (previous_step : Iterator [TEnd ]) -> Iterator [TEnd ]:
136- while (element := take_next (previous_step )) is not None :
137- if func (element ) is True :
138- yield element
126+ return filter (func , previous_step )
139127
140128 new_action .__name__ = func .__name__
141129 return self .flat_map (new_action )
@@ -148,8 +136,7 @@ def map( # noqa: A003
148136 """
149137
150138 def new_action (previous_step : Iterator [TEnd ]) -> Iterator [TOther ]:
151- while (element := take_next (previous_step )) is not None :
152- yield func (element )
139+ return map (func , previous_step )
153140
154141 new_action .__name__ = func .__name__
155142 return self .flat_map (new_action )
@@ -165,7 +152,7 @@ def map_tuple(
165152 """
166153
167154 def new_action (previous_step : Iterator [TEnd ]) -> Iterator [TOther ]:
168- while elements := take_up_to_n ( previous_step , n ):
155+ while elements := tuple ( islice ( previous_step , n ) ):
169156 yield func (elements )
170157
171158 new_action .__name__ = func .__name__
0 commit comments