Skip to content

Instantly share code, notes, and snippets.

View dkristian's full-sized avatar

Kristian Domagala dkristian

  • Melbourne, Australia
View GitHub Profile
trait Foo[L,R] {
def doLeft(l:L):L
}
object RightOnly extends Foo[Nothing, Int] {
def doLeft(l:Nothing) = sys.error("Not possible")
}
// > scalac -Ywarn-dead-code Nothing.scala
// Nothing.scala:4: warning: dead code following this construct
Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import scalaz._,Scalaz._
import scalaz._
import Scalaz._
scala> List(1,2,3).map(i => Some( WriterT(("Hello world", i))))
res0: List[Some[scalaz.WriterT[Any,Nothing,Nothing]]] = List(Some(scalaz.WriterTFunctions$$anon$23@5c41c446), Some(scalaz.WriterTFunctions$$anon$23@61bcfefb), Some(scalaz.WriterTFunctions$$anon$23@3f0842de))
/tmp/return> scalac -version
Scala compiler version 2.10.2 -- Copyright 2002-2013, LAMP/EPFL
/tmp/return> java -version
java version "1.7.0_15"
Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
/tmp/return> echo "object Return { def test(oi:Option[Int]):Option[String] = oi.map(i => return Some(i.toString)) }" > return.scala
/tmp/return> scalac return.scala
/tmp/return> scalac -optimize return.scala
error: could not find init in: Return.test
/tmp/return> scalac -version
Scala compiler version 2.10.2 -- Copyright 2002-2013, LAMP/EPFL
/tmp/return> java -version
java version "1.7.0_15"
Java(TM) SE Runtime Environment (build 1.7.0_15-b03)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
/tmp/return> echo "object Return { def test(oi:Option[Int]):Option[String] = oi.map(i => return Some(i.toString)) }" > return.scala
/tmp/return> scalac return.scala
/tmp/return> scalac -optimize return.scala
error: could not find init in: Return.test
@dkristian
dkristian / gist:5457532
Last active December 16, 2015 15:39
Trying to parameterise a shapeless polymorphic function
object Test {
import shapeless._
object head extends (List ~> Option) {
def apply[T](l : List[T]) = l.headOption
}
class get(i:Int) extends (List ~> Option) {
def apply[T](l : List[T]) = if (l.isDefinedAt(i)) Some(l(i)) else None
}
type LILS = List[Int] :: List[String] :: HNil
def ranges(s:Seq[Int]) =
s.sorted.foldLeft(Seq[(Int,Int)]()){
case (Seq(),i) => Seq((i,i))
case (h::t,i) => if (i > h._2 + 1) (i,i) :: h :: t else (h._1,i) :: t
}.reverse
import scalaz._
import Scalaz._
case class Person(name:String, age:Int)
val nameLens:Lens[Person,String] = Lens(_.name, (p,n) => p.copy(name = n))
val ageLens:Lens[Person,Int] = Lens(_.age, (p,a) => p.copy(age = a))
case class Update[A,V](lens:Lens[A,V], value:V)
@dkristian
dkristian / gist:1218621
Created September 15, 2011 05:39
Compose Equal[T]
import scalaz._
import Scalaz._
case class Identifier(i:Long)
case class Thing(id:Identifier)
object Thing extends Equals {
implicit def IdentifierEqual:Equal[Identifier] = equalA
implicit def ThingEqual:Equal[Thing] = equalBy(_.id)
}
object TypesProblem {
sealed trait Vice
case class Beer(style:String) extends Vice
case class Cigarette(brand:String) extends Vice
sealed trait Person[Vice] { def name:String; def vice:Vice }
case class Man[V](name:String, vice:V) extends Person[V]
case class Woman[V](name:String, vice:V) extends Person[V]
case class People[P[_]<:Person[_]](drinkers:List[P[Beer]] = List(), smokers:List[P[Cigarette]] = List())