{"id":19715674,"url":"https://github.com/pyx/monad","last_synced_at":"2025-04-29T20:30:39.832Z","repository":{"id":18827459,"uuid":"22042715","full_name":"pyx/monad","owner":"pyx","description":"monad - a functional library","archived":false,"fork":false,"pushed_at":"2018-05-27T07:50:32.000Z","size":130,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T19:03:56.134Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyx.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-20T20:30:38.000Z","updated_at":"2022-08-25T01:58:12.000Z","dependencies_parsed_at":"2022-08-05T01:15:19.371Z","dependency_job_id":null,"html_url":"https://github.com/pyx/monad","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyx%2Fmonad","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyx%2Fmonad/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyx%2Fmonad/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyx%2Fmonad/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyx","download_url":"https://codeload.github.com/pyx/monad/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251578254,"owners_count":21612000,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T22:39:07.305Z","updated_at":"2025-04-29T20:30:39.473Z","avatar_url":"https://github.com/pyx.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"===================================\nmonad - a functional python package\n===================================\n\n.. note::\n  **This project is superseded by Hymn(https://github.com/pyx/hymn)**.\n  \n  Limited by Python's syntax, there is no way to have a clean implementation\n  of do notation, the closest thing is a ``do`` decorator on generator\n  functions using ``yield`` as ``\u003c-``, which feels like black magic.\n\n  That's why I stopped shoehorning this into Python, and did a complete\n  rewrite in Hy (https://github.com/hylang/hy) a few years ago.\n\n  Being a lisp, or as they say, *Homoiconic Python*, Hy has the most flexible\n  syntax (or lack thereof :smile:), with it, I finally can write do notations,\n  check this out (for added fun, a ``Lazy`` monad is being demonstrated here,\n  we can never have such clean way to write thunk in pure python):\n\n  .. code:: clojure\n\n    =\u003e (import [hymn.types.lazy [force]])\n    =\u003e (require [hymn.types.lazy [lazy]])\n    =\u003e ;; lazy computation implemented as monad\n    =\u003e ;; macro lazy creates deferred computation\n    =\u003e (setv a (lazy (print \"evaluate a\") 42))\n    =\u003e ;; the computation is deferred, notice the value is shown as '_'\n    =\u003e a\n    Lazy(_)\n    =\u003e ;; evaluate it\n    =\u003e (.evaluate a)\n    evaluate a\n    42\n    =\u003e ;; now the value is cached\n    =\u003e a\n    Lazy(42)\n    =\u003e ;; calling evaluate again will not trigger the computation\n    =\u003e (.evaluate a)\n    42\n    =\u003e (setv b (lazy (print \"evaluate b\") 21))\n    =\u003e b\n    Lazy(_)\n    =\u003e ;; force evaluate the computation, same as calling .evaluate on the monad\n    =\u003e (force b)\n    evaluate b\n    21\n    =\u003e ;; force on values other than lazy return the value unchanged\n    =\u003e (force 42)\n    42\n    =\u003e (require [hymn.macros [do-monad]])\n    =\u003e ;; do notation with lazy monad\n    =\u003e (setv c (do-monad [x (lazy (print \"get x\") 1) y (lazy (print \"get y\") 2)] (+ x y)))\n    =\u003e ;; the computation is deferred\n    =\u003e c\n    Lazy(_)\n    =\u003e ;; do it!\n    =\u003e (force c)\n    get x\n    get y\n    3\n    =\u003e ;; again\n    =\u003e (force c)\n    3\n\n  **So, if you are interested in this package, please try\n  Hymn(https://github.com/pyx/hymn) instead**.\n\n\n\nIntroduction\n============\n\n\nWhat?\n-----\n\nMonads in python, with some helpful functions.\n\n\nHow?\n----\n\n::\n\n  \u003e\u003e\u003e from monad.decorators import maybe\n  \u003e\u003e\u003e parse_int = maybe(int)\n  \u003e\u003e\u003e parse_int(42)\n  Just(42)\n  \u003e\u003e\u003e parse_int('42')\n  Just(42)\n  \u003e\u003e\u003e parse_int('42.2')\n  Nothing\n\n  \u003e\u003e\u003e parse_float = maybe(float)\n  \u003e\u003e\u003e parse_float('42.2')\n  Just(42.2)\n\n  \u003e\u003e\u003e from monad.actions import tryout\n  \u003e\u003e\u003e parse_number = tryout(parse_int, parse_float)\n  \u003e\u003e\u003e tokens = [2, '0', '4', 'eight', '10.0']\n  \u003e\u003e\u003e [parse_number(token) for token in tokens]\n  [Just(2), Just(0), Just(4), Nothing, Just(10.0)]\n\n  \u003e\u003e\u003e @maybe\n  ... def reciprocal(n):\n  ...     return 1. / n\n  \u003e\u003e\u003e reciprocal(2)\n  Just(0.5)\n  \u003e\u003e\u003e reciprocal(0)\n  Nothing\n\n  \u003e\u003e\u003e process = parse_number \u003e\u003e reciprocal\n  \u003e\u003e\u003e process('4')\n  Just(0.25)\n  \u003e\u003e\u003e process('0')\n  Nothing\n  \u003e\u003e\u003e [process(token) for token in tokens]\n  [Just(0.5), Nothing, Just(0.25), Nothing, Just(0.1)]\n  \u003e\u003e\u003e [parse_number(token) \u003e\u003e reciprocal for token in tokens]\n  [Just(0.5), Nothing, Just(0.25), Nothing, Just(0.1)]\n  \u003e\u003e\u003e [parse_number(token) \u003e\u003e reciprocal \u003e\u003e reciprocal for token in tokens]\n  [Just(2.0), Nothing, Just(4.0), Nothing, Just(10.0)]\n\n\nWhy?\n----\n\nWhy not.\n\n\nRequirements\n============\n\n- CPython \u003e= 2.7\n\n\nInstallation\n============\n\nInstall from PyPI::\n\n  pip install monad\n\nInstall from source, download source package, decompress, then ``cd`` into source directory, run::\n\n  make install\n\n\nLicense\n=======\n\nBSD New, see LICENSE for details.\n\n\nLinks\n=====\n\nDocumentation:\n  http://monad.readthedocs.org/\n\nIssue Tracker:\n  https://bitbucket.org/pyx/monad/issues/\n\nSource Package @ PyPI:\n  https://pypi.python.org/pypi/monad/\n\nMercurial Repository @ bitbucket:\n  https://bitbucket.org/pyx/monad/\n\nGit Repository @ Github:\n  https://github.com/pyx/monad/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyx%2Fmonad","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyx%2Fmonad","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyx%2Fmonad/lists"}