by sevensor · 8y ago · 0 comments
class Funk:
def __init__(self, f):
self._f = f
def __or__(self, other):
return Funk(lambda *_, **__: other(self(*_, **__)))
def __call__(self, *args, **kwargs):
return self._f(*args, **kwargs)
This lets you do fun stuff like: pipeline = Funk(h) | Funk(g) | Funk(f)
pipeline(x)
pipeline(y)
And that makes me happy that I participated in this discussion.I'm kind of surprised nobody's done this before. Unless they have. Any pointers to more fully fleshed out implementations of this idea?
As it turns out, implementing that different syntax in pyfunctional wouldn't be too hard or API breaking I think. Mainly it would require 1) wrapping/exporting functions to a module you could bring into scope (eg `from functional import functions as F` or `from functional.functions import *`), 2) Writing wrapper code to provide something functionally similar to `pipe`.
On libraries, I swear I saw something a while back, but my googlefu just now didn't help me find it.