Skip to content

Commit

Permalink
fix(immutable list): len of empty list
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Apr 19, 2020
1 parent fe78066 commit 777af5f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
29 changes: 22 additions & 7 deletions pymonet/immutable_list.py
Expand Up @@ -44,6 +44,15 @@ def __add__(self, other: 'ImmutableList[T]') -> 'ImmutableList[T]':
self.tail.__add__(other)
)

def __len__(self):
if self.head is None:
return 0

if self.tail is None:
return 1

return len(self.tail) + 1

@classmethod
def of(cls, head: T, *elements) -> 'ImmutableList[T]':
if len(elements) == 0:
Expand All @@ -58,12 +67,6 @@ def of(cls, head: T, *elements) -> 'ImmutableList[T]':
def empty(cls):
return ImmutableList(is_empty=True)

@property
def length(self):
if self.tail is None:
return 1

return self.tail.length + 1

def to_list(self):
if self.tail is None:
Expand Down Expand Up @@ -128,7 +131,7 @@ def filter(self, fn: Callable[[Optional[T]], bool]) -> 'ImmutableList[T]':

def find(self, fn: Callable[[Optional[T]], bool]) -> Optional[T]:
"""
Returns new first element of ImmutableList that passed
Returns first element of ImmutableList that passed
info argument returns True
:param fn: function to call with ImmutableList value
Expand All @@ -142,3 +145,15 @@ def find(self, fn: Callable[[Optional[T]], bool]) -> Optional[T]:
return self.head

return self.tail.find(fn)

def reduce(self, fn: Callable[[U, T], U]) -> U:
"""
Method executes a reducer function
on each element of the array, resulting in a single output value.
:param fn: function to call with ImmutableList value
:type fn: Function(A, B) -> A
:returns: A
"""

return self.tail.find(fn)
5 changes: 3 additions & 2 deletions tests/test_immutable_list.py
Expand Up @@ -4,8 +4,9 @@


def test_eq():
assert ImmutableList(1).length == 1
assert ImmutableList(1).unshift(0).length == 2
assert len(ImmutableList()) == 0
assert len(ImmutableList(1)) == 1
assert len(ImmutableList(1).unshift(0)) == 2


def test_immutable():
Expand Down

0 comments on commit 777af5f

Please sign in to comment.