GithubHelp home page GithubHelp logo

suimong / pyeither Goto Github PK

View Code? Open in Web Editor NEW

This project forked from segfaultax/pyeither

0.0 1.0 0.0 34 KB

An implementation of Data.Either from Haskell in Python

License: MIT License

Python 41.85% Jupyter Notebook 58.15%

pyeither's Introduction

pyeither

A functional Python library that exposes Data.Either from Haskell.

data Either e a = Left e | Right a

Example Usage

import os
import json
import either

def main():
    path = "~/myfile.json"
    
    # Lift a normal value into Either
    e_path = either.pure(path)

    # Ensure the path is valid, returning Right(path) if it is or Left("not a file") if it isn't
    e_valid = e_path.bind(either.predicate(os.path.isfile, "not a file"))

    # Load a file as json, returning Right(contents) if it works, or Left(exc) if it doesn't
    e_data = e_valid.bind(lambda p: either.attempt(json.load, p))

    # It's annoying to assign intermediate values to variables, so you can chain expressions
    # Equivalent to above:

    e_data2 = (either.pure(path)
        .bind(either.predicate(os.path.isfile, "not a file"))
        .bind(lambda p: either.attempt(json.load, p)))

    # Python lacks do-notation, so there's not a nice equivalent syntax

    # Build pipelines of actions using monadic composition

    ensure_path = either.predicate(os.path.isfile, "not a file")
    load_file = lambda p: either.attempt(json.load, p)

    process = either.kleisli(ensure_path, load_file)

    # Equivalent to above:
    e_data3 = either.pure(path).bind(process)

Getting pyeither

pip install pyeither

Motivation

I've created an IPython Notebook that describes the motivation for this project as an interactive tutorial building an analogous system from scratch. If you're interested in learning more about the why of this project, please check it out!

Motivation Notebook

Special Thanks

This library depends on the absolutely wonderful attrs library, without which there would have been far more ugly boilerplate.

License

Copyright 2018 Michael-Keith Bernard

See LICENSE.txt for the full license.

pyeither's People

Contributors

segfaultax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.