Skip to content

Instantly share code, notes, and snippets.

View oluies's full-sized avatar

Örjan Lundberg oluies

  • Sweden
  • 10:01 (UTC +02:00)
  • X @oluies
View GitHub Profile
trait MapReduce[E, R] {
type S
def map(e: E): S
def reduce(s1: S, s2: S): S
def finish(s: S): R
}
def linear[E, R](seq: Array[E], mr: MapReduce[E, R]): R =
mr.finish(seq.map(mr.map).reduceLeft(mr.reduce))
package example
import com.google.common.base.Function
import com.google.common.collect.MapMaker
object MemoUtil {
import scalaz._
import Scalaz._
/**
@abhin4v
abhin4v / RingTopologySimulation.scala
Created July 12, 2010 19:27
Simulation of Ring network topology using Actors in Scala
import actors.Actor
import actors.Actor._
object RingTopologySimulation extends Application {
case class Message(count: Int)
class Node(id: Int, stops: Int) extends Actor {
var next: Node = null
@retronym
retronym / not-type-constraint.scala
Created July 15, 2010 15:28
Simulating a subtractive type with intentionally ambiguous implicits
trait Exclude[Scope, X]
object Exclude {
implicit def $$NOT_FOR_USER_CODE$$[X, A]: Exclude[X, A] = new Exclude[X, A] {}
def list[X] = new {
def apply[A] = $$NOT_FOR_USER_CODE$$[X, A]
}
}
sealed trait BoolOrInt
// Tony Morris' Optional Exercises
// see http://scala-programming-language.1934581.n4.nabble.com/Further-understanding-scala-Option-td2334540.html
// Scala version 2.8.0.final
// http://scalacheck.googlecode.com/files/scalacheck_2.8.0-1.8-SNAPSHOT.jar
/*
Below are 15 exercises. The task is to emulate the scala.Option API
without using Some/None subtypes, but instead using a fold (called a
catamorphism).
#define HEXCHAR2INT(x, y) ((x >= 'a' ? x-87 : x-48) * 16 + (y >= 'a' ? y-87 : y-48))
float * _parse_color(const char * str){
int size = strlen(str);
float * ret = new float[4];
switch(size){
case 4: // #rgb
ret[0] = HEXCHAR2INT(str[1], str[1]) / 255.0;
ret[1] = HEXCHAR2INT(str[2], str[2]) / 255.0;
package my.concurrent.multimap
import scala.reflect.Manifest
import java.util.concurrent.{ConcurrentSkipListSet, ConcurrentHashMap}
import java.util.{Set => JSet}
import scala.collection.JavaConversions._
import annotation.tailrec
class Index[K <: AnyRef,V <: AnyRef : Manifest] {
scala> def apply[A, B](a: A)(f: A => B) = f(a)
apply: [A,B](a: A)(f: (A) => B)B
scala> apply(1)(_ * 2)
res0: Int = 2
scala> def apply[A, B](a: A, f: A => B) = f(a)
apply: [A,B](a: A,f: (A) => B)B
scala> apply(1, _ * 2)
@fedesilva
fedesilva / optionally-saves-your-butt.scala
Created October 14, 2010 16:24
Optionally saves your skin
scala> // Problem: Some java apis return nulls.
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME")
res0: java.lang.String = null
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME").split(",")
java.lang.NullPointerException
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
makeTrade :: [(String, Maybe String)] -> Maybe Trade
-- using applicative
makeTrade alist =
Trade <$> lookup1 "account" alist
<*> lookup1 "instrument" alist
<*> (read <$> (lookup1 "market" alist))
<*> lookup1 "ref_no" alist
<*> (read <$> (lookup1 "unit_price" alist))
<*> (read <$> (lookup1 "quantity" alist))