Skip to content

Instantly share code, notes, and snippets.

@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
@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
@Qqwy
Qqwy / addition.beam_disasm.ex
Last active July 8, 2020 14:34
An example of what BEAM code ends up being generated for a simple TypeCheck spec.
{:beam_file, Addition,
[
{:__info__, 1, 2},
{:"__type_check_spec_for_add/2__", 0, 18},
{:add, 2, 8},
{:baseline_add, 2, 16},
{:module_info, 0, 20},
{:module_info, 1, 22}
], [vsn: [337339698024769425821845159222917769638]],
[
@eksperimental
eksperimental / typespec_example.ex
Last active July 2, 2020 18:36
Broken types in Elixir Issue #10140
# Downloaded from: https://gist.github.com/eksperimental/55f1e207ab5878a668668546f57a3f90
#
# Lists all the types that have the problem reported in
# https://github.com/elixir-lang/elixir/issues/10140
# create an Elixir project, and save this file in lib/typespec_example.ex
# Start IEx with: iex -S mix
# then run: TypespecExample.types()
# and paste the output into the shell.
defmodule TypespecExample do
@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>
@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.

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
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
@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',
@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