Skip to content

Instantly share code, notes, and snippets.

@qexat
Created December 24, 2025 22:04
Show Gist options
  • Select an option

  • Save qexat/c7da40ed2f187751aec2e6346740529b to your computer and use it in GitHub Desktop.

Select an option

Save qexat/c7da40ed2f187751aec2e6346740529b to your computer and use it in GitHub Desktop.
functional programmers when they are forced to use python (they are crying and seething)
from __future__ import annotations
from collections.abc import Callable
type List[T] = tuple[()] | tuple[T, List[T]]
def concat_in_reverse[T](
concatenatee: List[T],
concatenated: List[T],
) -> List[T]:
match concatenated:
case ():
return concatenatee
case (first, rest):
return concat_in_reverse((first, concatenatee), rest)
def reverse[T](list: List[T]) -> List[T]:
return concat_in_reverse((), list)
def concat[T](left: List[T], right: List[T]) -> List[T]:
return concat_in_reverse(left, reverse(right))
def map[T, U](func: Callable[[T], U], list: List[T]) -> List[U]:
match list:
case ():
return ()
case (first, rest):
return (func(first), map(func, rest))
def fold[T, Acc](
func: Callable[[Acc, T], Acc],
init: Acc,
list: List[T],
) -> Acc:
match list:
case ():
return init
case (first, rest):
return fold(func, func(init, first), rest)
def join[T](list: List[List[T]]) -> List[T]:
return fold(concat, (), list)
def bind[T, U](list: List[T], func: Callable[[T], List[U]]) -> List[U]:
return join(map(func, list))
@ar1ja
Copy link

ar1ja commented Dec 24, 2025

please never cook again lil bro ๐Ÿ™

@tello2004
Copy link

aight time to hop out of nvim. but let's hop on some linkedin instead tho ๐Ÿ™๐Ÿผ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment