This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
class A2 extends A | |
class B | |
trait M[X] | |
// | |
// Upper Type Bound | |
// | |
def upperTypeBound[AA <: A](x: AA): A = x |
NewerOlder