Skip to content

Instantly share code, notes, and snippets.

View Alwinfy's full-sized avatar

Alwinfy Alwinfy

  • United States
View GitHub Profile
@Alwinfy
Alwinfy / python_lisp.py
Last active October 17, 2023 15:50
a very quick lisp in python
class Env(dict):
__slots__ = ["parent"]
def __init__(self, l, parent, recur):
super().__init__(l)
self["recur"] = recur
self.parent = parent
def find(self, n):
return self[n] if n in self else self.parent.find(n)
class Lam:
@Alwinfy
Alwinfy / is_even_yandev.py
Created May 14, 2023 22:54
an implementation of the Yandev is_even() in Python
# is_even_yandev.py
def ast_info():
# extract an ast
root = compile("def foo(bar):\n if baz == 3: return", "", "exec", 0x400)
# destruct all interesting classes
fdef = root.body[0]
args = fdef.args
arg = args.args[0]
class Gensym:
__slots__ = ["index"]
def __init__(self):
self.index = 0
def __call__(self, prefix):
self.index += 1
return prefix + str(self.index)
# a type is:
import java.util.ArrayList;
import java.util.Optional;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class RawHooks {
public static record Pattern(String name) {
@Override
{-# LANGUAGE DeriveFunctor #-}
import Text.ParserCombinators.ReadP
type Name = String
data BinOp = Add | Sub | Mul | Div deriving Show
data Node a = Lit Bool Int | BinOp BinOp a a deriving (Show, Functor)
data Solver = FreeS | LitS Int | LeftS BinOp Int Solver | RightS BinOp Solver Int deriving Show
readLine :: ReadP (Name, Node Name)
readLine = do
from sys import stdin
def parse(s):
try:
expr = compile(s, "<given string>", "eval", flags=1024).body
except SyntaxError as ex:
raise ValueError("Bad string: " + ex.msg)
stack = [(expr, None)]
while True:
ex, info = stack.pop()
def parse(s):
try:
expr = compile(s, "<given string>", "eval", flags=1024).body
except SyntaxError as ex:
raise ValueError("Bad string: " + ex.msg)
val = None
stack = [(expr, None)]
while True:
ex, info = stack.pop()
if info:
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE TemplateHaskell #-}
import Control.Monad (when)
import Control.Monad.Writer
import Data.Functor.Classes (Show1, liftShowsPrec)
import Data.Functor.Compose
import Data.List (partition)
import Data.Semigroup (Min(..), Sum(..), getMin)
import Text.Show.Deriving (deriveShow1)
{-# LANGUAGE Rank2Types #-}
--data Tree a = Leaf a | Pair (Tree a) (Tree a)
newtype TreeF a = TreeF { unTreeF :: forall b. (a -> b) -> (b -> b -> b) -> b }
newtype PairF a b = PairF { unPairF :: forall c. (a -> b -> c) -> c }
newtype MaybeF a = MaybeF { unMaybeF :: forall b. b -> (a -> b) -> b }
pairF :: a -> b -> PairF a b
pairF x y = PairF $ \c -> c x y
pairFst :: PairF a b -> a
#![feature(array_chunks)]
use rand::Rng;
fn egcd(x: isize, y: isize) -> (isize, isize, isize) {
if y == 0 {
(x, 1, 0)
} else {
let (g, lv, rv) = egcd(y, x % y);
(g, rv, lv - (x / y) * rv)
}