This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-#LANGUAGE DeriveFunctor #-} | |
{-#LANGUAGE DeriveFoldable #-} | |
{-#LANGUAGE DeriveGeneric #-} | |
{-#LANGUAGE DeriveTraversable #-} | |
{-#LANGUAGE FlexibleInstances #-} | |
{-#LANGUAGE StandaloneDeriving #-} | |
{-#LANGUAGE TypeFamilies #-} | |
module Snake | |
( GameIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(" "); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This supports more cases but removes support for if-statements. | |
macro $do { | |
// Base Cases | |
case {_ { $ma:expr } } => { | |
return #{ | |
function() { | |
return $ma | |
}() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <float.h> | |
#include <math.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
// Table | |
// ----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-#LANGUAGE DeriveFunctor, FlexibleInstances, TypeFamilies #-} | |
module Tree | |
( TreeLike(..) | |
, TreeF(..) | |
, Tree(..) | |
, Trie | |
, toTrie | |
, nthLevel | |
, levels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-#LANGUAGE GADTs, StandaloneDeriving #-} | |
module Trie | |
( Trie | |
, emptyTrie | |
, trieIsEmpty | |
, trieFromList | |
, trieFromLists | |
, joinTries | |
, trieToLists |