Skip to content

Conversation

@JavierSegui
Copy link
Contributor

What

Add some operators implemented in kotlin

else func)


def Not(value: bool):
Copy link
Collaborator

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()):
Copy link
Collaborator

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

Copy link
Collaborator

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))
Copy link
Collaborator

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):
Copy link
Collaborator

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))
Copy link
Collaborator

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)) == []
Copy link
Collaborator

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)) != []
Copy link
Collaborator

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 = []
Copy link
Collaborator

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 = []
Copy link
Collaborator

@biellls biellls Jul 10, 2019

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:
Copy link
Collaborator

@biellls biellls Jul 10, 2019

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)?

Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants