Skip to content

Instantly share code, notes, and snippets.

@fdescamps
fdescamps / DiscoveringFunctionalProgramming.scala
Last active December 18, 2015 10:29
http://devlicio.us/blogs/christopher_bennage/archive/2010/09/06/what-is-functional-programming.aspx Chapter 1 : Discovering some functional programming concepts : First Class Functions Higher Order Functions Pure Functions Currying or Partial Application Covered in the 2nd post. Recursion Pattern Matching Memoization To test, get the file and ru…
// https://blog.stackmob.com/2013/01/resources-for-getting-started-with-functional-programming-and-scala/
import scala._
println
println( "https://blog.stackmob.com/2013/01/resources-for-getting-started-with-functional-programming-and-scala/" )
println
println( "First Class Functions" )
println( "This means that functions are basic types and can be passed around just like integers or strings." )
@fdescamps
fdescamps / PlayFramworkParseComplexeJson.scala
Created June 11, 2013 15:59
Able to read complex json, when you json block contains alternatively an object or an array without predictibility.
//Reads only sector with sequence of industries
implicit val secteurReads: Reads[Secteur] = (
( __ \ "name" ).read[String] ~
( __ \ "industry" ).lazyRead( play.api.libs.json.Reads.seq[Industrie] )
)( Secteur )
//Reads sector with sequence of industries or an only object industry
implicit val secteurReads: Reads[Secteur] = (
@fdescamps
fdescamps / EnumeratorTest
Created June 9, 2013 18:32
Just for test
def total2Chunks: Iteratee[Int, Int] = {
// `step` est la fonction qui consomme et qui reçoit le contexte précédent (idx, total) ainsi que le chunk courant
// context : (idx, total) idx est l'index pour compter les itérations
def step(idx: Int, total: Int)(i: Input[Int]): Iteratee[Int, Int] = i match {
// chunk vaut EOF ou Empty => on stoppe l'itération en déclenchant l'état Done avec le total courant
case Input.EOF | Input.Empty => Done(total, Input.EOF)
// un chunck a été trouvé
case Input.El(e) =>
// si c'est le 1er ou 2ème chunk, appeler `step` à nouveau en incrémentant idx et en calculant le nouveau total
if(idx < 2) Cont[Int, Int](i => step(idx+1, total + e)(i))
@fdescamps
fdescamps / ParseRefactor
Created June 7, 2013 14:28
Perf the code
public boolean checkInteger(String testInteger) {
/*
* The function must check that a given string is
*
* an integer
* not the empty string or null
* an integer greater than 10
* an integer between 2 and 100000 inclusive
* That the first digit is 3.
*/
@fdescamps
fdescamps / Benchmark.java
Created June 7, 2013 12:39
In order to use for benchmarking: Benchmark mbm = new Benchmark(); mbm.start(); // code to benchmark mbm.stop(); System.out.printf("%d\t%s", truecount, mbm);
package master.util;
import java.lang.management.*;
public class Benchmark {
private static final ThreadMXBean tmbean =
ManagementFactory.getThreadMXBean();
private static final long CONVERSION = 1000 * 1000;
private long cpuTime;
@fdescamps
fdescamps / StreamTest
Last active December 18, 2015 02:39
Stream et Json sont sur un bateau
/conf/secteurs.json
{
"query":
{
"count":9,
"created":"2013-06-03T12:35:25Z",
"lang":"en-US",
"results":
{
"sector":[
@fdescamps
fdescamps / Quicksort.scala
Created June 3, 2013 06:38
Quicksort.scala
def sortList(list: List[Int]): List[Int] = list match {
case Nil => Nil
case head :: tail => sortList(tail.filter(_ < head)) ::: head :: sortList(tail.filter(_ >= head))
}
@fdescamps
fdescamps / Mapping.scala
Last active December 17, 2015 18:59
Realizes a two lateral way mapping : val yahooMapping: YahooMapping = YahooMapping( "conf/yahoo_mapping.properties", "ISO-8859-1" ) yahooMapping.load() yahooMapping.print() // afficher le mapping yahooMapping.getLabel("toto") // a partir de la clef yahooMapping.getKey("totototo") // a partir du label yahooMapping.containsValue("totototo") // en …
package extraction.mapping
import scala.io.Source
/**
* .
* Realizes the mapping between fields in double lateral way
* example : toto => totototo
* User: Francois
*/
@fdescamps
fdescamps / ParseDouble
Created May 27, 2013 12:20
Parse a double when you don't really all about the characters... it's a little bit tricky
/**
* .
* Trying to parse a double
* @param s string to parse
* @return the double
*/
def parseDouble(s: String): Option[Double] = {
try {
Some(s.replaceAll("[^0-9]", "").toDouble)
} catch {
@fdescamps
fdescamps / List Map.scala
Created February 8, 2013 09:52
How to use the map list ?
What is the result of executing the following code?
List(1, 2).map { i => i + 1 }
List(1, 2).map { _ + 1 }
List(1, 2).map { i => println("Hi"); i + 1 }
List(1, 2).map { println("Hi"); _ + 1 }