Giter Club home page Giter Club logo

uniontypes's Introduction

uniontypes 0.1

build status

Union types For python.

These appear other languages like Haskell (Either) and Scala (Union or \/).

This extends to any arbirary set of types.

Examples

Creating a union type

>>> from uniontypes import Union
>>> u = Union[list, tuple, str]
>>> u
<class 'uniontypes.Union[list, tuple, str]'>

Creating boxed values

>>> u([1, 2, 3])
Union[list, tuple, str][list] [1, 2, 3]
>>> u((1, 2, 3))
Union[list, tuple, str][tuple] (1, 2, 3)
>>> u('123')
Union[list, tuple, str][str] '123'

Accessing the inner wrapper types

>>> u[list]
<class 'uniontypes.Union[list, tuple, str][list]'>
>>> u[tuple]
<class 'uniontypes.Union[list, tuple, str][tuple]'>
>>> u[str]
<class 'uniontypes.Union[list, tuple, str][str]'>

Class heirarchy

>>> isinstance(u([1, 2, 3]), u)
True
>>> isinstance(u([1, 2, 3]), u[list])
True
>>> isinstance(u([1, 2, 3]), (u[tuple], u[str]))
False

Option Types

>>> from uniontypes import Option
>>> oint = Option[int]
>>> oint
<class 'uniontypes.Option[int]'>
>>> oint(1)
Option[int] 1
>>> oint(None)
Nothing
>>> oint(None) is oint.nothing
True

uniontypes's People

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.