12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonにパイプ演算子と関数合成を導入する(仮)

Last updated at Posted at 2016-09-24

前提

class F:
    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        return self.f(*args, **kwargs)

    def __mul__(self, other):
        def _f(*args, **kwargs):
            return self.f(other(*args, **kwargs))
        return _f

    def __ror__(self, other):
        return self.f(other)


@F
def f(x):
    return x + 1

@F
def g(x):
    return x * x

帰結

>>> 1 | f
2

>>> 2 | g
4

>>> 1 | f | g
4

>>> (f * g)(1)
2

>>> (g * f)(1)
4
12
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?