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 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: |
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
# 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] |
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 Gensym: | |
__slots__ = ["index"] | |
def __init__(self): | |
self.index = 0 | |
def __call__(self, prefix): | |
self.index += 1 | |
return prefix + str(self.index) | |
# a type is: |
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 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 |
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
{-# 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 |
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 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() |
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
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: |
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
{-# 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) |
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
{-# 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 |
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
#![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) | |
} |