Skip to content

Instantly share code, notes, and snippets.


swyx sw-yx

View GitHub Profile
sw-yx / 0_reuse_code.js
Created Nov 10, 2016
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
sw-yx / fsa-cs-saturday-compilers.js
Last active Jul 30, 2017
this is a javascript calculator compiler built for the calculators class. to evaluate, create a new `Calculator` with a mathematical expression and it will parse the tree with `parseExpression()`. Then pass a `Visitor` (we have 3 options) to `tree.accept` to get either polish notation, original notation, or evaluated result. Scroll down to the b…
View fsa-cs-saturday-compilers.js
function Calculator(inputString){
this.tokenStream = this.lexer(inputString);
Calculator.prototype.peek = function () {
return this.tokenStream[0] || null;
sw-yx /
Last active Sep 5, 2017
the precedence order of `this`


In many card games, there are certain cards or combinations of cards that "beat" or "trump" others. For example, in poker, a straight beats a three of a kind, and a three of a kind beats a two of a kind. Likewise, in the case of ties between two similar hands, an Ace will beat a King, and so on and so forth.

Precedence in programming languages is just like this. In regards to this context, we know quite a couple different ways that this context can be set - first there's the normal way (set dynamically as method on an object), then there's the new keyword, then also call, apply and bind, and now arrow functions. But what happens if these different methods collide? For example:

fig 1

const Dog = (name, breed) => {
sw-yx / timediff.js
Created Oct 12, 2017
time difference in words
View timediff.js
function timeDifference(current, previous) {
const milliSecondsPerMinute = 60 * 1000
const milliSecondsPerHour = milliSecondsPerMinute * 60
const milliSecondsPerDay = milliSecondsPerHour * 24
const milliSecondsPerMonth = milliSecondsPerDay * 30
const milliSecondsPerYear = milliSecondsPerDay * 365
const elapsed = current - previous
sw-yx / .block
Created Jan 26, 2018
Film Flowers, Single Complete
View .block
license: gpl-3.0
sw-yx / .block
Last active Jan 26, 2018
Film Flowers, Single Starter Code
View .block
license: gpl-3.0
sw-yx / .block
Last active Jan 26, 2018
Film Flowers Petal Starter Code
View .block
license: gpl-3.0
sw-yx / console.log.js
Created Apr 28, 2018
better console.log tricks
View console.log.js
var dog = { name: "fido", age: 4 };
var cat = { name: "whiskers", age: 1 };
var cow = { name: "mootools", age: 9 };
console.log(dog); console.log(cat); console.log(cow); // can't tell which is which
console.log('dog', dog); console.log('cat', cat); console.log('cow', cow); // better
console.log('dog', dog, 'cat', cat, 'cow', cow); // better
console.log({ dog, cat, cow }); // better
sw-yx / gist:41a401f5a8feceba97d559ac2679f459
Created Mar 25, 2018
React Rally Talk Proposal: React Mind, Beginner's Mind
View gist:41a401f5a8feceba97d559ac2679f459
sw-yx / Timer fallback.js
Created Nov 14, 2018
Timer component for use as a Suspense fallback
View Timer fallback.js
function Timer() {
const startTime = React.useRef(;
const [time, setTime] = React.useState(;
React.useEffect(() => {
const id = setTimeout(() => {
ReactDOM.flushSync(() => {
}, 2);