Skip to content

Instantly share code, notes, and snippets.

View aJanuary's full-sized avatar

Andrew January aJanuary

  • Evertz
  • United Kingdom
  • 08:29 (UTC +01:00)
View GitHub Profile
@aJanuary
aJanuary / hn-comment-ancestory.js
Last active January 8, 2021 08:53
A Greasemonkey compatible user script for indicating whether a reply in a hacker news thread is from somehow new, or someone who appeared earlier in the thread.
// ==UserScript==
// @name HN comment ancestory
// @version 1
// @include https://news.ycombinator.com/item?*
// @grant none
// ==/UserScript==
const $commentTree = document.getElementsByClassName('comment-tree')[0];
const $commentRows = $commentTree.children[0].children
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD, LocalVariableBehavior.PERSISTENT);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(stdout);
container.setOutput(ps);
@aJanuary
aJanuary / latandvariablehoisting.md
Last active August 29, 2015 14:13
Let and variable hoisting

Variable hoisting is a topic that gets me unreasonably and disproportionally agitated, so I guess this post gives me another excuse to rant.

Variable hoisting isn't a thing. At least not in the sense that there's specific code in a javascript compiler that's re-writing all of your functions so the variable declarations are at the top.

Its a name given to an artefact of lexical scoping. All lexically scoped languages exhibit the exact same behaviour. But I've only ever seen the term used when talking about javascript, and there are lots of articles out there about it.

Why? Because of the way it interacts with undefined variables. In most of the other languages (anyone know of an exception?), if you try to reference the variable before its been declared (or definitely assigned a value), you'll get an error. For statically compiled languages, that will tend to be a compiler error, for the rest it will be an exception or other runtime

(function() {
var links = document.getElementsByTagName('a');
for (var i = 0, len = links.length; i < len; i += 1) {
var link = links[i];
var href = link.getAttribute('href');
if (href != null && href.substring(0, 2) == '#f') {
link.name = 'footnote' + href.substring(2);
} else if (link.name.length >= 1 && link.name[0] == 'f') {
link.href = '#footnote' + link.name.substring(1);
}
@aJanuary
aJanuary / gist:3687408
Created September 9, 2012 21:26
Testing UUID creation speed
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
class Program {
const int NumRuns = 1000000;
const int UUIDLength = 16;
static Random rng = new Random();
static Encoding encoding = System.Text.Encoding.GetEncoding("iso-8859-1");
@aJanuary
aJanuary / gist:2637407
Created May 8, 2012 17:07
Quick way to automatically fix an object to a particular interface in Java.
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class InterfaceFixer {
@SuppressWarnings("unchecked")
public static <T> T fixToInterface(T object, Class<T> clazz) {
return (T) Proxy.newProxyInstance(
clazz.getClassLoader(),
new Class[] { clazz },
@aJanuary
aJanuary / redirect_proxy.rb
Created January 24, 2012 09:50
Redirect Proxy
#!/usr/bin/ruby
require 'webrick/httpproxy'
require 'optparse'
# Monkey patch HTTPRequest so we can modify the url and cookie
class WEBrick::HTTPRequest
def update_uri(uri)
@unparsed_uri = uri
@request_uri = parse_uri(uri)
@aJanuary
aJanuary / gist:943890
Created April 27, 2011 08:08
A function to decode a uint into a series of boolean values.
public const int MaxStateSize = sizeof(uint) * 8;
public static IEnumerable<bool> DecodeState(uint state) {
return DecodeState(state, MaxStateSize);
}
public static IEnumerable<bool> DecodeState(uint state, int stateSize) {
if (stateSize < 0 || stateSize > MaxStateSize) {
throw new ArgumentException("stateSize");
}