-
Notifications
You must be signed in to change notification settings - Fork 0
Functions exceptions #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| name: Python tests | ||
|
|
||
| on: | ||
| push: | ||
| branches: ["*"] | ||
| pull_request: | ||
| branches: ["*"] | ||
|
|
||
| jobs: | ||
| test: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v3 | ||
|
|
||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
| python-version: '3.10' # или нужная версия | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install pytest | ||
| # pip install -r requirements.txt # если есть зависимости | ||
|
|
||
| - name: Run tests | ||
| run: | | ||
| pytest -v |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [tool.pytest.ini_options] | ||
| testpaths = "tests" | ||
| pythonpath = "src" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| def curry(func, n): | ||
| if n < 0: | ||
| raise Exception("Negative n-ary.") | ||
| try: | ||
| func(*range(n)) | ||
| except TypeError: | ||
| raise Exception("Not right n-ary.") | ||
|
|
||
| if n in (0, 1): | ||
| return func | ||
|
|
||
| def curried_func(x): | ||
| def new_func(*args): | ||
| return func(x, *args) | ||
|
|
||
| return curry(new_func, n - 1) | ||
|
|
||
| return curried_func | ||
|
|
||
|
|
||
| def uncurry(curried_func, n): | ||
| if n < 0: | ||
| raise Exception("Negative n-ary.") | ||
|
|
||
| if n == 0: | ||
| return curried_func | ||
|
|
||
| def uncurried_func(*args): | ||
| new_func = curried_func(args[0]) | ||
| for x in range(1, n): | ||
| new_func = new_func(args[x]) | ||
|
|
||
| return new_func | ||
|
|
||
| try: | ||
| uncurried_func(*range(n)) | ||
| except TypeError: | ||
| raise Exception("Not right n-ary.") | ||
|
|
||
| return uncurried_func | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from functions_exceptions.currying.currying import curry, uncurry | ||
|
|
||
|
|
||
| def test_base(): | ||
| def func(x, y, z): | ||
| return x + y + z | ||
|
|
||
| curry_func = curry(func, 3) | ||
| uncurry_func = uncurry(curry_func, 3) | ||
| assert func(10, 20, 30) == curry_func(10)(20)(30) == uncurry_func(10, 20, 30) | ||
|
|
||
|
|
||
| def test_unknown_argument_count(): | ||
| def func(*args): | ||
| return sum(args) | ||
|
|
||
| curry_func = curry(func, 5) | ||
| uncurry_func = uncurry(curry_func, 5) | ||
| assert ( | ||
| func(1, 2, 3, 4, 5) == curry_func(1)(2)(3)(4)(5) == uncurry_func(1, 2, 3, 4, 5) | ||
| ) | ||
|
|
||
|
|
||
| def test_no_arguments(): | ||
| def func(): | ||
| return 1 | ||
|
|
||
| curry_func = curry(func, 0) | ||
| uncurry_func = uncurry(curry_func, 0) | ||
| assert func() == curry_func() == uncurry_func() | ||
|
|
||
|
|
||
| def test_one_argument(): | ||
| def func(x): | ||
| return x | ||
|
|
||
| curry_func = curry(func, 1) | ||
| uncurry_func = uncurry(curry_func, 1) | ||
| assert func(1) == curry_func(1) == uncurry_func(1) | ||
|
|
||
|
|
||
| def test_curry_negative_n_ary(): | ||
| def func(x, y, z): | ||
| return x + y + z | ||
|
|
||
| try: | ||
| curry(func, -1) | ||
| except Exception as e: | ||
| assert str(e) == "Negative n-ary." | ||
|
|
||
|
|
||
| def test_uncurry_negative_n_ary(): | ||
| def func(x, y, z): | ||
| return x + y + z | ||
|
|
||
| try: | ||
| curry_func = curry(func, 3) | ||
| uncurry(curry_func, -1) | ||
| except Exception as e: | ||
| assert str(e) == "Negative n-ary." | ||
|
|
||
|
|
||
| def test_curry_not_right_n_ary(): | ||
| def func(x, y, z): | ||
| return x + y + z | ||
|
|
||
| try: | ||
| curry(func, 4) | ||
| except Exception as e: | ||
| assert str(e) == "Not right n-ary." | ||
|
|
||
|
|
||
| def test_uncurry_not_right_n_ary(): | ||
| def func(x, y, z): | ||
| return x + y + z | ||
|
|
||
| try: | ||
| curry_func = curry(func, 3) | ||
| uncurry(curry_func, 4) | ||
| except Exception as e: | ||
| assert str(e) == "Not right n-ary." |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.