Skip to content

Instantly share code, notes, and snippets.

@non
non / ring.scala
Created June 28, 2012 16:25
Example of 2.10.0-M4 macros with typeclasses
package ring
import language.implicitConversions
import language.experimental.macros
import scala.{specialized => spec}
import scala.reflect.makro.Context
// fairly uncontroversial typeclass
trait Ring[@spec(Int) A] {
def zero: A
erik@vx ~/angband/angband/src $ make -f Makefile.osx
CC attack.c
clang: warning: not using the clang compiler for the 'powerpc' architecture
llvm-gcc-4.2: error trying to exec '/usr/bin/../llvm-gcc-4.2/bin/powerpc-apple-darwin11-llvm-gcc-4.2': execvp: No such file or directory
clang: error: gcc frontend command failed with exit code 255 (use -v to see invocation)
make: *** [attack.o] Error 255
trait Challenge[F[_]] {
def map[A, B](f:A => B): F[A] => F[B]
def ap[A, B](f:F[A => B]): F[A] => F[B]
def foo[A, B, C](f:A => B => C): F[A] => F[B] => F[C] = {
sys.error("implement me!")
}
}
object Implicits {
implicit def xyz[A](as:Stream[A]) = new {
def takeThrough(p:A => Boolean) = tt(as)
def tt[A](as:Stream[A], p:A => Boolean):Stream[A] = as match {
case h #:: t => Stream.cons(h, if (p(h)) tt(t) else Stream.Empty)
case e => e
}
}
}
@non
non / gist:4040577
Created November 8, 2012 18:25
scala-mode2 indenting status
// ok
def foo0(x: Int) =
x match {
case 99 => 123
case _ => 999
}
// not great, prefer 1-dedent for last 3 lines
def foo1(x: Int) = x match {
case 99 => 123
scala> trait Foo[A] {
| type Bar
| def bar: Bar
|
| type Qux
| def qux: Qux
| }
defined trait Foo
scala> object IntHasFoo extends Foo[Int] {
@non
non / gist:4064198
Created November 13, 2012 05:47
Add map + flatMap to Function1-3 with no overhead
object RichF {
implicit class RichF1[A, Z](val f: A => Z) extends AnyVal {
def map[Y](g: Z => Y): A => Y =
(a: A) => g(f(a))
def flatMap[Y](g: Z => A => Y): A => Y =
(a: A) => g(f(a))(a)
}
implicit class RichF2[A, B, Z](val f: (A, B) => Z) extends AnyVal {
def map[Y](g: Z => Y): (A, B) => Y =
@non
non / calc.py
Created December 2, 2012 06:28
python calculator
#!/usr/bin/python
#
# by Erik Osheim
#
# This program uses a recursive descent, LL1 parser to generate a Code object
# for the given input. Code objects are pure expressions whose behavior can
# only be controlled via a dictionary of strings-to-numbers provided to run.
#
# Syntax:
#
#!/usr/bin/env python
#
# by Erik Osheim
from pymeta.grammar import OMeta
from itertools import chain
import math
import sys
# some errors we can encounter during compilation and execution
@non
non / dragonbat.pl
Created December 7, 2012 06:00
cute little battery script in perl (gasp)
#!/usr/bin/perl
use warnings FATAL => 'all';
use strict;
sub bool { return $_[0] eq "Yes" }
sub num { return int($_[0]) }
sub stime {
my ($n) = @_;
my $h = $n / 60;