Skip to content

Instantly share code, notes, and snippets.

View joelburton's full-sized avatar

Joel Burton joelburton

View GitHub Profile
@joelburton
joelburton / markov.js
Created April 2, 2018 18:38
Simple textual markov chain generator
// Textual markov chain generator - joel@joelburton.com
const fs = require('fs');
const util = require('util');
const process = require('process');
const preadFile = util.promisify(fs.readFile);
@joelburton
joelburton / poly.py
Created September 25, 2018 22:08
polymorphism
# Imagine with have some classes for animals:
class Animal:
"""Base class for all animals."""
def __init__(self, name):
self.name = name
class Dog(Animal):
class N:
"""Simple Binary Node class."""
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
"""Representation; useful for debugging tree."""
from app import app
from unittest import TestCase
class MadlibsTestCase(TestCase):
def setUp(self):
self.client = app.test_client()
def test_questions(self):
resp = self.client.get("/")
body = resp.get_data(as_text=True)
@joelburton
joelburton / this.md
Last active May 12, 2019 05:19
"Simple" Guide to JS this

The "Simple" Guide to JS this

A. all functions all called with a context — "what was I called on?"

  1. to find the context, look to the "left of the dot" when the function is invoked

    • eg: foo.bar() means bar will get context of foo

    • (this only works at all if "bar" is a property of foo --- otherwise, foo.bar itself is just undefined!)

def is_palindrome(word, i=0):
"""Is word a palindrome?
>>> is_palindrome("noon")
True
>>> is_palindrome("tacocat")
True
>>> is_palindrome("zacbz")
<html>
<head>
<style>
#outer { position: relative; left: 0; top: 0; }
#scrollable { width: 10em; height: 5em; overflow: scroll; }
#tooltip { display: none; position: absolute; left: 150px; top: 50px; border: solid 1px black; background-color: goldenrod; padding: 0.5em }
</style>
</head>
<body>
import React, { useState, useEffect } from 'react';
import './App.css';
/** Hook for validating email-like state.
* - `initVal`: initial state (defaults to null; is not validated!)
* Returns [data, setData] functions, like useState() does.
*
* setData validates email: if input is invalid email string,
* doesn't set & returns false. If valid, sets & returns true. */
// makes a "lazy array" of all even numbers 2...infinity
function * evens(n = 0) { // "Generator function"
while (true) {
n += 2
yield n;
}
}
let allEvens = evens() // allEvens is a "generator"
from time import time
from random import random
# A "decorator" -- a function that:
# - takes a function as its arg
# - returns a "wrapped version" of that function
#
# In this case: the wrapped version runs the real function,
# but keeps track of how long it took to run.