Skip to content

Instantly share code, notes, and snippets.

View lancetw's full-sized avatar
🐈

Hsin-lin Cheng lancetw

🐈
View GitHub Profile
@gaearon
gaearon / ReduxMicroBoilerplate.js
Last active March 26, 2020 00:35
Super minimal React + Redux app
import React, { Component } from 'react';
import { createStore, combineReducers, applyMiddleware, bindActionCreators } from 'redux';
import { provide, connect } from 'react-redux';
import thunk from 'redux-thunk';
const AVAILABLE_SUBREDDITS = ['apple', 'pics'];
// ------------
// reducers
// ------------
@m-ou-se
m-ou-se / replace-debian-with-arch.txt
Last active July 3, 2025 21:23
Instructions to replace a live Debian installation with Arch
# Download latest archlinux bootstrap package, see https://www.archlinux.org/download/
wget 'ftp://ftp.nluug.nl/pub/os/Linux/distr/archlinux/iso/latest/archlinux-bootstrap-*-x86_64.tar.gz'
# Make sure you'll have enough entropy for pacman-key later.
apt-get install haveged
# Install the arch bootstrap image in a tmpfs.
mount -t tmpfs none /mnt
cd /mnt
tar xvf ~/archlinux-bootstrap-*-x86_64.tar.gz --strip-components=1
@Rich-Harris
Rich-Harris / footgun.md
Last active July 11, 2025 10:35
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

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@oscarmorrison
oscarmorrison / d3Importer.js
Last active March 6, 2024 12:50
Modularly import only the D3.js modules you require
import { line, curve, curveCatmullRom } from "d3-shape";
import { scaleTime, scaleLinear } from "d3-scale";
import { axisBottom, axisLeft } from 'd3-axis';
import { timeParse, isoFormat } from "d3-time-format";
import { select } from "d3-selection";
import { extent, max, min } from "d3-array";
export default {
line: line,
scaleTime: scaleTime,
const alphabetBoardPath = (target) => {
const layout = 5
const hashMap = new Map(Array.from("abcdefghijklmnopqrstuvwxyz")
.map((c, i) => [ c, {x: (i / layout) | 0, y: i % layout | 0}]))
const repeat = (s, n) => (n <= 0 ? '' : s.repeat(n))
let p0 = { x: 0, y: 0 }
const ret = Array.from(target).reduce((ret, t) => {
const p = hashMap.get(t)
const alphabetBoardPath = (target) => {
const layout = 5
const hashMap = new Map(Array.from("abcdefghijklmnopqrstuvwxyz").map((c, i) => [ c, {x: (i / layout) | 0, y: i % layout | 0}]))
const repeat = (s, n) => (n <= 0 ? '' : s.repeat(n))
let p0 = { x: 0, y: 0 }
return Array.from(target).reduce((ret, t) => {
const p = hashMap.get(t)
ret.push(p.y < p0.y ? repeat('L', p0.y - p.y) : '', p.x < p0.x ? repeat('U', p0.x - p.x) : '', p.x > p0.x ? repeat('D', p.x - p0.x) : '', p.y > p0.y ? repeat('R', p.y - p0.y) : '', '!')
p0 = p
return ret
var alphabetBoardPath=function(f){var g=new Map(Array.from("abcdefghijklmnopqrstuvwxyz").map(function(a,c){return[a,{x:c/5|0,y:c%5|0}]})),d=function(a,c){return 0>=c?"":a.repeat(c)},a={x:0,y:0};return Array.from(f).reduce(function(e,c){var b=g.get(c);e.push(b.y<a.y?d("L",a.y-b.y):"",b.x<a.x?d("U",a.x-b.x):"",b.x>a.x?d("D",b.x-a.x):"",b.y>a.y?d("R",b.y-a.y):"","!");a=b;return e},[]).join("")};
@gaearon
gaearon / 00-README-NEXT-SPA.md
Last active June 9, 2025 05:45
Next.js SPA example with dynamic client-only routing and static hosting

Next.js client-only SPA example

Made this example to show how to use Next.js router for a 100% SPA (no JS server) app.

You use Next.js router like normally, but don't define getStaticProps and such. Instead you do client-only fetching with swr, react-query, or similar methods.

You can generate HTML fallback for the page if there's something meaningful to show before you "know" the params. (Remember, HTML is static, so it can't respond to dynamic query. But it can be different per route.)

Don't like Next? Here's how to do the same in Gatsby.

@ctlllll
ctlllll / longest_chinese_tokens_gpt4o.py
Created May 13, 2024 19:53
Longest Chinese tokens in gpt4o
import tiktoken
import langdetect
T = tiktoken.get_encoding("o200k_base")
length_dict = {}
for i in range(T.n_vocab):
try:
length_dict[i] = len(T.decode([i]))
except: