Skip to content

Instantly share code, notes, and snippets.

View sptz45's full-sized avatar
🏠
Working from home

Spiros Tzavellas sptz45

🏠
Working from home
View GitHub Profile
@razie
razie / scala-profiles.markdown
Created September 24, 2010 15:35
Scala abuses

Scala is a wonderful language, with a specification of a size comparable with that of Java. Overall, the specification is simpler than C++. Why then do some feel intimidated by its expressiveness?

Scala can generate complex constructs. There are some features of the language that, if not used properly, can negatively influence one's perception of its simplicity and generate a waste of many an hour of a frustrated developer.

Newcomers from simply typed, object-oriented, structured languages do not have a full grasp of all the features of scala and their effects or benefits, and they may be very surprised if others on their team use them.

One thing to have when starting or using scala is a readily available bible. I recommend "Programming in Scala", the .pdf version, an easily searcheable language reference.

The scala style guide is very good. Here we will describe the advanced features of the language, which are aimed at library-developers and should not b

@etorreborre
etorreborre / gist:730097
Created December 6, 2010 10:17
An implicit pearl in Scala
/**
* An answer from Jason Zaugg on the scala-internals mailing list
*
* Implicit parameters can have defaults.
*/
scala> implicit def OptionalImplicit[A <: AnyRef](implicit a: A = null) = Option(a)
OptionalImplicit: [A <: AnyRef](implicit a: A)Option[A]
scala> implicitly[Option[Ordering[Int]]]
@razie
razie / scalaskilset.markdown
Created December 9, 2010 17:42
Scala skill levels
@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 + ")"
}
@ymasory
ymasory / GetVersion.scala
Created April 17, 2011 00:25
detect Scala version at runtime
/** Use with a default value, for example:
* runningScalaVersion getOrElse "2.8.0"
*/
lazy val runningScalaVersion = {
val matcher = """version (\d+\.\d+\.\d+).*""".r
util.Properties.versionString match {
case matcher(versionString) => Some(versionString)
case _ => None
}
}
@milessabin
milessabin / gist:1705644
Created January 30, 2012 17:47
Access to companion object of Foo via implicit resolution
trait Companion[T] {
type C
def apply() : C
}
object Companion {
implicit def companion[T](implicit comp : Companion[T]) = comp()
}
object TestCompanion {
@teamon
teamon / Auth.scala
Created April 8, 2012 13:43
Play2.0 with Github OAuth2 example
package controllers
import lib._
import play.api.mvc._
import play.api.libs.json._
object Auth extends Controller {
val GITHUB = new OAuth2[GithubUser](OAuth2Settings(
scala> def isNumeric[N](implicit ev: Numeric[N] = null): Boolean = ev != null
isNumeric: [N](implicit ev: Numeric[N])Boolean
scala> isNumeric[String]
res2: Boolean = false
scala> isNumeric[Int]
res3: Boolean = true
@milessabin
milessabin / gist:2473113
Created April 23, 2012 19:03
Default argument values only need to typecheck if they're needed.
scala> implicit val i = 23
i: Int = 23
scala> def foo[T](t : T)(implicit ev : T = "foo") = ev
foo: [T](t: T)(implicit ev: T)T
scala> foo(0)
res6: Int = 23
scala> foo("bar")
@milessabin
milessabin / gist:2500326
Created April 26, 2012 15:22
Scala collection extension methods made easy (dependent types FTW!)
/*
* Copyright (c) 2012 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software