Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
bkyrlach / Crazy.scala
Created July 11, 2012 13:51
I had no idea...
trait Invert extends AddOperation {
override abstract def add(a: Int, b: Int): Int = super.add(a * -1, b * -1)
}
trait MultDiv extends AddOperation {
override abstract def add(a: Int, b: Int): Int = super.add(a * 2, b / 2)
}
trait Inc2Dec extends AddOperation {
@bkyrlach
bkyrlach / Loop.scala
Created July 11, 2012 19:56
Bad loop
def cyclic(players: List[Player], teams: List[Team], pick: Int = 1): List[Team] = {
if(players.isEmpty) {
teams
} else {
val team = teams.head
val player = choosePlayer(players)
team.copy(players = team.players + player)
val movedLast = teams.tail :+ team
if(pick % numberOfTeams == 0) {
cyclic(players.filterNot(_ == player), movedLast.reverse, pick + 1)
@bkyrlach
bkyrlach / FunctionTest.scala
Created July 14, 2012 13:15
Clojure style function...
object Test1 {
val fib: Any => Stream[Int] = {
case () => fib(1)
case (a: Int) => fib(0, a)
case (a: Int, b: Int) => Stream.cons(a, fib(b, a+ b))
}
val greet: Any => Unit = {
case () => println("Hello, world.")
case (name: String) => println("Hello, " + name + ".")
@bkyrlach
bkyrlach / core.clj
Created July 14, 2012 13:55
Understanding Clojure functions...
(defn foo
([a b] (println "bar"))
([a b c] (println "foobar")))
;#'user/foo
(foo 1 2)
;bar
;nil
(foo 1 2 3)
;foobar
;nil
@bkyrlach
bkyrlach / EmptyList.java
Created July 26, 2012 16:46
List comparison...
import java.util.ArrayList;
import java.util.List;
public class EmptyList {
//We can try and define a constant for empty List in Java too...
//But we have a problem. What type should the list be?
//We don't want to create a constant for each kind of empty list.
//The only choice seems to be Object... that can't be a good sign.
public static final List<Object> el = new ArrayList<Object>();
@bkyrlach
bkyrlach / P26.scala
Created July 26, 2012 21:22
Permutations of a list...
object P26 extends App {
def combinations[T](n: Int, l: List[T]): List[List[T]] = {
if(l.size < n) {
Nil
} else {
n match {
case 0 => Nil
case 1 => l.map{List(_)}
case _ => combinations(n-1,l.tail).map{ l.head :: _} ::: combinations(n, l.tail)
}
@bkyrlach
bkyrlach / Tree.scala
Created August 14, 2012 02:46
ADT Tree
abstract class Tree[A]
case class Leaf[A](a: A) extends Tree[A]
case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A]
object TreeTest extends App {
val t1 = Branch(Branch(Leaf(1), Leaf(2)), Leaf(3))
println(t1)
}
@bkyrlach
bkyrlach / TreeMagic.scala
Created September 13, 2012 21:44
Nemerel tree converted to Scala
package problems
trait Comparable[T] {
def compareTo(t: T): Int
}
case class ComparableInt(val a: Int) extends Comparable[ComparableInt] {
override def compareTo(b: ComparableInt): Int = {
if(a < b.a) {
-1
@bkyrlach
bkyrlach / DuckTyping.scala
Created September 17, 2012 15:43
Duck typing example..
class Program {
def run(): Unit = println("I pass a Turing test.")
}
class Person {
def run(): Unit = println("But I don't.")
}
class Engine {
def run(): String = "I run in a totally different way"
@bkyrlach
bkyrlach / ModuleFun.scala
Created October 11, 2012 18:28
Shouldn't this compile?
abstract class Tree[+A]
case class Node[A](l: Tree[A], e: A, r: Tree[A]) extends Tree[A]
case object Tip extends Tree[Nothing]
abstract class MONOID {
type t
val append: t => t => t
val identity: t
}