Skip to content

Instantly share code, notes, and snippets.

@skaslev
skaslev / Tangles.hs
Last active June 16, 2016 20:42
Tangles.hs
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
module Main where
import Control.Applicative
import Control.Monad
import qualified Data.Map as M
import Test.QuickCheck (quickCheck)
@skaslev
skaslev / Loeb.hs
Last active June 13, 2016 09:52
Loeb.hs
-- http://blog.sigfpe.com/2006/11/from-l-theorem-to-spreadsheet.html
{-# LANGUAGE FlexibleContexts #-}
module Main where
import Control.Applicative
import Control.Monad.Reader
loeb :: Functor a => a (a x -> x) -> a x
@skaslev
skaslev / cantor.py
Last active June 3, 2016 10:49
Cantor's diagonal argument
# https://en.wikipedia.org/wiki/Cantor%27s_diagonal_argument
def bits(n):
return list(int(x) for x in reversed(bin(n)[2:]))
def nth(lst, n):
if n < len(lst):
return lst[n]
@skaslev
skaslev / sudo.py
Created April 15, 2016 09:00 — forked from barneygale/sudo.py
import sys, marshal, functools, subprocess
child_script = """
import marshal, sys, types;
fn, args, kwargs = marshal.load(sys.stdin)
marshal.dump(
types.FunctionType(fn, globals())(*args, **kwargs),
sys.stdout)
"""
@skaslev
skaslev / quasicrystal.py
Created March 15, 2016 13:01 — forked from omegahm/quasicrystal.py
Quasicrystals in Python
# -*- coding: utf-8 -*-
import bohrium as np
from math import pi
import matplotlib.pyplot as plt
import matplotlib.colors as colors
fig = plt.figure()
plt.xticks([])
plt.yticks([])
@skaslev
skaslev / RigidbodyMassCalculator.cs
Created January 5, 2016 12:27 — forked from FreyaHolmer/RigidbodyMassCalculator.cs
Used to approximate a proper mass value for all the colliders in a given Rigidbody
using UnityEngine;
using System.Linq;
[RequireComponent(typeof(Rigidbody))]
public class RigidbodyMassCalculator : MonoBehaviour {
public float density = 1f;
public bool recalculateOnAwake = true;
Rigidbody rb;
@skaslev
skaslev / timer.cpp
Last active September 14, 2015 07:20
Nanosecond precision timer
#include <stdint.h>
#include <stdio.h>
#include <time.h>
namespace {
const int64_t SEC = 1;
const int64_t MILI = 1000; // 10^3
const int64_t MICRO = 1000 * 1000; // 10^6
const int64_t NANO = 1000 * 1000 * 1000; // 10^9
@skaslev
skaslev / .bashrc
Last active August 29, 2015 14:08
Bash config
export PROMPT_DIRTRIM=3
export PS1='\[\033[0;35m\]\u@\h\[\033[0;33m\] \w\[\033[00m\]$ '
export TERM=xterm-256color
export PATH="$HOME/bin:$PATH"
export GOPATH="$HOME/.go"
export USE_CCACHE=1
export CCACHE_DIR="$HOME/.ccache"
export CC=/usr/bin/clang
export CXX=/usr/bin/clang++
@skaslev
skaslev / defer.cpp
Last active October 15, 2021 23:18
Go-like defer statement for C++
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
namespace {
template<typename F>
class ScopeGuard {
public:
ScopeGuard(F f) : f_(f) {
@skaslev
skaslev / complex_step.cc
Last active August 29, 2015 14:07
Complex Step Differentiation
#include <stdio.h>
#include <complex>
#include <tuple>
namespace {
// See http://blogs.mathworks.com/cleve/2013/10/14/complex-step-differentiation/
template<class R, class F>
std::tuple<R, R> dF(F f, R x, R h=1e-8) {
using C = std::complex<R>;