Skip to content

Instantly share code, notes, and snippets.

@petekneller
petekneller / Nullable.scala
Created January 4, 2016 13:09 — forked from noelwelsh/Nullable.scala
Nullable types in Scala
object Nullable {
sealed trait NullableTag
type Nullable[A] = A with NullableTag
def nullAs[B]: Nullable[B] =
null.asInstanceOf[Nullable[B]]
implicit class ToNullable[A](val a: A) extends AnyVal {
def `?`: Nullable[A] = a.asInstanceOf[Nullable[A]]
}
@petekneller
petekneller / file1.scala
Created September 14, 2015 10:13
SBT BuildInfo example 2 - output in /src
object BuildInfo {
private def outputDir(baseDir: File): File = baseDir / "src_mananaged"
lazy val settings: Seq[Setting[_]] = Seq(
managedSourceDirectories in Compile <+= baseDirectory apply {baseDir => outputDir(baseDir)},
sourceGenerators in Compile <+= (streams, baseDirectory, version) map emitBuildInfo
)
def emitBuildInfo(logger: TaskStreams, baseDir: File, buildVersion: String): Seq[File] = {
/**
* Part Zero : 10:15 Saturday Night
*
* (In which we will see how to let the type system help you handle failure)...
*
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0)
*/
import scalaz._
import Scalaz._
@petekneller
petekneller / Props.scala
Created February 23, 2015 15:56
Dumping system props (drop this in a dir and do "sbt run")
package props
object Main extends App {
import System._
import scala.collection.JavaConversions._
getProperties.stringPropertyNames().foreach{ pn => println(s"$pn -> ${getProperty(pn)}") }
}
/*
The Oracle Java enum Planets example using the Klang DIY Enum, modified to include ordering.
*/
object KlangEnumPlanets extends App {
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
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
package demo
import scalaz._
import scalaz.syntax._
import scalaz.effect.IO
import scalaz.effect.IO._
import scalaz.syntax.std.either._
object Demo {
type M[A] = EitherT[IO, Throwable, A]
import scalaz.effect.IO
import scalaz.{EitherT, Monad}
object Program extends ProgramInstances {
def apply[A](a: A): Program[A] = Program[A](EitherT.right[IO, Throwable, A](IO(a)))
}
trait ProgramInstances {
implicit object ProgramMonad extends Monad[Program] {
override def point[A](a: => A): Program[A] = Program[A](a)
// No case classes
object Consoles {
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
/** GADTs in Scala and their limitations */
/** Background: what is an algebraic data type (ADT) ?
* ADT: (possibly) recursive datatype with sums and products
* In scala - a trait with case classes (case class is product, subtyping is sum)
*/
/** Motivation: untyped embedded DSL doesn't prevent nonsensical expressions */
sealed trait Expr {
def apply(other: Expr) = Ap(this, other)