This file contains hidden or 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
import ast | |
from inspect import getsource | |
from types import FunctionType, CodeType | |
from functools import wraps | |
from textwrap import dedent | |
from linecache import getline | |
def do(func): | |
""" |
This file contains hidden or 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
import attr | |
@attr.s | |
class Free: | |
"""X monad in the category of Y""" | |
@staticmethod | |
def pure(value): | |
"""A -> Free[_, A]""" |
This file contains hidden or 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
from inspect import signature | |
from functools import partial, wraps | |
from typing import Generic, TypeVar, TYPE_CHECKING, overload, Callable, cast | |
R = TypeVar("R") | |
A1 = TypeVar("A1") | |
A2 = TypeVar("A2") | |
A3 = TypeVar("A3") | |
A4 = TypeVar("A4") | |
A5 = TypeVar("A5") |
This file contains hidden or 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
from functools import reduce, partial | |
from typing import Callable, TypeVar, overload | |
A1 = TypeVar("A1") | |
A2 = TypeVar("A2") | |
A3 = TypeVar("A3") | |
A4 = TypeVar("A4") | |
A5 = TypeVar("A5") | |
Fn = Callable[[A1], A2] |
This file contains hidden or 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
class Reader: | |
def __init__(self, func): | |
self._func = func if callable(func) else lambda _: func | |
def run(self, env): | |
return self._func(env) | |
@staticmethod | |
def ask(): | |
return Reader(lambda env: env) |
This file contains hidden or 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
from functools import partial | |
from concurrent.futures import Future as _Future | |
class Future: | |
def __init__(self, future): | |
self._future = future |
This file contains hidden or 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
import inspect | |
class Monad: | |
def __init__(self, val): | |
self.val = val | |
def bind(self, func): | |
return func(self.val) |
This file contains hidden or 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
from typing import Type, TypeVar, Callable, TYPE_CHECKING | |
A = TypeVar("A") | |
B = TypeVar("B") | |
_CONVERSIONS = {} | |
def reg(from_: Type[A], to: Type[B], func: Callable[[A], B]) -> None: | |
_CONVERSIONS[(from_, to)] = func |
This file contains hidden or 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
from asyncio import get_event_loop, Future | |
class Batcher: | |
""" | |
Use class to wrap functionality that is best represented as a single batched operation in async. | |
For example exporting data. | |
""" | |
def __init__(self, func): | |
self._queue = [] |
This file contains hidden or 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
""" | |
Yet another placeholder underscore lambda replacement. | |
This is a proof of concept trying to tackle the difficult issue of detecting when the function is still being built, and when it is being used. | |
>>> list(map(_.upper(), "abc")) | |
>>> # ["A", "B", "C"] | |
""" | |
import operator | |
import sys | |
from dis import disco, opmap |