This file contains hidden or 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 unfold { | |
def unfoldRight[A, B](seed: B)(f: B => Option[(A, B)]): List[A] = f(seed) match { | |
case Some((a, b)) => a :: unfoldRight(b)(f) | |
case None => Nil | |
} | |
def unfoldLeft[A, B](seed: B)(f: B => Option[(B, A)]) = { | |
def loop(seed: B)(ls: List[A]): List[A] = f(seed) match { | |
case Some((b, a)) => loop(b)(a :: ls) |
This file contains hidden or 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 birthday { | |
1 to 4 map { i => "Happy Birthday " + (if (i == 3) "dear Scala" else "to you") } foreach println | |
(1 to 4).foldLeft("")((r,c) => r + ("\nHappy Birthday " + (if (c == 3) "dear Scala" else "to you"))) | |
} |
This file contains hidden or 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
def findFiles(d: File): Array[File] = { | |
val (dirs, files) = d.listFiles.partition(_.isDirectory) | |
files ++ dirs.flatMap(findFiles) | |
} |
This file contains hidden or 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 scala.sys.SystemProperties | |
import scala.io.Source.fromURL | |
object ProxyTester extends App { | |
// Uncomment if you are behind a proxy, then edit the IP and port to match your proxy | |
// val props = new SystemProperties | |
//props("http.proxyHost") = "localhost" | |
//props("http.proxyPort") = "8080" | |
// Uncomment to see all the system properties including proxy settings |
This file contains hidden or 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 scala.util.matching.Regex | |
object regex { | |
val number = "07923 874123" //> number : String = 07923 874123 | |
val pattern = """(\d{5})[ -]?(\d{6})""" //> pattern : String = (\d{5})[ -]?(\d{6}) | |
number.matches(pattern) //> res0: Boolean = true | |
// Compiled pattern |
This file contains hidden or 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
"zoology".groupBy(identity).mapValues(_.length) | |
// Map(y -> 1, g -> 1, l -> 1, o -> 3, z -> 1) | |
// identity is equivalent to x => x |
This file contains hidden or 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 scala.annotation.tailrec | |
import scala.util.Random | |
import scala.util.{ Try, Success, Failure } | |
object retry { | |
{ // extra block is a workaround for Scala worksheet bug with @tailrec | |
@tailrec | |
def withRetry[T](attempts: Int)(f: => T): T = { | |
println("Attempts remaining: " + attempts) |
This file contains hidden or 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
val bytes = List[Byte](0, 1, -1, 127, -128) | |
def toBits(b: Byte) = 0 to 7 map (b >> _ & 1) | |
val bits = bytes flatMap toBits | |
// Note: least-significant bit on the left! | |
// List(0, 0, 0, 0, 0, 0, 0, 0, | |
// 1, 0, 0, 0, 0, 0, 0, 0, | |
// 1, 1, 1, 1, 1, 1, 1, 1, | |
// 1, 1, 1, 1, 1, 1, 1, 0, |
This file contains hidden or 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 javax.swing.JCheckBox | |
object SwingBuilder { | |
// Instead of this: | |
val checkbox = new JCheckBox() | |
checkbox.setText("Hello") | |
checkbox.setAlignmentX(0.0f) | |
checkbox.setLocation(0, 100) |
This file contains hidden or 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
// Adapted from http://jim-mcbeath.blogspot.co.uk/2009/08/scala-class-linearization.html | |
object diamond { | |
class A { def t = 1 } | |
// super refers to the next trait in the linearization, not necessarily to A | |
trait B extends A { override def t = super.t + 1 } | |
trait C extends A { override def t = super.t * 2 } | |
// Linearises to (D1, C, B, A) |
OlderNewer