scala> false && { println("!!"); true } // does not print anything
res0: Boolean = false
scala> true || { println("!!"); false } // doesn't print anything either
res1: Boolean = true
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
// Write some code, that will flatten an array of arbitrarily | |
// nested arrays of integers into a flat array of integers. | |
// e.g. [[1,2,[3]],4] -> [1,2,3,4]. | |
val a = List(List(1,2,List(3)),4) | |
val b = List(1,2,List(3)) | |
/////////////////// | |
//implicit class WrapFlatten(val ls: List[Any]) { | |
def flatten(ls: List[Any]): List[Any] = ls flatMap { | |
case i: List[_] => flatten(i) | |
case e => List(e) |
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 | |
/** | |
* EXERCISE 2.1 | |
* Write a recursive function to get the nth Fibonacci number (http://mng.bz/C29s). | |
* The first two Fibonacci numbers are 0 and 1 . The nth number is always the sum of the | |
* previous two—the sequence begins 0, 1, 1, 2, 3, 5 . Your definition should use a | |
* local tail-recursive function. | |
*/ | |
def fib(n: Int): Int = { |
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 org.scalatest.FunSuite | |
import scala.concurrent.duration._ | |
import scala.concurrent.{Await, Future} | |
// the following is equivalent to `implicit val ec = ExecutionContext.global` | |
import scala.concurrent.ExecutionContext.Implicits.global | |
class FutureFun extends FunSuite { | |
test("future basics") { | |
def unWrap[T](x: Future[T]): T = Await.result(x, 5 seconds) |
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 a: List[Int] = Nil | |
a: List[Int] = List() | |
scala> a map {x => x + 2 } | |
res0: List[Int] = List() | |
scala> val a = Nil | |
a: scala.collection.immutable.Nil.type = List() | |
scala> a map {x => x + 2 } |
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
// "com.spotify" % "docker-client" % "8.10.+" % Test | |
import com.spotify.docker.client.messages.{ContainerConfig, ContainerCreation, HostConfig, PortBinding} | |
import com.spotify.docker.client.{DefaultDockerClient, DockerClient} | |
import scala.collection.JavaConverters._ | |
import scala.util.Try | |
case class DockerServiceWrapper(image: String, hostPortForward: Int, env: List[String] = Nil) { |
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
package com.enernoc.fdr.api | |
import java.time.LocalDateTime | |
import java.time.format.DateTimeFormatter | |
import org.joda.time.DateTime | |
import org.joda.time.format.DateTimeFormat | |
import org.scalatest.FunSuite | |
import rapture.json._ | |
import rapture.json.jsonBackends.lift._ |
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
test("pipe delimited to jvm obj") { | |
case class Person(fullName: String, age: Int, address: String, hs: String) | |
object Parser { | |
def splitByPipe(s: String): Array[String] = s.split('|') | |
def init(key: String, ps: Map[String, String]): String = ps.getOrElse(key, "") | |
def bind(headers: List[String], pipeDelimited: String): Map[String, String] = headers | |
.zip(splitByPipe(pipeDelimited).toList) |
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
scala> def add: Int => Int => Int = (x: Int) => (y: Int) => x + y | |
add: Int => (Int => Int) | |
scala> add(3)(4) | |
res17: Int = 7 | |
scala> val a = add(3) | |
a: Int => Int = $$Lambda$1142/734729236@6e49b011 | |
scala> a(4) |
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 toEnglish = Map((0, 4) -> "four", (1, 3) -> "thirty", (2, 2) -> "two hundreds", (3, 1) -> "one thousand") | |
def intToEnglish(a: Int): String = a.toString.toList | |
.map { _.toString.toInt } | |
.reverse.zipWithIndex.reverse | |
.map { t => toEnglish(t.swap) } | |
.mkString(" ") | |
intToEnglish(4) // res0: String = four | |
intToEnglish(34) // res1: String = thirty four |