Skip to content

Instantly share code, notes, and snippets.

@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:
@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
@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")
}
}
@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)
#
@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))
@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
@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