Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def first(self, default=Empty()):
"""Return the first item of the iterable. Raise IndexError if empty or default if provided.
>>> flu.first([0, 1, 2, 3])
0
>>> flu.first([], default='some_default')
'some default'
when *default* is not provided and the iterable is empty, raise IndexError
"""
x = default
for x in self:
return x
if isinstance(x, Empty):
raise IndexError("Empty iterator")
return default
def last(self, default=Empty()):
"""Return the last item of the iterble. Raise IndexError if empty or default if provided.
>>> flu.last([0, 1, 2, 3])
3
>>> flu.last([], default='some_default')
'some default'
"""
x = default
for x in self:
pass
if isinstance(x, Empty):
raise IndexError("Empty iterator")
return x
def tail(self, n: int = 10, container_type: Type = list):
"""Return up to the last *n* elements from the iterable
>>> flu.tail(range(20))
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> flu.tail(range(15), n=2)
[18, 19]
"""
for val in self.window(n, fill_value=Empty()):
pass
return container_type([x for x in val if not isinstance(x, Empty)])
def first(self, default=Empty()):
"""Return the first item of the iterable. Raise IndexError if empty or default if provided.
>>> flu.first([0, 1, 2, 3])
0
>>> flu.first([], default='some_default')
'some default'
when *default* is not provided and the iterable is empty, raise IndexError
"""
x = default
for x in self:
return x
if isinstance(x, Empty):
raise IndexError("Empty iterator")
return default