pycategory


Namepycategory JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/usabarashi/pycategory
SummaryPython implementation of the Scala standard library.
upload_time2023-07-03 17:29:43
maintainer
docs_urlNone
authorusabarashi
requires_python>=3.10,<4.0
licenseMIT
keywords functional scala
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # category

# Installation

pip install works for this library.

```console
pip install pycategory
```

# Usage

Here is an example of how to use the `category` package:

## curry

```python
from category import curry

@curry
def function(arg1: int, /, arg2: int, arg3: int = 3, *, arg4: int = 4) -> int:
    return arg1 + arg2 + arg3 + arg4

function2 = function        # (int) -> ((int) -> int)
function1 = function2(1)    # (int) -> int
result = function1(2)       # int
```

## Pipeline

```python
from category import Pipeline, curry

def squared(value: int) -> int:
    return value**2

assert 42**2**2 == ~(Pipeline(42) >> squared >> squared)

@Pipeline
@curry
def cubed(arg1: int, arg2: int, arg3: int) -> int:
    return arg1 * arg2 * arg3

assert 42**3 == ~(cubed << 42 << 42 << 42)
```

## Either

```python
from category import Either, EitherDo, Frame, Left, Right

class Error(Frame):
    ...

@MEither.do
def context(value: int) -> EitherDo[Error, int]:
    one = yield from Left[Error, int](Error(unmask=("value",)))
    two = 2
    three = yield from Right[Error, int](3)
    return one + two + three

match context(42).pattern:
    case Left(value):
        print(f"Left case {value}")
    case Right(value):
        print(f"Right case {value}")
```

## Option

```python
from category import VOID, Option, OptionDo, Some, Void

@Option.do
def context() -> OptionDo[int]:
    one = yield from VOID
    two = 2
    three = yield from Some[int](3)
    return one + two + three

match context().pattern:
    case Void():
        print("Void case")
    case Some(value):
        print(f"Some case {value}")
```

## Try

```python
from category import Failure, Success, Try, TryDo

@Try.hold(unmask=("value",))
def hold_context(value: int, /) -> int:
    if not value:
        raise Exception("error")
    return value

@Try.do
def context() -> TryDo[int]:
    one = yield from hold_context(0)
    two = 2
    three = yield from Success[int](3)
    return one + two + three

match context().pattern:
    case Failure() as failure:
        print(f"Failure case {failure.exception}")
    case Success(value):
        print(f"Success case {value}")

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/usabarashi/pycategory",
    "name": "pycategory",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0",
    "maintainer_email": "",
    "keywords": "Functional,Scala",
    "author": "usabarashi",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/f1/fc/f8a91fbfe1d83a6e339bec194f2ff0294db74d9ed3004a4aec25b63ee5c5/pycategory-0.1.1.tar.gz",
    "platform": null,
    "description": "# category\n\n# Installation\n\npip install works for this library.\n\n```console\npip install pycategory\n```\n\n# Usage\n\nHere is an example of how to use the `category` package:\n\n## curry\n\n```python\nfrom category import curry\n\n@curry\ndef function(arg1: int, /, arg2: int, arg3: int = 3, *, arg4: int = 4) -> int:\n    return arg1 + arg2 + arg3 + arg4\n\nfunction2 = function        # (int) -> ((int) -> int)\nfunction1 = function2(1)    # (int) -> int\nresult = function1(2)       # int\n```\n\n## Pipeline\n\n```python\nfrom category import Pipeline, curry\n\ndef squared(value: int) -> int:\n    return value**2\n\nassert 42**2**2 == ~(Pipeline(42) >> squared >> squared)\n\n@Pipeline\n@curry\ndef cubed(arg1: int, arg2: int, arg3: int) -> int:\n    return arg1 * arg2 * arg3\n\nassert 42**3 == ~(cubed << 42 << 42 << 42)\n```\n\n## Either\n\n```python\nfrom category import Either, EitherDo, Frame, Left, Right\n\nclass Error(Frame):\n    ...\n\n@MEither.do\ndef context(value: int) -> EitherDo[Error, int]:\n    one = yield from Left[Error, int](Error(unmask=(\"value\",)))\n    two = 2\n    three = yield from Right[Error, int](3)\n    return one + two + three\n\nmatch context(42).pattern:\n    case Left(value):\n        print(f\"Left case {value}\")\n    case Right(value):\n        print(f\"Right case {value}\")\n```\n\n## Option\n\n```python\nfrom category import VOID, Option, OptionDo, Some, Void\n\n@Option.do\ndef context() -> OptionDo[int]:\n    one = yield from VOID\n    two = 2\n    three = yield from Some[int](3)\n    return one + two + three\n\nmatch context().pattern:\n    case Void():\n        print(\"Void case\")\n    case Some(value):\n        print(f\"Some case {value}\")\n```\n\n## Try\n\n```python\nfrom category import Failure, Success, Try, TryDo\n\n@Try.hold(unmask=(\"value\",))\ndef hold_context(value: int, /) -> int:\n    if not value:\n        raise Exception(\"error\")\n    return value\n\n@Try.do\ndef context() -> TryDo[int]:\n    one = yield from hold_context(0)\n    two = 2\n    three = yield from Success[int](3)\n    return one + two + three\n\nmatch context().pattern:\n    case Failure() as failure:\n        print(f\"Failure case {failure.exception}\")\n    case Success(value):\n        print(f\"Success case {value}\")\n\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Python implementation of the Scala standard library.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/usabarashi/pycategory",
        "Repository": "https://github.com/usabarashi/pycategory"
    },
    "split_keywords": [
        "functional",
        "scala"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a96ddf51f32510d7f749147932081b5071ee95b37e36ded8151fde2f3996bb86",
                "md5": "c9f3e8672d65117a882d2423ac16cc08",
                "sha256": "7ec01ace1cc56097986d6cbb39042bcd2f51defe603077ea3929df3ed09cb5b6"
            },
            "downloads": -1,
            "filename": "pycategory-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c9f3e8672d65117a882d2423ac16cc08",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0",
            "size": 18493,
            "upload_time": "2023-07-03T17:29:41",
            "upload_time_iso_8601": "2023-07-03T17:29:41.638790Z",
            "url": "https://files.pythonhosted.org/packages/a9/6d/df51f32510d7f749147932081b5071ee95b37e36ded8151fde2f3996bb86/pycategory-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f1fcf8a91fbfe1d83a6e339bec194f2ff0294db74d9ed3004a4aec25b63ee5c5",
                "md5": "08653821649a48d0fa1697a6fe547595",
                "sha256": "b8a519b62e49c166130d03af3128edc67f2c1454caddfeaa7791dadd7499f1d1"
            },
            "downloads": -1,
            "filename": "pycategory-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "08653821649a48d0fa1697a6fe547595",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0",
            "size": 14869,
            "upload_time": "2023-07-03T17:29:43",
            "upload_time_iso_8601": "2023-07-03T17:29:43.922590Z",
            "url": "https://files.pythonhosted.org/packages/f1/fc/f8a91fbfe1d83a6e339bec194f2ff0294db74d9ed3004a4aec25b63ee5c5/pycategory-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-03 17:29:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "usabarashi",
    "github_project": "pycategory",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "pycategory"
}
        
Elapsed time: 0.09793s