Skip to content

Instantly share code, notes, and snippets.

@ubourdon
ubourdon / Feed.scala
Created August 24, 2016 08:44
feed a scalaz Process with recursive async function
/**
My problem is:
I have a function getData[A](params ...): Task[List[A]] that return asynchronously data
But this data is big so I cut the call in several step playing with params
In term of API, I want expose a function stream[A]: Process[Task, A]
I see Process.repeatEval & stream.async.unboundedQueue API from scalaz
but I don't understand how to use it to produce this result
@ubourdon
ubourdon / async.scala
Created April 8, 2016 13:08
From scala.concurrent.Future[A] To scalaZ.concurrent.Task[A]
import java.util.concurrent.ExecutorService
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scala.concurrent.{Future, Promise}
import scala.util.{Failure, Success}
import scalaz.{-\/, \/-}
import scalaz.concurrent.{Strategy, Task}
import scalaz.Scalaz.ToEitherOps
package object async {
implicit class FutureOps[A](f: Future[A]) {
@ubourdon
ubourdon / tests.scala
Last active October 28, 2015 12:33
Scala collection library WTF !
/*test("sortWith") {
val ns = // !!! SCROLL DOWN --- MORE TEST DATA BELOW !!!
List(
1327986000000L,
1327986000000L,
1327986000000L,
1327986000000L,
1254283200000L,
1254283200000L,
1254283200000L,
@ubourdon
ubourdon / FrontDev.scala
Last active October 9, 2015 16:58
Performmance Immo recrute un développeur 'FrontEnd' pour faire du React.js
package fr.perfimmo.hiring
// Le code compile ^^
object FrontDev {
val whoWeAre = PerformmanceImmo(
companyName = "performmance immo",
website = "http://www.performance-immo.com",
areaField = "edition logiciel / administration de bien",
@ubourdon
ubourdon / TrampolineTest.scala
Created September 22, 2015 14:40
Trampoline style
import org.scalatest.{Matchers, FunSuite}
import scala.annotation.tailrec
// https://www.youtube.com/watch?v=hzf3hTUKk8U
// http://blog.richdougherty.com/2009/04/tail-calls-tailrec-and-trampolines.html
class TrampolineTest extends FunSuite with Matchers {
sealed trait Trampoline[A] {
final def run: A = runImpl(this)
@ubourdon
ubourdon / MonadT.scala
Last active September 2, 2015 09:53
MonadT exemple
object MoanadT {
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import scalaz.OptionT
import scalaz.Scalaz.{ToIdOps, ToEitherOps}
import scalaz.{\/, EitherT}
import scalaz.std.scalaFuture._
def fOptionT[A](x: Future[Option[A]]): OptionT[Future,A] = OptionT[Future,A](x)
def fEitherT[E,A](x: Future[E \/ A]) = EitherT[Future, E, A](x)
def toEither[E, A](e: E)(x: OptionT[Future, A]): EitherT[Future, E, A] = x.fold(some => some.right[E], e.left[A]) |> fEitherT[E,A]
@ubourdon
ubourdon / ApplyTo.scala
Last active August 29, 2015 14:27
Why have this compilation error & how to fix it
/** In fact I try to write parametric Eventsourcing apply function
* And which allow me to have a parametric function for CommandHandler[D] too.
*/
trait ApplyTo {
def applyTo[E, S](startingState: S)(events: List[E])(implicit de: DomainEvent.Aux[E, S]): S = {
events.foldLeft(startingState) { (currentState, event) => de.apply(currentState, event) }
}
}
@ubourdon
ubourdon / AlgoTest.scala
Created July 24, 2015 14:16
extract parts of algo in function produce 5x slower algo
class SpikeTest2 extends FunSuite with Matchers with DurationTestTools {
val boatPrice = Price(1D, false, 20D)
test("classic algo") {
val season = BoatSeason(TODAY.minusDays(150), TODAY.plusDays(150))
val specialPeriod = (0 to 50).map { i => SpecialPricePeriod(TODAY.plusDays(i), TODAY.plusDays(i).plusDays(2), i +1) }.toList
val (time, _) = mesureTimeExecutionOf { ClassicAlgo.buildBookingDaysPrice(boatPrice, season, specialPeriod, TODAY, TODAY.plusDays(5)) }
time shouldBeMinusThan (50 milliseconds)
@ubourdon
ubourdon / Test.scala
Created April 24, 2015 11:54
polymorphic function & type alias - compilation error
object Test {
val toto: Toto = ???
val indexTotoInES: Toto => Future[Try[String]] = ???
val insertToto: Toto => Future[Option[Toto]] = ???
toto |> saveAndIndex(indexTotoInES)(insertToto) // compile OK
toto |> saveAndIndexAlias(indexTotoInES)(insertToto) // Don't compile with errors :
/**
@ubourdon
ubourdon / mapping.json
Last active August 29, 2015 14:17
ElasticSearch request
"pi_idxreclamations":{
"mappings":{
"lineshifts":{
"properties":{
"agence":{
"properties":{
"adresse":{
"type":"string"
},
"codePostal":{