Skip to content

Instantly share code, notes, and snippets.

View oluies's full-sized avatar

Örjan Angré (Lundberg) oluies

  • Sweden
  • 17:58 (UTC +02:00)
  • X @oluies
View GitHub Profile
@ArtemGr
ArtemGr / alternative, using regex
Created May 21, 2009 16:29
CSV parser in Scala
val pattern = java.util.regex.Pattern.compile ("""(?xs) ("(.*?)"|) ; ("(.*?)"|) (?: \r?\n | \z ) """)
val matcher = pattern.matcher (input)
while (matcher.find) {
val col1 = matcher.group (2)
val col2 = matcher.group (4)
// ...
}
abstract class some[X](val value : X) { type T = X }
abstract class someFactory[S <: some[_]](val P : (S#T) => boolean)
{
def apply(t : S#T) : Option[S] = if(P(t)) Some(fetch(t)) else None
def unapply(s : S) : Option[S#T] = if(s == null) None else Some(s.value)
def fetch(t : S#T) = mk(t)
//isValid = predicate/constraint
abstract class Type[T](val isValid : (T) => boolean)
{
//X is the reference type
final protected[Type] case class X[T] (val value : T) {
override def toString = Type.this.getClass.getSimpleName + "(" + value + ")"
}
//Type is the alias for X[T] that we expose to the outside world
type Type = X[T]
@jboner
jboner / ReTweetRec.scala
Created July 17, 2009 22:26 — forked from Villane/ReTweetRec.scala
ReTweetRec
package retweetrec
import scala.xml.XML
import java.net.URL
import java.io.InputStream
object ReTweetRec {
def getFollowers(id: String) = {
val data = new URL("http://twitter.com/followers/ids/" + id + ".xml").getContent
@IcyMidnight
IcyMidnight / MySQL functions to convert between binary and string uuids
Created July 31, 2009 09:27
MySQL functions to convert between binary and string uuids
DELIMITER |
CREATE FUNCTION uuid_from_bin(b BINARY(16))
RETURNS CHAR(36) DETERMINISTIC
BEGIN
DECLARE hex CHAR(32);
SET hex = HEX(b);
RETURN CONCAT(LEFT(hex, 8), '-', MID(hex, 9,4), '-', MID(hex, 13,4), '-', MID(hex, 17,4), '-', RIGHT(hex, 12));
END
|
/**
* http://gist.github.com/161702.
* Start with this Groovy example, http://gist.github.com/160492
* and Scala variants suggested by others, http://gist.github.com/161159
* and http://gist.github.com/162389
*
* Here is what I came up with, but it's somewhat different. The others assume
* that Cucumber will parse the regex's and invoke the anonymous function
* specified with the Given declaration. The problem is that it's difficult to
* define a generic function signature.
import java.util.regex._
object Cucumber {
var givens:List[(String,(String) => Boolean)] = Nil
var whens:List[(String,(String) => Boolean)] = Nil
var thens:List[(String,(String) => Boolean)] = Nil
var testsRun = 0
var testsFailed = 0
@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
class ThreadLocal[T](init: => T) extends java.lang.ThreadLocal[T] with Function0[T] {
override def initialValue:T = init
def apply = get
def withValue[S](thunk:(T => S)):S = thunk(get)
}
@retronym
retronym / type-bounds.scala
Created December 16, 2009 11:17
Tour of Scala Type Bounds
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x