Skip to content

Instantly share code, notes, and snippets.

View markandrus's full-sized avatar
🍊
orange

Mark Roberts markandrus

🍊
orange
View GitHub Profile
@markandrus
markandrus / Snake.hs
Last active August 29, 2015 14:12
Snake.hs
{-#LANGUAGE DeriveFunctor #-}
{-#LANGUAGE DeriveFoldable #-}
{-#LANGUAGE DeriveGeneric #-}
{-#LANGUAGE DeriveTraversable #-}
{-#LANGUAGE FlexibleInstances #-}
{-#LANGUAGE StandaloneDeriving #-}
{-#LANGUAGE TypeFamilies #-}
module Snake
( GameIO
@markandrus
markandrus / test.scala
Last active August 29, 2015 14:06
test.scala
import scala.language.higherKinds
// The point of this exercise is to build up a data type isomorphic to List,
// using basic data types like Option and Tuple2. Along the way, we define
// functors, compositions of functors, fixed-points of functors, catamorphisms,
// and tagged types.
//
// I'm new to Scala, so there are probably prettier ways to do many of these
// things.
object Test {
@markandrus
markandrus / test.scala
Last active August 29, 2015 14:06
test.scala
import scala.language.higherKinds
object Test {
// Functors
trait Functor[F[_]] {
def fmap[A, B](fa: F[A])(f: A => B): F[B]
}
implicit def OptionFunctor: Functor[Option] = new Functor[Option] {
def fmap[A, B](fa: Option[A])(f: A => B) = fa map f
@markandrus
markandrus / class.sjs
Last active August 29, 2015 14:04
class.sjs
// Class Macro with Support for Inheritance
// ========================================
macro class {
rule { $className($cparam:ident (,) ...) { $cbody } } => {
function $className($cparam (,) ...) {
if (!(this instanceof $className)) {
return new $className($cparam (,) ...);
}
$cbody ...
@markandrus
markandrus / parity.c
Last active August 29, 2015 14:04
parity.c
#include <stdio.h>
#include <stdlib.h>
void print_bits(int i) {
printf("\n ");
int n = sizeof(int) * 8;
for (int j = n-1; j >= 0; j--) {
if (!((j+1) % 8)) {
printf(" ");
}
@markandrus
markandrus / server.js
Created July 9, 2014 01:01
WebRTC Test Client
// This is the signaling server for relaying messages between Node.js and
// Chrome. Run it with
//
// $ node server.js
//
function SignalingServer() {
var clients = {};
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
@markandrus
markandrus / do.js
Created February 4, 2014 01:11
Do-notation
// This supports more cases but removes support for if-statements.
macro $do {
// Base Cases
case {_ { $ma:expr } } => {
return #{
function() {
return $ma
}()
}
#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// Table
// -----
@markandrus
markandrus / Tree.hs
Last active December 23, 2015 03:39
{-#LANGUAGE DeriveFunctor, FlexibleInstances, TypeFamilies #-}
module Tree
( TreeLike(..)
, TreeF(..)
, Tree(..)
, Trie
, toTrie
, nthLevel
, levels
@markandrus
markandrus / Trie-Free-Monad.hs
Last active December 19, 2015 08:19
Here's a toy implementation of a 'Trie' so that I can get a feel for Free monads (not that this is a monad; maybe I should explore using MonadPlus instead of the Monoid constraint?).
{-#LANGUAGE GADTs, StandaloneDeriving #-}
module Trie
( Trie
, emptyTrie
, trieIsEmpty
, trieFromList
, trieFromLists
, joinTries
, trieToLists