Skip to content

Instantly share code, notes, and snippets.

View joedougherty's full-sized avatar

Joe Dougherty joedougherty

View GitHub Profile
def f(nth):
stack = [0, 1]
for _ in range(nth):
stack = [stack[1], stack[0]] # swap
stack.append(stack[0]) # over
stack[1] = stack[1] + stack[2] # +
stack.pop()
return stack[1]
( in this example I use "TOS" to mean "Top of Stack" )
( Here is a one-line word, `f`, that computes the nth fibonacci number. )
: f 0 1 rot 1 ?DO swap over + LOOP swap . ;
10 f . ( compute the 10th fibonacci number. )
( `.` pops the computed fibonacci number off the stack and prints it. )
( the stack is empty once again. )
@joedougherty
joedougherty / FunctionChain.py
Last active April 4, 2020 17:03
A small to class to chain together multiple single-argument functions.
class FunctionChain:
"""
Chains together two or more functions together
and returns final result when called.
functions:
* must be named
* must take *exactly one* required argument
* ought to have explict return statements
@joedougherty
joedougherty / autopool.py
Last active September 9, 2019 15:07
shell out...in parallel
from multiprocessing import Pool
import subprocess
import sys
def run(args):
cmd, echo = args
process = subprocess.Popen(
cmd,
@joedougherty
joedougherty / timeblocks.py
Created April 5, 2019 02:47
find free time where you can
from datetime import date, datetime, time
class Block:
"""
Base class to represent a block of time.
Instantiation can be like:
# Use datetime objects directly
from z3 import *
"""
Generate a 3x3 magic square.
Imagine it like so:
-------------
| a | b | c |
-------------
@joedougherty
joedougherty / forth_ideas.cr
Created February 27, 2018 22:08
forth ideas (in crystal)
class Stack
def initialize
@contents = [] of Int32
end
def pop
@contents.pop()
end
def push(value)
@joedougherty
joedougherty / goForth_intro.txt
Last active September 30, 2017 21:38
goForth in 10 minutes
TIILE: gForth Overiew
ORIG DATE: 20170930
LAST REV DATE: 20170930
AUTHOR: mj <modusjonens@gmail.com>
==========================================================
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
==========================================================
from copy import deepcopy
class ClausePair:
def __init__(self, c1_set, c2_set):
self.c1, self.c2 = frozenset(c1_set), frozenset(c2_set)
def __eq__(self, other):
if isinstance(other, self.__class__):
return ((self.c1 == other.c1 and self.c2 == other.c2) or
def find_matching_node(expr, matching_fn, parent_node=None, parent_rel=None):
if isinstance(expr, Expression) and matching_fn(expr):
return (expr, parent_node, parent_rel)
if isinstance(expr, Term):
return
left_match = find_matching_node(expr.left, parent_node=expr, parent_rel='left')
right_match = find_matching_node(expr.right, parent_node=expr, parent_rel='right')
if left_match: