Skip to content

Instantly share code, notes, and snippets.

View pierzchalski's full-sized avatar

Edward Pierzchalski pierzchalski

View GitHub Profile
@pierzchalski
pierzchalski / gist:7166148
Created October 26, 2013 06:58
So I tried to make a macro for Scala 2.10.3, to create collections for sealed case objects (basically the One Big Thing that Java's enums still had going for them). Found a solution on Stack Exchange (at http://stackoverflow.com/questions/13671734/iteration-over-a-sealed-trait-in-scala). Eight hours later, it works!
package org.pierzchalski.util
import scala.language.experimental.macros
import scala.reflect.macros.Context
import scala.collection.immutable
object Util {
def values[A]: immutable.Set[A] = macro valuesImpl[A]
def valuesImpl[A](c: Context)(implicit ev: c.WeakTypeTag[A]): c.Expr[immutable.Set[A]] = {
@pierzchalski
pierzchalski / TypeClassesDemo.scala
Last active August 29, 2015 14:04
Problems with Type Classes
import shapeless._
/*
* Created by EAPierzchalski on 7th August 2014
*/
//Generic[_] and TypeClass[_] are known to have issues with
//sealed case class families inside of objects,
//so we pre-empt that by moving them to the top level.
@pierzchalski
pierzchalski / NestedRecursion.scala
Last active August 29, 2015 14:05
NestedRecursionDivergence
import shapeless._
sealed trait V
case class S(value: String) extends V
case class N(value: Int) extends V
sealed trait L
case class Vs(values: Seq[V]) extends L
object NestedRecursion {
@pierzchalski
pierzchalski / TypeClassImports.scala
Created November 7, 2014 10:33
TypeClassCompanion import issues
import shapeless._
import shapeless.test._
sealed trait T
case class A(s1: String, t: T) extends T
case class B(s2: String) extends T
object Demo1 {
import DemoAux._
@pierzchalski
pierzchalski / DistDemo.scala
Created January 8, 2015 04:57
A quick and dirty demonstration of using implicits within the new Shapeless TypeClass framework. Here, we use 'coproduct.Length' to ensure uniform probabilities over coproduct types.
import shapeless._
import shapeless.ops.coproduct.Length
import shapeless.ops.nat.ToInt
import scala.util.Random
import scalaz.Reader
sealed trait T
case class A(s: String) extends T
case class B(n: Int, t: T) extends T
@pierzchalski
pierzchalski / cs3231-rs.diff
Created February 16, 2015 02:04
The diff between a fresh CS3231-asst0 svn load and a working rust build system (if the binaries `rustc' and `llc' are in PATH).
diff --git a/asst0-src/kern/conf/conf.kern b/asst0-src/kern/conf/conf.kern
index d23e6bc..b18ae7f 100644
--- a/asst0-src/kern/conf/conf.kern
+++ b/asst0-src/kern/conf/conf.kern
@@ -425,3 +425,12 @@ file test/synchtest.c
file test/malloctest.c
file test/fstest.c
optfile net test/nettest.c
+
+#######################################
@pierzchalski
pierzchalski / getasst.sh
Created March 26, 2015 05:31
COMP3231 utilities
#! /bin/sh
set -e
if [ -z "$1" ]
then
echo "Usage: $0 ASSN-NUM"
echo "Fetches a copy of asst[ASSN-NUM] and puts it at"
echo "/home/\$USER/cs3231/asst[ASSN-NUM]-src".
echo "Deletes all traces of the unholy svn."
#[macro_use]
extern crate nom;
use nom::{alphanumeric, digit};
use std::str::FromStr;
use std::collections::{HashSet, HashMap, VecDeque};
// do_parse! lets you chain parsers together and give the intermediates a name.
// expr_res! lets you turn Result-returning functions into fake parsers.
// we use this to (step by step) turn a string into a usize.