Skip to content

Instantly share code, notes, and snippets.

View jorgeortiz85's full-sized avatar

Jorge Ortiz jorgeortiz85

View GitHub Profile
@jorgeortiz85
jorgeortiz85 / unsafe.scala
Created August 12, 2011 15:41
The bug from hell
// This code is unsafe to use in a multithreaded environment.
object A {
def foo = "foo"
B.bar
}
object B {
def bar = "bar"
A.foo
@jorgeortiz85
jorgeortiz85 / please_typecheck.scala
Created July 22, 2011 16:40 — forked from dt/please_typecheck.scala
I want line 15 to typecheck
package demo
trait Bar
abstract class Foo[T] {}
object Foo {
implicit def FooFromBar[T <: Bar : Manifest]: Foo[T] = new Foo[T] { }
def apply[T : Foo] = implicitly[Foo[T]]
def baz[T : Foo](arg: T): Unit = { println("ha!")}
@jorgeortiz85
jorgeortiz85 / FoldUnion.scala
Created June 9, 2011 17:11
Folding over Scala union types
type ¬[A] = A => Nothing
type ∨[T, U] = ¬[¬[T] with ¬[U]]
type ¬¬[A] = ¬[¬[A]]
type |∨|[T, U] = { type λ[X] = ¬¬[X] <:< (T ∨ U) }
class FoldUnion[T](t: T) {
def boxClass(x: java.lang.Class[_]): java.lang.Class[_] = x.toString match {
case "byte" => manifest[java.lang.Byte].erasure
case "char" => manifest[java.lang.Character].erasure
case "short" => manifest[java.lang.Short].erasure
import sbt._
class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
val liftVersion = "2.3"
val liftWebkit = "net.liftweb" %% "lift-webkit" % liftVersion % "compile"
val liftMapper = "net.liftweb" %% "lift-mapper" % liftVersion % "compile"
val jetty = "org.mortbay.jetty" % "jetty" % "6.1.22" % "test"
val junit = "junit" % "junit" % "4.5" % "test"
val logback = "ch.qos.logback" % "logback-classic" % "0.9.26"
@jorgeortiz85
jorgeortiz85 / MongoDynamic.scala
Created May 17, 2011 06:01
Proof-of-concept MongoDynamic
import com.mongodb._
import org.bson.types.ObjectId
trait MongoDynamic extends Dynamic {
def applyDynamic(name: String)(args: Any*): MongoDynamic
def expand[A : Manifest]: Option[A]
}
object EmptyMongoDynamic extends MongoDynamic {
override def applyDynamic(name: String)(args: Any*): MongoDynamic = EmptyMongoDynamic
@jorgeortiz85
jorgeortiz85 / PrivateMethodCaller.scala
Created April 7, 2011 15:41
Calling private methods in Scala
// Usage:
// p(instance)('privateMethod)(arg1, arg2, arg3)
class PrivateMethodCaller(x: AnyRef, methodName: String) {
def apply(_args: Any*): Any = {
val args = _args.map(_.asInstanceOf[AnyRef])
def _parents: Stream[Class[_]] = Stream(x.getClass) #::: _parents.map(_.getSuperclass)
val parents = _parents.takeWhile(_ != null).toList
val methods = parents.flatMap(_.getDeclaredMethods)
val method = methods.find(_.getName == methodName).getOrElse(throw new IllegalArgumentException("Method " + methodName + " not found"))
@jorgeortiz85
jorgeortiz85 / gist:906503
Created April 6, 2011 20:52
Scala for-comprehensions
// The way Scala for-comprehensions work is that:
for (x <- c) f(x)
// simply translates to:
c.foreach(x => f(x))
// Likewise:
object AuthenticatedSession extends Loggable {
def logIn(credentialsOption: Option[Credentials]): Twitter = {
val (tw, credentials) = credentialsOption match {
case Some(cr) =>
(createTwitter(Some(cr)), cr)
case None =>
val twitter = createTwitter(None)
val requestToken = twitter.getOAuthRequestToken
DesktopUtil.browse(requestToken.getAuthorizationURL)
(twitter, Dialog.showInput(null, "Enter the PIN from the Twitter authorization page in your browser",
@jorgeortiz85
jorgeortiz85 / DynamicImpl.scala
Created January 17, 2011 20:16
Method calls & XML traversal with Scala's new Dynamic type
class DynamicImpl(x: AnyRef) extends Dynamic {
def _select_(name: String): DynamicImpl = {
new DynamicImpl(x.getClass.getMethod(name).invoke(x))
}
def _invoke_(name: String)(args: Any*) = {
new DynamicImpl(x.getClass.getMethod(name, args.map(_.asInstanceOf[AnyRef].getClass) : _*).invoke(x, args.map(_.asInstanceOf[AnyRef]) : _*))
}
override def typed[T] = x.asInstanceOf[T]
override def toString = "Dynamic(" + x.toString + ")"
}
scala> object Foo
defined module Foo
scala> val x = Foo
x: Foo.type = Foo$@24753433
scala> val y = identity(Foo)
y: object Foo = Foo$@24753433