Skip to content

Instantly share code, notes, and snippets.

@chux0519
chux0519 / 3.js
Created December 19, 2018 07:31
Sheep
// 最大公约数
const gcd = (a, b) => a === 0 ? b : gcd(b % a, a)
// 最小公倍数
const lcm = arr => arr.reduce((a, b) => a * b / gcd(a, b), 1)
const overallTotal = 9 / 10
const overallAvg = overallTotal / 3
for (let x = 2; ; ++x) {

Keybase proof

I hereby claim:

  • I am chux0519 on github.
  • I am chux0519 (https://keybase.io/chux0519) on keybase.
  • I have a public key ASBmv5sc3jH3XoEPCaV_gT02QP_HMprXAgdpHvPkLx8s2Qo

To claim this, I am signing this object:

@chux0519
chux0519 / backup.sh
Last active November 2, 2018 05:48
rancher - backup and restore
#!/bin/bash
DATA_PATH=$1
DATE=$(date +%F)
BACKUP=rancher-data-backup-$DATE.tar.gz
CONTAINER_NAME=$(docker ps | grep rancher | awk '{print $NF}')
printf "found running rancher container $CONTAINER_NAME\n"
@chux0519
chux0519 / eh.py
Created September 26, 2018 14:42
cmu db prototype
PAGE_SZ = 2
class Page(object):
def __init__(self):
self.m = {}
self.d = 0
def full(self):
return len(self.m) >= PAGE_SZ
@chux0519
chux0519 / git log without whitespace
Last active October 22, 2018 09:42
mongodb-log-sed.sh
sed -n '/protocol:op_query/p' mongod.log | sed 's/\([0-9|_|T]*\+0000\) .*command \(.*\..*\) command:.*planSummary: \([A-Z|_]*\) .*protocol:op_query \([0-9]*ms\)$/\1 \2 \3 \4/g'
@chux0519
chux0519 / ss-install.sh
Created June 23, 2018 02:17
install ss
#!/bin/bash
# Install Shadowsocks on CentOS 7
echo "Installing Shadowsocks..."
random-string()
{
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1
}
@chux0519
chux0519 / s3.js
Last active June 22, 2018 03:27
aws s3 wrapper
const AWS = require('aws-sdk')
function buildS3Service (options) {
const {
s3Key,
s3Secret
} = options
const s3Client = new AWS.S3({
accessKeyId: s3Key,
secretAccessKey: s3Secret
@chux0519
chux0519 / cps-sum.js
Created June 14, 2018 15:30
递归求和的cps变换
// 简单递归求和写法
const sumRec = x => { if (x === 0) return 0; else return x + sumRec(x - 1) }
// CPS变换,转递归为尾递归
// nodejs 依赖于v8,当前版本的实现已经不支持尾递归优化,因此下面的写法虽然是尾递归,但是仍会爆栈
const sumCPS = (x, kont) => {
if (x === 0) return kont(0)
else return sumCPS(x - 1, res => kont(res + x))
}
@chux0519
chux0519 / SPA-no-cache-server.js
Created May 22, 2018 07:04
superstatic, etag
const superstatic = require('superstatic').server
const path = require('path')
const fsProvider = require('superstatic/lib/providers/fs')
// Remove etag, cancel the browser's cache, avoid update issues
function noCacheProvider (options) {
const handler = fsProvider(options)
return (req, pathname) => handler(req, pathname).then(res => {
if (res) { // res can be null
res.modified = false
@chux0519
chux0519 / task-execution-limiter.example.js
Last active January 23, 2018 03:11
task-execution-limiter examples, paste code to http://requirebin.com to see result
const {TaskExecutionLimiter, buildTaskExecutionLimiter, buildWithLimit} = require('task-execution-limiter')
const console = require('consoleit')
const case1 = () => {
console.log('=> case 1: Usage of `TaskExecutionLimiter.schedule`')
// Using TaskExecutionLimiter class
// schedule method
const limiter = new TaskExecutionLimiter({
interval: 1000, // rate limit: one task per second
limit: 1,