Skip to content

Instantly share code, notes, and snippets.

@paul-jean
paul-jean / 0-readme.md
Last active January 18, 2016 02:31
using the presence of an object value as truthy is a BAD idea

This is an example of not being precise enough when checking for a value in an object. I inadvertently caused this bug during a live coding exercise in a job interview, and spent a couple minutes debugging before I found it.

The exercise was to code up the Fibonnaci sequence, and I was doing a variation where I memoized previously computed values of the sequence (dynamic programming). I had set up an object to hold values of the sequence as key-value pairs, where the key is n and the value is f(n).

I called the object fibMemo, and initialized it with the first two entries in the

@paul-jean
paul-jean / same_child.js
Created October 13, 2014 22:22
Referencing the same child object from two different parent objects means mutations to the common child effect both parents
// Referencing the *same* object on A.X and B.X, means changing A.X also changes B.X:
var A = {}, B = {};
var commonChild = {a:'sameValue'};
A.X = commonChild;
B.X = commonChild;
A.X.b = 'alsoSameValue';
A.X.b === B.X.b // true
// Referencing *different* objects on A.X and B.X, means changing A.X does NOT change B.X:
@paul-jean
paul-jean / http-header.sh
Created June 15, 2014 02:25
awk script to extract header from HTML response
echo -e "GET / HTTP/1.0\r\n\r\n" | nc google.com 80 | awk '{a[NR]=$0} END {line=1; while (length(a[line]) > 1) {print a[line]; line += 1} }'
@paul-jean
paul-jean / http-get.sh
Created June 15, 2014 01:32
simplest possible HTTP GET request
echo -e "GET / HTTP/1.0\r\n\r\n" | nc google.com 80 | head -5
@paul-jean
paul-jean / Factorial.java
Created February 19, 2014 23:55
Implement the factorial function using regular recursion, then again using tail recursion.
public class Factorial {
public static long fact(long n) {
if (n == 0) return 1;
return n * fact(n - 1);
}
public static long factTail(long n, long acc) {
if (n == 1) return acc;
return factTail(n - 1, n * acc);
@paul-jean
paul-jean / test-scoping.js
Created February 19, 2014 18:09
Playing with scopes in JS
var g = 5;
console.log("Global value of g = " + g); // g: 5
var x = function() {
g = 10;
};
var y = new x();
console.log("After y = new x(), g = " + g); // g: 10
console.log("After y = new x(), y.g = " + y.g); // y.g: undefined
// ^^^ because x is referring to global g
package uk.ac.ucl.cs.GI15.timNancyKawal {
class Trie[V](key: Option[Char]) {
def this() {
this(None);
}
import scala.collection.Seq
import scala.collection.immutable.TreeMap
import scala.collection.immutable.WrappedString
@paul-jean
paul-jean / mma-add-opts
Created January 29, 2014 21:43
Accept additional options for a special case of an existing head in Mathematica
Options[f]={a->1,b->2}
f[x_,opts:OptionsPattern[]]:={x+1,OptionValue[a],OptionValue[b]}
f[s_String,opts:OptionsPattern[Join[Options@f,{c->"c"}]]]:={s,OptionValue[a],OptionValue[b],OptionValue[c]}
@paul-jean
paul-jean / gist:8424692
Created January 14, 2014 20:04
Find twitter handles in a mail thread.
#!/bin/sh
cd /Users/rule146/Library/Mail/V2/IMAP-paul.jean.letourneau@imap.gmail.com/[Gmail].mbox/All\ Mail.mbox/A2AE7A8A-125F-45FA-A000-DDD9372AE843/Data/Messages
grep -E -H "To: hackerschool-w2014@googlegroups.com" *.emlx | grep -o -E "\\d+.emlx" | xargs -I % grep -o -E "(\\s|\()@[a-zA-z0-9]+" % | tr "(" " " | sort | uniq