Skip to content

Instantly share code, notes, and snippets.

@sinelaw
sinelaw / copy.py
Last active July 2, 2019 18:51
Parallel copy of a single file
import threading
import os
import sys
BLOCK_SIZE = 1024*1024
THREADS = 16
def write_range(source, target, start, amount, block_size):
with open(source, 'r') as source_file:
with open(target, 'r+') as target_file:
#!/bin/bash
set -eu
BRANCH_A="$1"
shift
BRANCH_B="$1"
shift
PATHS="$@"
BRANCH_A_COMMITS=$(mktemp)
ONLY_IN_BRANCH_A=$(mktemp)
BRANCH_B_COMMITS=$(mktemp)
@sinelaw
sinelaw / atomic.c
Created August 17, 2017 08:01
testing <stdatomic.h> on Intel cross-cacheline
#include <stdint.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdatomic.h>
#define CACHELINE__BYTECOUNT (64)
struct Data {
uint8_t bytes[CACHELINE__BYTECOUNT - 4];
@sinelaw
sinelaw / git-unmerged-commits
Last active June 2, 2016 13:13
Given branch/ref (e.g. my_branch) and target (e.g. origin/master), lists commits in branch that have no "equivalent" ones in target (by author, date and message)
#!/bin/bash
set -eu
# Similar to git branch --merged but looks at commit author, date and
# subject, instead of hash.
#
# Given branch/ref (e.g. my_branch) and target (e.g. origin/master),
# lists commits in branch that have no "equivalent" ones in target (by
# author, date and message).
@sinelaw
sinelaw / binary.hs
Created February 3, 2016 08:39
Binary encode/decode of Fixed
-- run it like:
-- stack ghc --package criterion -- binary.hs -Wall -O2 && ./binary
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE BangPatterns #-}
module Main where
import Data.ByteString.Lazy (ByteString)
import Data.Binary
import Data.Fixed
/*jsl:option explicit*/
"use strict";
// /////////////////////////////////////////////////////////////////////
// // JQUERY EXTENSIONS
/* Removed this part.. */
/////////////////////////////////////////////////////////////////////
// STRING FORMATTING
@sinelaw
sinelaw / Parser.hs
Last active October 31, 2015 22:20
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
-- |
module Main where
import Control.Applicative (Alternative(..), (<|>))
import Data.List (foldl', intercalate)
import Data.Monoid ((<>))
@sinelaw
sinelaw / optional-args.js
Last active September 9, 2015 20:50
optional args using polymorphic records + record field constraints
// foo :: { 0: Num, 1: Maybe Num } -> Num
function foo(a, opt_b) {
var b = opt_b == undefined ? 3 : opt_b;
return a + b;
}
// foo here inferred to have this type:
// RowFields r Maybe => { 0: Num | r } -> b
foo(3); // translated to: foo { 0 = 3 | .. }
@sinelaw
sinelaw / bla.hs
Created September 9, 2015 20:26
type case simulated using polymorphic sums
foo :: +{ Num : Number, Str : String | r } -> Number
function foo(x) {
if (typeof x == Number) { .. x is a number here! ...; return 0 }
else if (typeof x == String) { .. a string here ..; return 1 }
else { .. }
}
\case x of
Num n -> ... ; 0
Str s -> ... ; 1
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.