Skip to content

Instantly share code, notes, and snippets.

View gustavofranke's full-sized avatar

Gustavo Franke gustavofranke

View GitHub Profile
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)
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)
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._
// "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) {
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 }
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)

Laziness: I'll think about a title later

An analogy as intuition.

scala> false && { println("!!"); true } // does not print anything
res0: Boolean = false

scala> true || { println("!!"); false } // doesn't print anything either 
res1: Boolean = true
@gustavofranke
gustavofranke / fpis.sc
Created May 7, 2017 22:09
exercises from the amazing fpiscala
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 = {
// 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)
@gustavofranke
gustavofranke / implic.sc
Last active January 27, 2018 15:44
implicit examples
//////// Implicit parameter
def giveMeAnInt(implicit i: Int) = i
giveMeAnInt(123)
implicit val someInt: Int = 42
giveMeAnInt
giveMeAnInt(321)
def makeString(i: Int): String = s"makeString: ${i.toString}"
makeString(1)