Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
bkyrlach / ClientServer.scala
Created September 6, 2011 23:01
Client/Server with remote actors...
/*
* This is an attempt to keep references to client actors on a remote actor to push events out to them as appropriate.
* Note that, as per the comment in the client actor, the console output shows up on the server console not the client(s)
* console(s).
*
*/
package com.kyrlach.scratch
import akka.actor.{Actor,ActorRef}
import akka.actor.Actor._
@bkyrlach
bkyrlach / Stats.scala
Created September 23, 2011 20:51
Ladder simulation
//A new approach... doesn't work yet.
import akka.stm._
import java.util.concurrent.CyclicBarrier
import scala.actors.Actor
import scala.actors.Actor._
class Player(val id:Int, val matches:Ref[Int], val rating:Ref[Int]) {
@bkyrlach
bkyrlach / Combinator.scala
Created November 2, 2011 18:59
Fixed Point Combinator
//This is as close as I've come thus far, but it cheats. :(
object Combinator {
def main(args:Array[String]):Unit = {
lazy val y:((Int => Int) => (Int => Int)) => (Int => Int) = g => g(y(g))(_)
val error:(Int => Int) = n => throw new java.lang.Exception("OOPS")
val fp:((Int => Int) => (Int => Int)) = f => n => if (n == 0) 1 else n * f(n - 1)
println(y(fp)(10))
@bkyrlach
bkyrlach / FileSearch.scala
Created January 6, 2012 20:19
Faillerization...
package org.cincyfp.actor
import scala.actors.Actor
import scala.actors.Actor._
object GenFile {
def main(args:Array[String]):Unit = {
val r = new java.util.Random
val f = new java.io.File("test.data")
val fos = new java.io.FileOutputStream(f)
@bkyrlach
bkyrlach / Option.cs
Created June 5, 2012 18:54
Replicating Scala Option
//Usage
Option<String> someString = Option<String>.Some("Hi");
Option<String> noString = Option<String>.None();
Console.WriteLine(someString.Select(x => x.Length).GetOrElse(-1));
Console.WriteLine(noString.Select(x => x.Length).GetOrElse(-1));
//Definition
abstract class Option<T>
@bkyrlach
bkyrlach / Expression.fs
Created June 14, 2012 20:34
F# and Scala stuff...
//Here's some F# code...
type Expression =
| Number of int
| Add of Expression * Expression
| Multiply of Expression * Expression
| Variable of string
let rec Evaluate (env:Map<string,int>) exp =
match exp with
@bkyrlach
bkyrlach / GenericVersion.scala
Created June 18, 2012 14:19
Summation of higher kinded types.
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[T] {
val append: T => T => T
val identity: T
}
object IntMonoid extends MONOID[Int] {
@bkyrlach
bkyrlach / AST.cs
Created June 18, 2012 16:00
A more functional approach...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AST
{
class Program
{
static void Main(string[] args)
@bkyrlach
bkyrlach / MacroTest.scala
Created June 27, 2012 01:54
First Scala macro...
import language.experimental.macros
import scala.reflect.makro.Context
object MacroTest {
def lookup_builder(t: List[(Int, Int)], d: Int): Int => Int = macro lookup_builder_impl
def lookup_builder_impl(c: Context)(t: c.Expr[List[(Int, Int)]], d: c.Expr[Int]): c.Expr[Int => Int] = {
import c.mirror._
import c.universe._
@bkyrlach
bkyrlach / SymbolFun.scala
Created June 29, 2012 19:10
More fun with DSL's...
package com.kyrlach.symbol
object Identifier {
val idents = new java.util.HashMap[Symbol, Identifier]
def set[T](s: Symbol, v: T) = {
println("Setting " + s + " to " + v)
idents.put(s, new Identifier{ type I = T; val init = v })
}
def get[T](s: Symbol): T = {
println("Getting " + s)