Skip to content

Instantly share code, notes, and snippets.

@philsquared
philsquared / C++ Extension Methods
Created April 11, 2013 23:45
How to write the "left arrow operator" to enable extension methods in C++
#include <cassert>
#include <iostream>
template<typename T, typename R=void>
struct ExtMethod {
ExtMethod& operator - () {
return *this;
}
template<typename U>
@kballenegger
kballenegger / fib.c
Created May 13, 2013 04:59
Fibonacci generator in C with Y-Combinator powered memoization.
//
// Fibonacci generator in C with Y-Combinator powered memoization.
//
// Written by Kenneth Ballenegger in 2013
//
#include <stdlib.h>
#include <stdio.h>
#include <Block.h>
module ActiveSupport::Concern
def append_features(base)
if base.instance_variable_defined?("@_dependencies")
base.instance_variable_get("@_dependencies") << { :module => self, :method => :include }
return false
else
return false if base < self
@_dependencies.each { |dep| base.send(dep[:method], dep[:module]) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
@puffnfresh
puffnfresh / reornament.idr
Last active September 15, 2018 21:20
Algebraic Ornaments!
module reornament
-- Idris translation of Agda code:
-- https://gist.github.com/gallais/e507832abc6c91ac7cb9
-- Which follows Conor McBride's Ornaments paper:
-- https://personal.cis.strath.ac.uk/conor.mcbride/pub/OAAO/Ornament.pdf
ListAlg : Type -> Type -> Type
ListAlg A B = (B, A -> B -> B)
@chpatrick
chpatrick / baked.hs
Last active August 10, 2021 18:11
Baked-in Storable vectors
{-# LANGUAGE QuasiQuotes, TemplateHaskell, ScopedTypeVariables #-}
module BakedVector where
import qualified Data.ByteString.Lazy as BSL
import Data.ByteString.Lazy (unpack)
import Data.ByteString.Builder (toLazyByteString)
import Data.ByteString.Builder.Prim
import Foreign.Storable
import qualified Data.Vector.Storable as VS
@fasiha
fasiha / README.md
Last active January 10, 2020 12:58
How Rust slays brittle indexing logic.

In writing one’s own Base64 codec for the Cryptopals Crypto Challenge in Rust, one gets to a point where every chunk of four adjacent elements in an input vector has to be transformed into a chunk of three elements in an output vector.

That is, the string SSdt containing four ASCII bytes becomes the string I'm containing three ASCII bytes, and IGtp becomes  ki, and so on, so that SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t is decoded to I'm killing your brain like a poisonous mushroom.

I had a function to do this four-to-three downconversion but looping over the two arrays, lining up the indexes, the keeping track of magic threes and magic fours in my code gave me a headache as I worked through writing the following:

pub fn decode(s: &[u8]) -> Vec<u8> {
    let mut out: Vec<u8> = vec![0; s.len() / 4 * 3];
    for i in 0..ou
@ndmitchell
ndmitchell / Binary.hs
Last active August 10, 2021 21:42
Binary existentials
{-# LANGUAGE StaticPointers #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
import Data.Binary
import System.IO.Unsafe
import GHC.StaticPtr
@reiver-dev
reiver-dev / fira_code_patch.py
Created February 5, 2018 03:07
Move ligatures for Fira Code font to private unicode area at U+e100
import os
import sys
import argparse
from glob import glob
from itertools import chain
import fontforge
ADDITIONAL_LIGATURES = [
'x.multiply',
@Qqwy
Qqwy / classical_star_phase.c
Last active November 27, 2018 09:26
Bytebeat implementation of a classical piece of music, using a single sinusoid and comparing the naive, discontinuous signal with an improved, phase-accumulation based version.
/*
* Date: 2018-11-26
* Author of this C code: Wiebe-Marten Wijnja (Qqwy).
* Author of original JavaScript-version: Unknown.
*
* This version uses phase-accumulation to ensure that the signal remains continuous throughout,
* and that there are no nasty clicks when we change to the next note.
* You can add the directive `DISCONTINUOUS VERSION` (using e.g. the `-D` flag of `gcc` and `clang`)
* to compile it as the original version, that bases its samples only directly on the current sample,
* and therefore includes clicks whenever the note changes.
#include <iostream>
#include <vector>
auto continuation = [](auto && val){return [=](auto && cont) { return cont(val);};};
auto style = [](const auto &val) { return val; };
auto mod = [](auto &&modv) { return [=](auto &&truecont){ return [=](auto &&falsecont) { return [=](auto &&val) { if (val % modv) { return truecont(val); } else { return falsecont(val); }; }; }; }; };
auto passing = [](auto &&val) { return [=](auto &&cont){ return [=](auto &&) {return cont(val);};};};
bool isLeapYear(int year){
return (continuation)