Skip to content

Instantly share code, notes, and snippets.

@internetimagery
internetimagery / do_notation_ast.py
Last active August 27, 2021 21:48
Simple do notation for python (modifying the ast)
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):
"""
@internetimagery
internetimagery / freedom.py
Last active August 29, 2021 04:31
Free monad in python
import attr
@attr.s
class Free:
"""X monad in the category of Y"""
@staticmethod
def pure(value):
"""A -> Free[_, A]"""
@internetimagery
internetimagery / curried.py
Last active August 19, 2021 10:14
Simple typed curry implimentation
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")
@internetimagery
internetimagery / composition.py
Created August 17, 2021 20:12
Simple compose function (made 100 times before!)
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]
@internetimagery
internetimagery / reader_monad.py
Last active August 6, 2021 21:31
Rough implimentation of reader monad
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)
@internetimagery
internetimagery / future_monad.py
Last active August 3, 2021 09:57
Monadic future wrapper
from functools import partial
from concurrent.futures import Future as _Future
class Future:
def __init__(self, future):
self._future = future
@internetimagery
internetimagery / do.py
Created July 26, 2021 10:11
Rough do notation decorator
import inspect
class Monad:
def __init__(self, val):
self.val = val
def bind(self, func):
return func(self.val)
@internetimagery
internetimagery / conv.py
Last active July 11, 2021 08:08
Generic conversions, statically checked (inspired by rust From traits)
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
@internetimagery
internetimagery / batcher.py
Last active July 7, 2021 09:23
Simple inversion of control for async in python. Taking something that is better suited for batch processing than threading, and using a standard await interface.
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 = []
@internetimagery
internetimagery / underscore.py
Last active June 15, 2021 08:28
Experiment in "underscore" placeholder lambda generation. Detecting when to finalize and when to build.
"""
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