Skip to content

Instantly share code, notes, and snippets.

Avatar

Loïc Descotte loicdescotte

View GitHub Profile
@loicdescotte
loicdescotte / xmlParsing.scala
Last active Aug 29, 2015
Simple Scala XML Parsing
View xmlParsing.scala
//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 Aug 29, 2015
Scala futures error handling
View futureError.scala
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
View observe.js
var names = ['joe', 'bob'];
Object.observe(names, function(changes){
changes.forEach(function(change) {
console.log(change.type, change.name)
});
console.log("names",names);
});
@loicdescotte
loicdescotte / MixedTweets.md
Created Aug 5, 2012
How to push 2 mixed searches from twitter with Scala, Play2, Iteratee and Comet
View MixedTweets.md

##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 Oct 10, 2015
Play2 : Les Iteratees expliqués aux humains... francophones!
View iteratees_humains.md

#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 / Lambda.java
Created Feb 19, 2013
Java 8 lamdba test
View Lambda.java
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 Dec 15, 2015
printToFile function
View printToFile.md

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 Dec 23, 2015
Scala pattern matching examples
View patternMatch.md

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"
}
View ConstructorDI.scala
class PrintService {
def print = println("I'm a real service")
}
trait Services {
val printService = new PrintService()
// put your other services here
}
//for example, a Play controller
@loicdescotte
loicdescotte / composeFutureOfNullable.kt
Last active May 16, 2016
Kotlin : compose futures of nullables
View composeFutureOfNullable.kt
@Test
fun combineNullable() {
fun giveInt(x: Int):Int? = x+1
fun giveInt2(x: Int):Int? = x+2
fun combine(x: Int): Int? = giveInt(x)?.let { giveInt2(it) }
assertEquals(combine(1),4)
}