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
// immutable cycle | |
class Node[T]( val value: T, _next: => Node[T] ){ | |
lazy val next = _next | |
} | |
val cycle: Node[Int] = new Node( 1, new Node( 2, cycle ) ) | |
// prints List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2) | |
println(cycle.iterator.take(10).map(_.value).toList) | |
implicit class NodeIterator[T](node: Node[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
{-# LANGUAGE ExistentialQuantification #-} | |
-- type "hierarchy" | |
class VehicleType a where | |
description :: a -> String | |
data Car = Car String Int -- model year | |
instance VehicleType Car where | |
description (Car model year) | |
= "Car " ++ model ++ " " ++ (show year) |
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
Welcome to Scala version 2.11.5 (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.reflect.runtime.universe._ | |
import scala.reflect.runtime.universe._ | |
scala> typeOf[java.lang.String].dealias.## | |
res0: Int = -1457551251 |
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> new{def name = "Int"}.name | |
res0: String = Int | |
scala> new{def name = "Int"} | |
res1: AnyRef{def name: String} = $anon$1@73f9d297 | |
scala> res1.name | |
warning: there was one feature warning; re-run with -feature for details | |
res2: String = Int |
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> case class Person(name: String, age:Int) | |
defined class Person | |
scala> import org.scalacheck._ | |
import org.scalacheck._ | |
scala> Gen.resultOf((Person.apply _).tupled).sample | |
res0: Option[Person] = Some(Person(祹ᨳ츜呣썷﹢偺ꉕォ资曪꫰︯㶄헖룗燠ᮆ첫谝팳뺑赊㈛뷝Ҽ贽暩鑸㮩㸢綃ﯝ媳瀨鑾斉严郋ꪝ莇鯯샄❯⇚䉙虒ꁝ䰙ᑶ웮絶ꄝ弖䧗ꌦst྾朘ࣳ㞳섡嘇᪴,-1789025436)) | |
scala> GenMore.resultOf((LargeCaseClass.apply _).tupled).sample |
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
/** | |
Defines missing implicit [[org.scalacheck.Arbitrary]] instances for tuples and functions | |
Hopefully supported by scalacheck out of the box soon | |
https://github.com/rickynils/scalacheck/pull/151 | |
*/ | |
package org.scalacheck | |
object ArbitraryMore{ | |
// Functions // | |
/** Arbitrary instance of Function6 */ |
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 Predef.{any2stringadd => _,_} | |
// GUESS THE INFERRED TYPE FOR ys! | |
class :=[T,Q] | |
object := { | |
/** Ignore default */ | |
implicit def useProvided[P, D] : P := D = new (P := D) | |
/** Infer type argument to default */ | |
implicit def useDefault[D] : D := D = new (D := D) | |
} |
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 | |
class :=[T,Q] | |
object :={ | |
/** Ignore Default */ | |
implicit def useProvided[Provided,Default] = new :=[Provided,Default] | |
/** Infer type argument as Default */ | |
implicit def useDefault[Default] = new :=[Default,Default] | |
} | |
scala> def bar[T](implicit ct: reflect.ClassTag[T]): T = { println(ct.toString); null.asInstanceOf[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
object Take1{ | |
case class DurationFactory[T](create: Int => T) | |
trait Duration{ | |
def duration: Int | |
} | |
case class MeetingDuration(duration: Int) extends Duration | |
implicit val factoryMeetingDuration = DurationFactory(new MeetingDuration(_:Int)) | |
case class ConstraintDuration(duration: Int) extends Duration |
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
// admittedly there is some optional implicit magic below to reduce some boiler place, | |
// but it is only using implicits, not runtime reflection as many test frameworks do | |
// usage: stand-alone test | |
object MyTest extends Test( | |
assert(2 == 1+1) | |
) | |
// usage: test suite | |
object MySuite extends TestSuite( |