Skip to content

Instantly share code, notes, and snippets.

View furf's full-sized avatar
🎯
Focusing

Dave Furfero furf

🎯
Focusing
View GitHub Profile
export type Callback<T> = (value: T) => void;
export type Unsubscribe = () => void;
export default class Callbacks<T> {
private callbacks = new Set<Callback<T>>();
subscribe(callback: Callback<T>): Unsubscribe {
this.callbacks.add(callback);
return () => this.unsubscribe(callback);
}
@furf
furf / setTimeoutWithThreshold.ts
Last active July 22, 2020 22:03
Experiments for canceling timeouts when system sleeps.
type Timestamp = number;
type Callback = (error: Error | null, timestamp: Timestamp) => void;
/**
* The default number of milliseconds beyond the expected time of execution to
* wait before assuming failure.
*/
const THRESHOLD = 10000;
/**
@furf
furf / timer.js
Last active July 9, 2020 16:20
A stopwatch-like timer
const Events = new Map();
function startEvent() {
return {
start: Date.now(),
end: null,
};
}
function endEvent(event) {
@furf
furf / lazy.js
Last active January 3, 2020 16:02
Your task will be to write a program for reversing numbers in binary. For instance, the binary representation of 13 is 1101, and reversing it gives 1011, which corresponds to number 11. The input contains a single line with an integer N, 1 ≤ N ≤ 1000000000. Output one line with one integer, the number we get by reversing the binary representatio…
function reversebinary (n) {
return parseInt(n.toString(2).split('').reverse().join(''), 2);
}
@furf
furf / index.js
Created July 13, 2019 15:59
Coding challenge for constructing and traversing (depth-first) trees.
// Found at https://www.glassdoor.com/Interview/Reddit-Senior-Software-Engineer-Interview-Questions-EI_IE796358.0,6_KO7,31.htm
// # a list of strings.Each string is a management / report relationship.
// #
// # EXAMPLE INPUT:
// #
// #[
// # 'B,E,F',
// # 'A,B,C,D',
// # 'D,G,I',
@furf
furf / readme.txt
Created October 16, 2012 20:45
bookmarklet to easify unfriending on facebook
1. Create a bookmark with the code snippet as the URL.
2. Add it to your bookmarks bar.
3. On any FB page, click the bookmarklet to activate. (Your friends page is a good place to start!)
Now, when you move your cursor over names and icons a remove button should appear. Click the button, wait a second and that friend is no longer yours.
@furf
furf / normalizeIndex.js
Created August 26, 2013 17:55
Normalize an index against the length of an array. Useful for carousels with looping.
function normalizeIndex (index, length) {
return (length + (index % length)) % length;
}
function reduceRot13Cipher(map, c, i, abc) {
const off = 13;
const len = abc.length;
const j = (i + off) % len;
map[c] = abc[j];
return map;
}
const rot13 = (() => {
const abc = 'abcdefghijklmnopqrstuvwxyz';
@furf
furf / bookmarklet.url
Created October 4, 2012 18:24
Bookmarklet for displaying QR code of current URL (good for presentations)
javascript:void !function(e,t,n,r,i,s){while(n--&&(i=t[n])>e);s=r.style,s.position="fixed",s.zIndex=-1>>>1,s.top=s.left="50%",s.marginTop=s.marginLeft=i/-2+"px",r.src="http://chart.apis.google.com/chart?cht=qr&chld=H|0&chs="+i+"x"+i+"&chl="+escape(location)}(Math.min(top.innerHeight,top.innerWidth),[100,150,200,250,300,350,400,500],8,document.body.appendChild(new Image))
@furf
furf / 0.reverseList.js
Created August 14, 2018 16:32
Reverse a Linked List, because why not.
function buildList(values) {
return values.reduceRight((next, value) => ({ value, next }), null);
}
function getValues(node) {
const values = [];
while (node) {
values.push(node.value);
node = node.next;
}