Skip to content

Instantly share code, notes, and snippets.

@LeeeeT
LeeeeT / lambda.py
Last active February 7, 2024 22:52
Lambdas ain't too much
from functools import cache
from sys import setrecursionlimit
setrecursionlimit(10000)
(lambda: lambda define:
@LeeeeT
LeeeeT / t.py
Last active April 27, 2024 22:43
from collections.abc import Callable
from dataclasses import dataclass
def curry[First, *Rest, Result](
function: Callable[[First, *Rest], Result],
) -> Callable[[*Rest], Callable[[First], Result]]:
return lambda *rest: lambda first: function(first, *rest)
@LeeeeT
LeeeeT / .py
Last active February 1, 2024 02:26
I swear to God
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import cast, Final, NewType
class Compose[A, C]:
def __init__[B](self, f: Callable[[B], C], g: Callable[[A], B]) -> None:
self.f: Final = f
@LeeeeT
LeeeeT / .py
Last active February 3, 2024 17:17
I do not swear to God
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Final, NewType
class Compose[Value, Result]:
def __init__[Intermediate](self, second: Callable[[Intermediate], Result], first: Callable[[Value], Intermediate]) -> None:
self.second: Final = second
@LeeeeT
LeeeeT / order.py
Last active February 7, 2024 19:25
Order
from collections.abc import Callable
from dataclasses import dataclass
from typing import Concatenate
@dataclass(frozen=True)
class Partial1[First, **Rest, Result]:
function: Callable[Concatenate[First, Rest], Result]
first: First
@LeeeeT
LeeeeT / awaitable_option.py
Last active February 7, 2024 22:46
Awaitable-Option monad composition
from asyncio import run
from collections.abc import Coroutine
from dataclasses import dataclass
from typing import Any, Protocol
class Callable[*Arguments, Result](Protocol):
def __call__(self, *args: *Arguments) -> Result:
...
@LeeeeT
LeeeeT / lazy.py
Created February 8, 2024 01:26
Lazy monad
from collections.abc import Callable
from dataclasses import dataclass
from typing import Concatenate, Final
@dataclass(frozen=True)
class Partial1[First, **P, Result]:
function: Callable[Concatenate[First, P], Result]
first: First
@LeeeeT
LeeeeT / lazy.py
Created February 24, 2024 11:43
Laziness complete
from __future__ import annotations
from collections.abc import Callable, Iterator
from typing import Protocol, cast
type Lazy[Value] = Callable[[], Value]
def lazify[Value](value: Value) -> Lazy[Value]:
@LeeeeT
LeeeeT / io.py
Created February 25, 2024 12:34
Pure IO
import sys
from collections.abc import Callable, Iterator
from functools import cache
type Lazy[T] = Callable[[], T]
def run_lazy[T](lazy: Lazy[T]) -> T:
@LeeeeT
LeeeeT / io.py
Last active March 2, 2024 22:12
Pure IO (does work)
import sys
from collections.abc import Callable, Iterator
from functools import cache
type Lazy[T] = Callable[[], T]
type Stream[T] = Lazy[tuple[T, Stream[T]]]