Skip to content

Instantly share code, notes, and snippets.

View gustavofranke's full-sized avatar

Gustavo Franke gustavofranke

View GitHub Profile
@gustavofranke
gustavofranke / fpmax.scala
Last active February 13, 2020 14:25
John de Goes' Fpmax in a granular way that helps me to visualise the sequence refactoring steps, I stopped when the code became testable
package fpmax
import scala.io.StdIn.readLine
import scala.util.Try
object App0 extends App {
def main(): Unit = {
println("What is your name?")
val name = readLine()
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
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 = {