Skip to content

Instantly share code, notes, and snippets.

@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
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
trait Enum { //DIY enum type
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia
type EnumVal <: Value //This is a type that needs to be found in the implementing class
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS}
val oldVec = get
@dadrox
dadrox / ScalaEnum.scala
Created November 10, 2011 17:40 — forked from viktorklang/ScalaEnum.scala
DIY Scala Enums (with optional exhaustiveness checking, name introspection, boilerplate reverse lookup, and (de)serialization)
// See https://github.com/dadrox/scala.enum for examples in the unit test
trait Enum {
import java.util.concurrent.atomic.AtomicReference
type EnumVal <: Value //This is a type that needs to be found in the implementing class
def withName(name: String): Option[EnumVal] = values.find(_.name == name)
def withNameIgnoringCase(name: String): Option[EnumVal] = values.find(_.name.equalsIgnoreCase(name))
@gkossakowski
gkossakowski / fix-sbt-compiler-interface.sh
Created January 19, 2012 23:32
Fix sbt's compiler interface for 2.10.0-M1
#!/usr/bin/env bash
# This is very hacky remedy to following error people using sbt
# and 2.10.0-M1 release of Scala are going to see:
#
# API.scala:261: error: type mismatch;
# found : API.this.global.tpnme.NameType
# (which expands to) API.this.global.TypeName
# required: String
# sym.isLocalClass || sym.isAnonymousClass || sym.fullName.endsWith(LocalChild)
#
@asouza
asouza / scala2.10-reflection
Created March 6, 2012 14:34
Scala 2.10 new Reflection API
import scala.reflect.api._
import scala.reflect.runtime._
import scala.reflect.runtime.Mirror._
object Pimps{
implicit def pimp(str:String) = new {
def test = println("hello")
}
}
@MichaelShaw
MichaelShaw / gist:2917907
Created June 12, 2012 14:35
Basic Skeletal Animation code in Scala
class Skeleton(val name:String) {
val joints = new ArrayBuffer[Joint]()
val jointsByName = new HashMap[String, Joint]() // convenience only
var rootJoint:Option[Joint] = None
}
class Joint {
var name:String = "unnamed joint :-("
val relativeTranslation = new Vector3d() // relative to parent
@devnulled
devnulled / OptionCheatsheet.scala
Created June 27, 2012 15:45
Scala Option Cheatsheet
// flatMap
// This code is equivalent to:
// option.flatMap(foo(_))
option match {
case None => None
case Some(x) => foo(x)
}
// flatten
// This code is equivalent to:
@wemrysi
wemrysi / gist:4567725
Created January 18, 2013 19:40
JUnitInterface for spray-testkit
package spray.testkit
import spray.util._
import akka.actor.ActorSystem
trait JUnitInterface extends TestFrameworkInterface {
def failTest(msg: String) = throw new AssertionError(msg)
}
trait JUnitRouteTest extends RouteTest with JUnitInterface {
@robmiller
robmiller / .gitconfig
Created July 17, 2013 07:52
Some useful Git aliases that I use every day
#
# Working with branches
#
# Get the current branch name (not so useful in itself, but used in
# other aliases)
branch-name = "!git rev-parse --abbrev-ref HEAD"
# Push the current branch to the remote "origin", and set it to track
# the upstream branch
publish = "!git push -u origin $(git branch-name)"
@staltz
staltz / introrx.md
Last active May 12, 2025 23:22
The introduction to Reactive Programming you've been missing