Skip to content

Comments

Kalsina Yana Curry task#5

Open
yaprogrammer18-yanchi wants to merge 5 commits intomainfrom
branch_for_curry_task
Open

Kalsina Yana Curry task#5
yaprogrammer18-yanchi wants to merge 5 commits intomainfrom
branch_for_curry_task

Conversation

@yaprogrammer18-yanchi
Copy link
Owner

Completed curry task and created tests for it. Created two functions: curry and uncurry. Raised some exceptions connected with arity and arguments. Tested all the exceptions raises, also tested how my functions work with other different functions. I had the idea to create one universal test, that has different quantity of arguments every time and puts them into for e.g. in summ function... and in the end checks the result... But I had some problems with arity, 'cause i couldn't check the number of argumets that summ function requires.. Could you tell me please, whether it is possible to create such a universal test?
P.S. I hope you'll accept the way I tryed to solve the curry task, because I think that my version of solving it is not the most beautiful and correct.. But I tryed:)

Comment on lines +7 to +8
if arity <= 0:
raise MyError("Арность не может быть меньше или равна 0.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А чем вам не угодила нулевая арность?

Comment on lines +13 to +15
for el in (a, b, c):
func = func(el)
assert func == a + b + c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему не просто func(a)(b)(c)?

Comment on lines +33 to +42
try:

def summ(a, b, c):
return a + b + c

curry(summ, -1)
assert False
except MyError as e:
assert isinstance(e, MyError)
assert str(e) == "Арность не может быть меньше или равна 0."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для таких вещей есть pytest.raises: https://docs.pytest.org/en/7.1.x/how-to/assert.html#assertraises

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я не до конца уловил вашу идею про универсальный тест. Вы могли бы сделать функцию, которая на случайных входах проверяет, что каррированная функция отрабатывает так же, как и изначальная. Проблема тут в типах, потому что генерировать случайные числа просто, а вот что-то более сложное -- надо постараться

import random

def fully_apply(f, args):
    res = f
    for arg in args:
        res = res(arg)
    return res

def generate_int_input(func):
    [random.randint(1, 10**6) for _ in range(func.__code__.co_argcount)]

def curried_equiv(func):
    func_c = curry(func, func.__code__.co_argcount)
    input = generate_int_input(func)
    assert fully_apply(func_c, input) == func(*input)

def test_summ():
    def summ(a, b, c):
        return a + b + c

    curried_equiv(summ)

Но функции должны работать только с int-ми (возвращать могут, впрочем, любой тип, главное, чтобы можно было два элемента сравнивать), и придумывать их вы должны тоже сами

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.

2 participants