Skip to content

Instantly share code, notes, and snippets.

@bakkot
bakkot / threads.cc
Created November 17, 2012 21:00
Example of several surprisingly difficult C++11 threading features
// compile as g++-4.7 --std=c++11 threads.cc -lpthread -o threads
// g++ 4.7.2 will compile fine without the -lpthread, but (at least on my machine) the resulting binary burns and dies. -lpthread does need to occur after the .cc to work, for reasons not clear to me.
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono> // timing, aw yeah
#include <atomic>
#include <cmath> // later irrelevant
@bakkot
bakkot / ssc comment wider
Last active August 29, 2015 14:11
bookmarklet to make comments on SSC wider
javascript:(function(){c=document.querySelector('#comments');c.style.position='absolute';c.style.marginLeft='-100px';c.style.marginRight='40px';c.style.marginBottom='20px';document.querySelectorAll('#respond')[1].style.display='none';}())
@bakkot
bakkot / SSC-SBC1
Created December 18, 2014 06:01
SSC sort by number of children (including in subthreads)
javascript:function c(l){return l.querySelectorAll('li.comment').length;}function s(e){var h=[];for(var i=0; i<e.children.length; ++i){h.push({'E':e.children[i],'C':c(e.children[i])});}var t=h.sort(function(a,b){return a.C-b.C;});for(var i=t.length-1;i>0;--i){e.insertBefore(t[i].E,t[0].E);}}s(document.querySelector('ol.commentlist'));var d=document.querySelectorAll('ul.children');for(var i=0;i<d.length;++i){s(d[i]);}
@bakkot
bakkot / SSC-SBC2
Created December 18, 2014 06:03
SSC sort by number of children (only top-level threads)
javascript:function c(l){return l.querySelectorAll('li.comment').length;}function s(e){var h=[];for(var i=0; i<e.children.length; ++i){h.push({'E':e.children[i],'C':c(e.children[i])});}var t=h.sort(function(a,b){return a.C-b.C;});for(var i=t.length-1;i>0;--i){e.insertBefore(t[i].E,t[0].E);}}s(document.querySelector('ol.commentlist'));
@bakkot
bakkot / SSC-SBC Full
Created December 18, 2014 06:04
SSC sort by number of children (un-minified)
function countChildren(liCommentEle) {
return liCommentEle.querySelectorAll('li.comment').length;
}
function sortList(listEle) {
var children = [];
for(var i=0; i<listEle.children.length; ++i) {
children.push({'ele': listEle.children[i], 'childCount': countChildren(listEle.children[i])});
}
var sorted = children.sort(function(a, b){return a.childCount - b.childCount;});
@bakkot
bakkot / gist:01ce8967945690ddd4cf
Created February 10, 2015 20:01
SSC Collapse top-level threads
javascript:(function(){cs=document.querySelectorAll('.depth-1 > .commentholder a.comment-reply-link:last-child');cs=Array.prototype.slice.call(cs);cs.reverse();cs.forEach(function(c){commentToggle.call(c);});})()
@bakkot
bakkot / gist:375f1d1cc07d69181d9e
Created April 1, 2015 20:54
SSC floater fixed position
javascript:(function(){document.querySelector('.comments-floater').style.position='absolute';})()
@bakkot
bakkot / horrifying.js
Last active December 17, 2022 18:02
Various really bad JavaScript
(function x(){"use strict"; x = 1;}()); // TypeError
(function x(){x = 1; return x !== 1;}()); // write fails silently; function returns true
(function x(){"use strict"; x = (function(){throw 0;})();})() // Error 0
// These three lines rely on ES6.
(function x(){const x = 1;})() // No-op. In particular, not a redeclaration of x.
(function (){"use strict"; const x = 1; x = 2;})() // TypeError
(function (){const x = 1; x = 2;})() // TypeError. contrast (function x(){x = 2;}());
@bakkot
bakkot / ldbe.py
Created October 15, 2015 09:10
investigating a hypothesis about graphs
import networkx as nx
from collections import deque
import random
random.seed(42) # determinism!
def unique(l):
return len(l) == len(set(l))
def canonical(path): # path is a tuple. should also ensure sub-cycles are canonized, really, but fuck it
# picks the minimum-valued starting point.
(function () {
var g, h;
function x() { f = ""; }
function y() { h = f; }
{
// var-scoped f is undefined, let-scoped f is a function
f = 1; // let-scoped f gets value 1
x(); // var-scoped f gets value ""
y(); // h gets value of var-scoped f
function f() {} // var-scoped f gets value of let-scoped f