-
Notifications
You must be signed in to change notification settings - Fork 0
kotlin-operators #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
| else func) | ||
|
|
||
|
|
||
| def Not(value: bool): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for index, self_item in enumerate(self.items): is more idiomatic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)) != [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lo mismo que all
|
|
||
| # TODO: Fix non laziness | ||
| def distinct(self) -> 'Stream[T]': | ||
| unique_items = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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