Skip to content

Instantly share code, notes, and snippets.

// This is my solution to the "Little JavaScript Problem" from http://lisperator.net/blog/a-little-javascript-problem/
// Apparently this means Mihai Bazon will hire me... Woo!
var pair = (head, tail) => (f => f(head, tail))
var car = (pair) => pair((head, tail) => head)
var cdr = (pair) => pair((head, tail) => tail)
var range = (min, max) => pair(min, min == max ? null : range(min + 1, max))
var map = (list, func) => pair(func(car(list)), cdr(list) === null ? null : map(cdr(list), func))
var reverse = (list, newList=null) => list === null ? newList : reverse(cdr(list),pair(car(list), newList))
var foreach = (list, func) => list === null ? null : (func(car(list)), foreach(cdr(list), func))
@PaulKinlan
PaulKinlan / monitorEvents.js
Created October 14, 2016 07:38
monitorEvents.js
function monitorEvents(element) {
var log = function(e) { console.log(e);};
var events = [];
for(var i in element) {
if(i.startsWith("on")) events.push(i.substr(2));
}
events.forEach(function(eventName) {
element.addEventListener(eventName, log);
});
@Rich-Harris
Rich-Harris / module-ordering.md
Last active March 23, 2022 19:22
Non-deterministic module ordering is an even bigger footgun

Non-deterministic module ordering is an even bigger footgun

Update – in the comments below, @bmeck clarifies that statically imported modules do evaluate in a deterministic sequential order, contra the Hacker News comments that prompted this gist.

Another follow-up to Top-level await is a footgun. On the Hacker News comments, Domenic says this:

(The argument also seems to be predicated on some fundamental misunderstandings, in that it thinks everything would have to be sequentialized. See my other replies throughout this thread.)

In other words, if you have an app like this...

@atdt
atdt / diffbranches
Created September 12, 2016 19:33
Diff JavaScript (or any other type of file) across two production branches
#!/usr/bin/env bash
file_pattern='*.js' # Which files to consider
v1="php-1.28.0-wmf.18" # Base branch
v2="php-1.28.0-wmf.17" # Target branch
while IFS= read -r a; do
b="${a/$v1/$v2}"
diff -u "$a" "$b" # Change '-u' to '-q' if you only want
# to list which files have changed.
done < <(find "/srv/mediawiki/${v1}" -name "$file_pattern")
@Rich-Harris
Rich-Harris / footgun.md
Last active March 9, 2025 06:13
Top-level `await` is a footgun

Edit — February 2019

This gist had a far larger impact than I imagined it would, and apparently people are still finding it, so a quick update:

  • TC39 is currently moving forward with a slightly different version of TLA, referred to as 'variant B', in which a module with TLA doesn't block sibling execution. This vastly reduces the danger of parallelizable work happening in serial and thereby delaying startup, which was the concern that motivated me to write this gist
  • In the wild, we're seeing (async main(){...}()) as a substitute for TLA. This completely eliminates the blocking problem (yay!) but it's less powerful, and harder to statically analyse (boo). In other words the lack of TLA is causing real problems
  • Therefore, a version of TLA that solves the original issue is a valuable addition to the language, and I'm in full support of the current proposal, which you can read here.

I'll leave the rest of this document unedited, for archaeological

@bendc
bendc / supportsES6.js
Created August 25, 2016 08:05
Test if ES6 is ~fully supported
var supportsES6 = function() {
try {
new Function("(a = 0) => a");
return true;
}
catch (err) {
return false;
}
}();
@atdt
atdt / takeover.py
Last active December 3, 2022 17:53
Zero-downtime restarts via interprocess descriptor transfer
#!/usr/bin/env python3
# -*- coding: utf8 -*-
"""
takeover.py
~~~~~~~~~~~
This script demonstrates a technique for zero-downtime restarts via
interprocess descriptor transfer.
The script operates a simple echo service on port 9999. When a new instance
is launched, the old instance will transfer the server socket to the new
@ashfurrow
ashfurrow / Fresh macOS Setup.md
Last active October 14, 2024 10:28
All the stuff I do on a fresh macOS Installation

Apps to install from macOS App Store:

  • Pastebot
  • GIF Brewery
  • Slack
  • Keynote/Pages/Numbers
  • 1Password
  • OmniFocus 3
  • Airmail 3
  • iA Writer
@v3rse
v3rse / the-way-of-the-substack.md
Created July 13, 2016 01:51
The way of the substack

The way of the substack

With over 200 modules in npm, and many such as browserify, dnode, optimist, etc. relied upon day-to-day by developers all around the world, substack is a pretty damn productive guy.

Plus, he's got an awesome philosophy on programming to boot (yes, there is a programming philosophy! ... no comprende? Let me explain later).

BTW, how do I know this? I got to talk to him at campjs as he wrote & published a module (lexical-scope to be exact). He was super friendly and shared alot of his thoughts on many topics.

All in all, it was really cool to meet someone that's completely congruent with what he says & lives out as a programmer. It almost felt Bret Victor-like.

@Krinkle
Krinkle / cost.js
Last active September 28, 2018 20:17
/**
* https://tfl.gov.uk/corporate/terms-and-conditions/tfl-call-charges
*
* > The rate is 0.66p per minute.
* > There is also a 40p connection charge.
* > Call charges are rounded up to the nearest 10p, with a minimum charge of 60p.
*
* @param {number} seconds
* @return {string} Cost in GBP
*/