Skip to content

Commit

Permalink
+any and all
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Sep 5, 2019
1 parent e852e87 commit b95f2e4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
15 changes: 13 additions & 2 deletions lambdas/_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ._bool import TRUE, FALSE, OR
from ._bool import TRUE, FALSE, OR, NOT
from ._pair import CONS, CAR, CDR
from ._combinators import Y
from ._natural import DEC, INC, GTE, ISZERO, ZERO
Expand Down Expand Up @@ -64,10 +64,21 @@
(TRUE)
))
LENGTH = lambda l: REDUCE(lambda x: lambda n: INC(n))(l)(ZERO)

INDEX = Y(lambda f: lambda n: lambda l: (
ISZERO(n)
(lambda _: HEAD(l))
(lambda _: f(DEC(n))(TAIL(l)))
(TRUE)
))
ANY = Y(lambda f: lambda l: (
EMPTY(l)
(lambda _: FALSE)
(lambda _: HEAD(l)(TRUE)(f(TAIL(l))))
(TRUE)
))
ALL = Y(lambda f: lambda l: (
EMPTY(l)
(lambda _: TRUE)
(lambda _: NOT(HEAD(l))(FALSE)(f(TAIL(l))))
(TRUE)
))
34 changes: 32 additions & 2 deletions tests/test_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from lambdas import ZERO, ONE, TWO, THREE, FOUR
from lambdas import ADD, LTE
from lambdas import LIST, APPEND, PREPEND, REVERSE, LENGTH
from lambdas import ADD, LTE, FALSE, TRUE
from lambdas import LIST, APPEND, PREPEND, REVERSE, LENGTH, ANY, ALL
from lambdas import MAP, RANGE, REDUCE, FILTER, TAKE, DROP, INDEX
from lambdas import decode_number, decode_list

Expand Down Expand Up @@ -148,3 +148,33 @@ def test_index(given, number, expected):
result = INDEX(number)(lst)
if expected is not None:
assert result == expected


@pytest.mark.parametrize('given, expected', [
([], FALSE),
([FALSE, FALSE], FALSE),
([FALSE, TRUE], TRUE),
([TRUE, FALSE], TRUE),
([FALSE, TRUE, FALSE], TRUE),
])
def test_any(given, expected):
lst = LIST
for el in given:
lst = APPEND(lst)(el)
assert ANY(lst) is expected


@pytest.mark.parametrize('given, expected', [
([], TRUE),
([TRUE], TRUE),
([TRUE, TRUE], TRUE),
([FALSE, FALSE], FALSE),
([FALSE, TRUE], FALSE),
([TRUE, FALSE], FALSE),
([FALSE, TRUE, FALSE], FALSE),
])
def test_all(given, expected):
lst = LIST
for el in given:
lst = APPEND(lst)(el)
assert ALL(lst) is expected

0 comments on commit b95f2e4

Please sign in to comment.