This file contains 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
scala> import scala.collection._ | |
import scala.collection._ | |
scala> val l1: List[Int] = Set(1, 2, 3, 4).map(_/2).toList | |
l1: List[Int] = List(0, 1, 2) | |
scala> val l1: List[Int] = Set(1, 2, 3, 4).map(_/2)(breakOut) | |
l1: List[Int] = List(0, 1, 1, 2) |
This file contains 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
scala> trait Pure[T] | |
defined trait Pure | |
scala> val plusOne: Int => Int = _ + 1 | |
plusOne: Int => Int = <function1> | |
scala> val frobPlusOne: Int => Int = { i => println("frob") ; i + 1 } | |
frobPlusOne: Int => Int = <function1> | |
scala> implicit val plusOneIsPure = new Pure[plusOne.type]{} |
This file contains 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
scala> class somefn(j: Int) extends Poly1 { | |
| implicit def caseOne[A] = at[List[(Int,A)]]( xs => xs.filter(_._1 == j).map(_._2) ) | |
| } | |
defined class somefn | |
scala> object t1 extends somefn(4) | |
defined module t1 | |
scala> object t2 extends somefn(5) | |
defined module t2 |
This file contains 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
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
import shapeless._ ; import ops.hlist.Comapped | |
sealed trait Parser[A] | |
case class DummyParser[A]() extends Parser[A] | |
case class AllOf[L <: HList, O <: HList](parsers: L) | |
(implicit val cm: Comapped.Aux[L, Parser, O]) extends Parser[O] |
This file contains 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
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
case class Address(street : String, city : String, postcode : String) | |
case class Person(name : String, age : Int, address : Address) | |
// Exiting paste mode, now interpreting. | |
defined class Address | |
defined class Person |
This file contains 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
scala> import syntax.typeable._ | |
import syntax.typeable._ | |
scala> val wat: Any = List(1, 2, 3, 4) | |
wat: Any = List(1, 2, 3, 4) | |
scala> wat.cast[List[Int]].map(_.sum) | |
res0: Option[Int] = Some(10) | |
scala> val wat2: Any = "foo" |
This file contains 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 http://stackoverflow.com/questions/23192760/using-a-variable-as-an-argument-in-copy-on-a-case-class | |
import shapeless._, record._, syntax.singleton._, ops.hlist.Remove | |
/** | |
* This will be in shapeless 2.1.0 ... | |
* | |
* Permute the elements of the supplied `HList` of type `L` into the same order as the elements of | |
* the `HList` of type `M`. | |
*/ |
This file contains 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 shapeless._ | |
import ops.record.{ Selector, Updater } | |
import record.{ FieldType, field } | |
trait PathLens[T, P] { | |
type Elem | |
def get(t : T) : Elem | |
def set(t : T)(e : Elem) : T | |
} |
This file contains 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
miles@frege:~$ scala | |
Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_55). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import scala.language.dynamics | |
import scala.language.dynamics | |
scala> case class Assoc[K, V](value: V) | |
defined class Assoc |
This file contains 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
scala> trait Foo { type T; val t: T } | |
defined trait Foo | |
scala> val x = new Foo { type T = Int ; val t = 23 } | |
x: Foo{type T = Int} = $anon$1@5aeea69b | |
scala> val y = new Foo { type T = String ; val t = "foo" } | |
y: Foo{type T = String} = $anon$1@7d7902b8 | |
scala> def compare(a: Foo, b: Foo): Option[b.T] = a match { case a2: b.type => Some(a2.t) ; case _ => None } |
OlderNewer