Last active
November 23, 2017 20:58
-
-
Save ryukinix/1e99bac3d49f81f006c620d102675e16 to your computer and use it in GitHub Desktop.
A hacky implementation of pipelines in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright © 2017 Manoel Vilela | |
# | |
# @project: Pipelines in Python | |
# @author: Manoel Vilela | |
# @email: manoel_vilela@engineer.com | |
# | |
from functools import partial | |
class Infix(object): | |
def __init__(self, func): | |
self.func = func | |
def __or__(self, other): | |
return self.func(other) | |
def __ror__(self, other): | |
return Infix(partial(self.func, other)) | |
def __call__(self, v1, v2): | |
return self.func(v1, v2) | |
@Infix | |
def pipe(x, f): | |
return f(x) | |
def example(): | |
range(1, 10) |pipe| sum |pipe| (lambda x: x + 10) |pipe| print # 55 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment