Skip to content

Instantly share code, notes, and snippets.

View ThomasAlexandre's full-sized avatar

Thomas Alexandre ThomasAlexandre

  • Stockholm, Sweden
View GitHub Profile
import io.circe._, io.circe.generic.auto._, io.circe.jawn._, io.circe.syntax._
case class WIFI(network:String,password:String)
val myWifi = WIFI("Scala","My_Wifi_Password")
myWifi.asJson
myWifi.asJson.noSpaces // will return a String "{"password":"My_Wifi_Password","network":"Scala"}"
import org.joda.time._
val dateStream: Stream[LocalDate] = {
def loop(ld: LocalDate): Stream[LocalDate] = ld #:: loop(ld plusDays 1)
loop(LocalDate.now())
}
dateStream.take(18).toList
<link rel="import" href="../components/polymer/polymer.html">
<polymer-element name="my-element">
<template>
<style>
#design_host {
position: absolute;
width: 100%;
height: 100%;
@ThomasAlexandre
ThomasAlexandre / Scala One-liner.scala
Created February 20, 2012 12:30
JFokus 2012 Summary of "Scala One-liners" talk
case class Deal(title:String="",disc:Int=0,city:String="")
val r = url("http://localhost:8086")
(for{x<-Http(r<>{_\"deal"});d=(x\"disc").text.toInt if d>20}yield Deal((x\"title").text,d,(x\"city").text))groupBy(_.city)mapValues(_.size)
@ThomasAlexandre
ThomasAlexandre / groupon service dispatch.scala
Created February 15, 2012 20:23
JFokus 2012 v1 - sample from "One liners are your friend"
Http(url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>") >>> System.out)
Http(url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>") as_str)
val grouponCitiesURL = url("http://api.groupon.com/v2/divisions.xml?client_id=<your_client_id>")
Http(grouponCitiesURL as_str)
val citiesAsXML = Http(grouponCitiesURL <> { response => response \\ "division" })
val cityNames = citiesAsXML map ( city => (city \ "name").text )
val cityNames = citiesAsXML map ( city => (city \ "name").text ) filter(_.startsWith("H"))
val cityLocations = citiesAsXML map { city => ((city \ "name").text,(city \ "lat").text,(city \ "lng").text) }
cityLocations find (location => location._1 == "Honolulu")
cityLocations find (_._1 == "Honolulu")
@ThomasAlexandre
ThomasAlexandre / REPL power mode.scala
Created February 15, 2012 20:19
Timing and Logging from REPL
:power
def using[A <: {def close(): Unit}, B](param: A)(f: A => B): B =
try { f(param) } finally { param.close() }
def appendToFile(fileName:String, textData:String) =
using (new java.io.FileWriter(fileName, true)){
fileWriter => using (new java.io.PrintWriter(fileWriter)) {
printWriter => printWriter.println(textData)
}
}
def timedAndLogged[T](body: => T): T = {
@ThomasAlexandre
ThomasAlexandre / JFokus 2012 scala oneliners.scala
Created February 15, 2012 20:16
JFOKUS 2012 Sample from "One-liners are your friend"
case class Deal( title:String = "", discount:Int = 0, city:String = "" )
val deal = Deal()
val deal = Deal("Hotel")
val deal = Deal("Hotel",city="Paris")
val betterDeal = deal.copy(discount=50)
import dispatch._
val request = url("http://localhost:8086/deals")
Http( request >>> System.out)
val deals = Http( request as_str )
val dealsAsXml = Http( request.handleResponseAsXml({ response => response }) )
@ThomasAlexandre
ThomasAlexandre / JFokus 2012 akka sample.scala
Created February 15, 2012 20:15
Akka Simple Dataflow Concurrency Example
import dispatch._
import akka.dispatch._
import Future.flow
val params = Promise[Map[String,String]]()
val deals = Future( Http( url("http://localhost:8086/deals") <<? params.get as_str ))
println("Result:" + deals.result)
flow { params << Map ("city" -> "stockholm") }
Thread.sleep(2000)
println("Result:" + deals.result)
@ThomasAlexandre
ThomasAlexandre / scala case classes.scala
Created January 12, 2012 10:02
Jfokus 2012 scala case class example
case class Conference(name:String="JFokus",year:Int=2011,myTalk:Option[String]=None)
val lastYear = Conference()
val thisYear = Conference(year=2012,myTalk=Some("One-liners are your friend"))
@ThomasAlexandre
ThomasAlexandre / reduceLeft example.scala
Created January 12, 2012 09:58
Pair programming in the scala REPL with my 2-year old son
val teletubbies = List("Tinky Winky","Dipsy","Laa-Laa","Po").reduceLeft(_+", "+_) + " say hello"