Conversation
| else func) | ||
|
|
||
|
|
||
| def Not(value: bool): |
There was a problem hiding this comment.
this could be moved to commodities.py (which we can rename sugar.py)
| return next(islice(self.items, index, index + 1)) | ||
|
|
||
| def index_of(self, item: T) -> int: | ||
| for index, self_item in zip(self.items, count()): |
There was a problem hiding this comment.
for index, self_item in enumerate(self.items): is more idiomatic
There was a problem hiding this comment.
Also aren't items and count() in the wrong order in the zip?
| return self.get(index) | ||
|
|
||
| def get(self, index: int) -> T: | ||
| return next(islice(self.items, index, index + 1)) |
There was a problem hiding this comment.
TODO: Benchmark vs for loop
|
|
||
| def last_index_of(self, item: T) -> int: | ||
| last_index = -1 | ||
| for index, self_item in zip(count(), self.items): |
There was a problem hiding this comment.
zip can be changed to enumerate
| return last_index | ||
|
|
||
| def sub_stream(self, from_index_inclusive: int, to_index_exclusive: int) -> 'Stream[T]': | ||
| return Stream(islice(self, from_index_inclusive, to_index_exclusive)) |
There was a problem hiding this comment.
Nice, could we bind __getslice__ to this so we can do stream[4:7]?
| return len(self) - 1 | ||
|
|
||
| def all(self, condition: Filter) -> bool: | ||
| return list(filter(compose(expand(condition), Not), self.items)) == [] |
There was a problem hiding this comment.
Mola la implementación pero creo que no es muy performant, seguramente un for y salir cuando uno sea False es lo mejor. De todas formas python tiene un all() que igual puede ser rápido. Seguramente all(condition(x) for x in self.items)
| return list(filter(compose(expand(condition), Not), self.items)) == [] | ||
|
|
||
| def any(self, condition: Filter) -> bool: | ||
| return list(filter(expand(condition), self.items)) != [] |
|
|
||
| # TODO: Fix non laziness | ||
| def distinct(self) -> 'Stream[T]': | ||
| unique_items = [] |
There was a problem hiding this comment.
Podemos usar un set comprehension (yo también me acabo de enterar de que existen XD)
return Stream({x for x in self.items})
|
|
||
| # TODO: Fix non laziness | ||
| def distinct_by(self, selector: Transform) -> 'Stream[T]': | ||
| unique_items = [] |
There was a problem hiding this comment.
Lazy implementation (untested)
def distinct_by(self, selector: Transform) -> 'Stream[T]':
def _generate_distinct_by():
unique_items = Set()
for item in self.items:
key = expand(selector)(item)
if key not in unique_keys:
yield key
unique_items.add(key)
return _generate_distinct_by()
|
|
||
| def to_list(self) -> List[T]: | ||
| return list(self.items) | ||
| if self.with_cache: |
There was a problem hiding this comment.
Can we make self.items be a @property that returns either the list if with_cache=True and cache is not None and otherwise returns the generator (which is currently self.items, we can rename it to self._generator)?
There was a problem hiding this comment.
The nice thing about this is that if we do that then every method in this class automatically uses the cache if it's available instead of throwing an exception, without changing any code.
What
Add some operators implemented in kotlin