Skip to content

Instantly share code, notes, and snippets.

@dpwright
dpwright / base64.hs
Created December 10, 2013 00:53
Super-simple base 64 decoder done for the sake of the exercise. Implemented entirely in terms of base; thus it doesn't make use of ByteString or Text which would probably be faster/better.
import Data.Word8
import Data.Bits
import Data.Char
import Data.List
-- Sample text taken from Wikipedia
sampleText :: String
sampleText = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="
-- splitEvery is defined in the Data.List.Splits library from package
@dpwright
dpwright / arr.rb
Last active August 29, 2015 14:01
Functional additions to ruby "array"
# Threw together these two functions to demonstrate adding some immutable modification
# functions to Ruby's Array class. "update" takes a hash of indices to values and
# returns a new array with the appropriate indices replaced by the supplied values.
# "modify" takes a list of indices and runs the block on those indices that match;
# like a version of "map" where you can specify the indices.
# Maybe you can already do this in Ruby, but I couldn't see anything in the docs so
# here's my hacky little monkey patch...
class Array
@dpwright
dpwright / funcmon.swift
Last active August 29, 2015 14:02
First attempt at functors/monads in Swift
import Foundation
//Dumb protocols... because protocols can't (afaik) take a generic type
//parameter, I can't define functions like fmap and bind in them, which are
//pretty essential for any kind of Functor/Monad implementation, but there you
//go...
//The bits in comments demonstrate what I'd *like* to do...
protocol Functor {
typealias WrappedType
typealias SelfGeneric
@dpwright
dpwright / lambdaman.hs
Created June 11, 2015 05:53
z80 example
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Prelude hiding (print, and, or)
import Data.Bits hiding (xor, bit)
import Data.Word
@dpwright
dpwright / enum-classes.cpp
Created August 18, 2015 01:27
Experimenting with type-safe features of enum classes in C++ 11
// Tested with clang version: Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
// clang++ --std=c++11 enum-classes.cpp
#include <iostream>
#include <cassert>
#include <array>
enum class MaleNames {
JACK,
JOHN,