Skip to content

Instantly share code, notes, and snippets.

@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,
@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 / 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 / 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 / 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 / fixPath.ps1
Last active December 26, 2015 04:28
Overcoming Windows' bizarre ordering of %PATH% variables
# Windows constructs the %PATH% environment variable by appending the user
# PATH to the system PATH, which means you can't override system-level programs
# with your own, user-specific version.
# To fix this, save this script and set it to run on logon by following these
# instructions: http://technet.microsoft.com/en-us/library/cc770908.aspx
# (If step 1 is a mystery to you, go to Start->Run and type "gpedit.msc")
# Requires Windows 7
# If your path is longer than 1024 characters, this might not work :-(
@dpwright
dpwright / watchdog.lhs
Last active September 30, 2016 18:25
Watchdog -- Dumb Haskell process monitor
Watchdog
========
Watchdog is a simple utility to spawn and monitor a process, respawning it if it
dies or is killed for whatever reason. This is useful if you have a daemon
process that may encounter an unexpected error, or even just as a quick 'n' easy
upgrade mechanic -- just pop the new executable over the old one and shut the
process down!
There are a million of these, but I needed something quick 'n' dirty to
@dpwright
dpwright / README.md
Last active June 21, 2018 03:09
Monadic operations in C++

Monadic operations in C++

This began as a further attempt to implement the Maybe monad in C++, but quickly spiralled out of control and now includes an implementation of the List monad as well (using std::list!). This is really for my own amusement rather than to try and do anything useful with them. It also gave me an excuse to try out C++ 11 lambda functions for the first time.

My original implementation defined a macro called MBind which caused a number of problems -- thankfully [PJayB][pjayb] managed to find a way around that so

@dpwright
dpwright / maybem.cpp
Created September 6, 2013 15:35
General Maybe monad in C++
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
template<typename a> class Maybe
{
public:
static Maybe<a> Just(a value) { return Maybe(value); }
@dpwright
dpwright / ctuple.h
Last active December 12, 2015 00:18
Statically-defined tuples in C
/* Basic tuple definitions */
#define ctuple1(a) union { struct { a hd; }; struct { a idx0; }; }
#define ctuple2(a, b) union { struct { a hd; ctuple1(b) tl; }; \
struct { a idx0; b idx1; }; }
#define ctuple3(a, b, c) union { struct { a hd; ctuple2(b, c) tl; }; \
struct { a idx0; b idx1; c idx2; }; }
#define ctuple4(a, b, c, d) union { struct { a hd; ctuple3(b, c, d) tl; }; \