All Projects → python-effect → Effect

python-effect / Effect

effect isolation in Python, to facilitate more purely functional code

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Effect

Pfun
Functional, composable, asynchronous, type-safe Python.
Stars: ✭ 75 (-76.85%)
Mutual labels:  functional-programming, effects
Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+66.05%)
Mutual labels:  functional-programming, effects
Fx Ts
Computational environments and effects for TypeScript
Stars: ✭ 42 (-87.04%)
Mutual labels:  functional-programming, effects
Parapet
A purely functional library to build distributed and event-driven systems
Stars: ✭ 106 (-67.28%)
Mutual labels:  functional-programming, effects
Functionaljava
Functional programming in Java
Stars: ✭ 1,472 (+354.32%)
Mutual labels:  functional-programming, effects
Zio
ZIO — A type-safe, composable library for async and concurrent programming in Scala
Stars: ✭ 3,167 (+877.47%)
Mutual labels:  functional-programming, effects
Algebraic Effects
Manage side-effects in your javascript application cleanly with algebraic effects
Stars: ✭ 162 (-50%)
Mutual labels:  functional-programming, effects
Tofu
Functional programming toolbox
Stars: ✭ 281 (-13.27%)
Mutual labels:  functional-programming, effects
Android Cleanarchitecture Kotlin
This is a movies sample app in Kotlin, which is part of a serie of blog posts I have written about architecting android application using different approaches.
Stars: ✭ 3,646 (+1025.31%)
Mutual labels:  functional-programming
Kocircuit
Ko: A generic type-safe language for concurrent, stateful, deadlock-free systems and protocol manipulations
Stars: ✭ 305 (-5.86%)
Mutual labels:  functional-programming
Lambda
🔮 Estudos obscuros de programação funcional
Stars: ✭ 297 (-8.33%)
Mutual labels:  functional-programming
Aecor
Pure functional event sourcing runtime
Stars: ✭ 299 (-7.72%)
Mutual labels:  functional-programming
Coconut
Simple, elegant, Pythonic functional programming.
Stars: ✭ 3,422 (+956.17%)
Mutual labels:  functional-programming
Funcshell
Improve your shell by making it functional through Haskell! (An update to Awkward)
Stars: ✭ 297 (-8.33%)
Mutual labels:  functional-programming
Prelude Ts
Functional programming, immutable collections and FP constructs for typescript and javascript
Stars: ✭ 315 (-2.78%)
Mutual labels:  functional-programming
Hareactive
Purely functional reactive programming library
Stars: ✭ 293 (-9.57%)
Mutual labels:  functional-programming
Hamsters
A mini Scala utility library
Stars: ✭ 292 (-9.88%)
Mutual labels:  functional-programming
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (-4.01%)
Mutual labels:  functional-programming
Functional Fortran
Functional programming for modern Fortran
Stars: ✭ 311 (-4.01%)
Mutual labels:  functional-programming
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (-6.79%)
Mutual labels:  functional-programming

Effect

.. image:: https://travis-ci.org/python-effect/effect.svg?branch=master :target: https://travis-ci.org/python-effect/effect

.. image:: https://img.shields.io/pypi/v/effect.svg :target: https://pypi.python.org/pypi/effect

Effect is a library for helping you write purely functional code by isolating the effects (that is, IO or state manipulation) in your code. Documentation is available at https://effect.readthedocs.org/, and its PyPI page is https://pypi.python.org/pypi/effect.

It supports_ 3.6 and above.

.. _supports: https://travis-ci.org/python-effect/effect

You can install it by running pip install effect.

.. image:: https://radix.github.io/effect/sigh-defects.png :target: https://twitter.com/extempore2/status/553597279463305218

What Is It?

Effect lets you isolate your IO and state-manipulation code.

The benefits of this are many: first, the majority of your code can become purely functional, leading to easier testing and ability to reason about behavior. Also, because it separates the specification of an effect from the performance of the effect, there are two more benefits: testing becomes easier still, and it's easy to provide alternative implementations of effects.

Effect is somewhat similar to "algebraic effects", as implemented in various typed functional programming languages. It also has similarities to Twisted's Deferred objects.

Example

A very quick example of using Effects:

.. code:: python

from effect import sync_perform, sync_performer, Effect, TypeDispatcher

class ReadLine(object):
    def __init__(self, prompt):
        self.prompt = prompt

def get_user_name():
    return Effect(ReadLine("Enter a candy> "))

@sync_performer
def perform_read_line(dispatcher, readline):
    return raw_input(readline.prompt)

def main():
    effect = get_user_name()
    effect = effect.on(
        success=lambda result: print("I like {} too!".format(result)),
        error=lambda e: print("sorry, there was an error. {}".format(e)))

    dispatcher = TypeDispatcher({ReadLine: perform_read_line})
    sync_perform(dispatcher, effect)

if __name__ == '__main__':
    main()

Effect takes what we call an intent, which is any object. The dispatcher argument to sync_perform must have a performer function for your intent.

This has a number of advantages. First, your unit tests for get_user_name become simpler. You don't need to mock out or parameterize the raw_input function - you just call get_user_name and assert that it returns a ReadLine object with the correct 'prompt' value.

Second, you can implement ReadLine in a number of different ways - it's possible to override the way an intent is performed to do whatever you want.

For more information on how to implement the actual effect-performing code, and other details, see the documentation. There is also a full example of interacting with the user and using an HTTP client to talk to the GitHub API in the effect-examples repository.

.. _documentation: https://effect.readthedocs.org/ .. _effect-examples: https://github.com/python-effect/effect-examples

Videos

Some talks have been given about Effect.

  • Side Effects are a Public API_ by Chris Armstrong at Strange Loop_ (2015-09-26)
  • Functionalish Programming in Python with Effect_ by Robert Collins at Kiwi PyCon_ (2015-09-05)

.. _Side Effects are a Public API: https://www.youtube.com/watch?v=D37dc9EoFus .. _Strange Loop: https://thestrangeloop.com/ .. _Functionalish Programming in Python with Effect: https://www.youtube.com/watch?v=fM5d_2BS6FY .. _Kiwi PyCon: https://nzpug.org/

Thanks

Thanks to Rackspace for allowing me to work on this project, and having an excellent open source employee contribution policy_

.. _open source employee contribution policy: https://www.rackspace.com/blog/rackspaces-policy-on-contributing-to-open-source/

Authors

Effect was originally written by Christopher Armstrong_, but now has contributions from the following people:

.. _Christopher Armstrong: https://github.com/radix

  • cyli_
  • lvh_
  • Manish Tomar_
  • Tom Prince_

.. _cyli: https://github.com/cyli .. _lvh: https://github.com/lvh .. _Manish Tomar: https://github.com/manishtomar .. _Tom Prince: https://github.com/tomprince

IRC

There is a #python-effect IRC channel on irc.freenode.net.

See Also

For integrating Effect with Twisted's Deferreds, see the txEffect package (pypi, github).

.. _pypi: https://warehouse.python.org/project/txeffect .. _github: https://github.com/python-effect/txeffect

Over the past few years, the ecosystem of libraries to help with functional programming in Python has exploded. Here are some libraries I recommend:

  • pyrsistent_ - persistent (optimized immutable) data structures in Python
  • toolz_ - a general library of pure FP functions
  • fn.py_ - a Scala-inspired set of tools, including a weird lambda syntax, option type, and monads

.. _pyrsistent: https://pypi.python.org/pypi/pyrsistent/ .. _toolz: https://pypi.python.org/pypi/toolz .. _fn.py: https://pypi.python.org/pypi/fn

License

Effect is licensed under the MIT license:

Copyright (C) 2014 Christopher Armstrong

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].