Skip to content

Instantly share code, notes, and snippets.

@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)
@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>
@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.
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")
#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)
@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
@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',
defmodule SteveTrollsElixir.Private do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@on_definition {unquote(__MODULE__), :on_priv_def}
end
end
defmacro private() do
quote do

lib/transfer_resolver.csv:

10D,1,Signed to 10-Day Contract
10D2,2,Signed to Second 10-Day Contract
ABS,3,Leave of Absence
ACT,4,Activated
BRV,5,Placed on Bereavement List
CEXP,6,Contract Expired
CEXT,,Contract Extension
@Qqwy
Qqwy / capturepipe2.ex
Last active June 24, 2020 15:38
Elixir example of a pipe-operator that uses some simple metaprogramming to allow piping into capture-syntax. Version with slightly more verbose `&`-prefixed syntax. Naive implementation that does not allow using multiple captures in the same pipeline.
defmodule Capturepipe do
@doc """
A pipe-operator that extends the normal pipe
in one tiny way:
It allows the syntax of having a bare `&1` capture
to exist inside a datastructure as one of the pipe results.
This is useful to insert the pipe's results into a datastructure
such as a tuple.