Skip to content

Instantly share code, notes, and snippets.

View loicdescotte's full-sized avatar

Loïc Descotte loicdescotte

View GitHub Profile
@loicdescotte
loicdescotte / MixedTweets.md
Created August 5, 2012 17:59
How to push 2 mixed searches from twitter with Scala, Play2, Iteratee and Comet

##MixedTweets

We will see how to mix 2 twitter searches and push the results to the browser in real time.

First, checkout this mini project

To try it, you need to install Play 2.0

###Controller

@loicdescotte
loicdescotte / iteratees_humains.md
Last active October 10, 2015 17:47
Play2 : Les Iteratees expliqués aux humains... francophones!

#Play2 : Les Iteratees expliqués aux humains... francophones!

Disclaimer : Ce qui suit est la traduction d'un article anglophone paru sur le blog mandubian.com

Vous pouvez retrouver l'article original ici

Vous avez probablement remarqué une nouvelle fonctionnalité intrigante de Play2 nommée Iteratee (ainsi que ses compagnons Enumerators et Enumeratee). Le but de cet article est d'essayer de rendre le concept d'Iteratee compréhensible pour le plus grand nombre avec des arguments simples, en évitant l'approche mathématique / fonctionnelle.

Cet article ne prétend pas tout expliquer à propos des Iteratee / Enumerator / Enumeratee mais traite plutôt les idées qui se cachent derrière.

@loicdescotte
loicdescotte / Forcomptran.md
Last active May 27, 2023 06:27
Scala for comprehension translation helper

Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

@loicdescotte
loicdescotte / Lambda.java
Created February 19, 2013 19:27
Java 8 lamdba test
import java.util.Arrays;
public class Lambda {
public static void main (String[] args){
Arrays.asList("aaa", "bbb", "ccc", "abb").stream()
.filter((String w) -> w.startsWith("a"))
.forEach(w -> System.out.println(w));
}
}
@loicdescotte
loicdescotte / printToFile.md
Last active May 18, 2016 08:37
Scala "print to file" function

Higher order function to write stuffs in a file, with automatic resources management

Code

  def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
    val p = new java.io.PrintWriter(f)
    try { op(p) } finally { p.close() }
  }
@loicdescotte
loicdescotte / printToFile.md
Last active December 15, 2015 09:09
printToFile function

Higher order function to write stuffs in a file, with automatic resources management

Code

  def printToFile(f: java.io.File)(op: java.io.PrintWriter => Unit) {
    val p = new java.io.PrintWriter(f)
    try { op(p) } finally { p.close() }
  }
@loicdescotte
loicdescotte / patternMatch.md
Last active December 23, 2015 20:39
Scala pattern matching examples

Scala pattern matching example

Value matching

def f(x: Int): String = x match {
  case 1 | 2 => "one or two"
  case 3 => "three"
  case _ => "other values"
}
@loicdescotte
loicdescotte / streamFile.md
Last active September 15, 2017 09:17
Stream and transform file with Play

Chunck by chunk

def  transform = Action {
    
     val fileStream: Enumerator[Array[Byte]] = {
         Enumerator.fromFile(new File("data.txt"))
     }
     
     val transfo = Enumeratee.map[Array[Byte]]{byteArray =>  
@loicdescotte
loicdescotte / xmlParsing.scala
Last active August 29, 2015 14:01
Simple Scala XML Parsing
//val xmlNode = xml.XML.loadFile("xxx")
val xmlNode =
<persons>
<person age="30">
<name>bob</name>
</person>
<person age="40">
<name>joe</name>
</person>
@loicdescotte
loicdescotte / futureError.scala
Last active August 29, 2015 14:01
Scala futures error handling
val f: Future[Int] = Future {
if(math.random < 0.5) 1 else throw new Exception("Oh no")
} recover {
case ex:Exception => {
println(ex.getMessage)
-1
}
}
f map println