Skip to content

Instantly share code, notes, and snippets.

View joelburton's full-sized avatar

Joel Burton joelburton

View GitHub Profile
@joelburton
joelburton / methodChaining.js
Created July 13, 2020 21:20
Example of method chaining in JavaScript.
/** Method chaining (like jQuery uses) is an awesome programming technique --
*
* Here's an example of a Cat class that is built so that the methods can
* be chained together. See example at bottom for using this.
*
*/
class Cat {
constructor(name) {
this._name = name;
/** 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.
*/
function timeit(fn) {
function wrapper(...args) {
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.
// 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"
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. */
<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>
def is_palindrome(word, i=0):
"""Is word a palindrome?
>>> is_palindrome("noon")
True
>>> is_palindrome("tacocat")
True
>>> is_palindrome("zacbz")
@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!)

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)
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."""