Skip to content

Instantly share code, notes, and snippets.

View vdebergue's full-sized avatar

Vincent Debergue vdebergue

View GitHub Profile
@vdebergue
vdebergue / mixins.coffee
Created April 18, 2013 18:25
Example of how to do Mixins in Coffeescript similar to the Scala ones
class Mixable
@with: (classes...) ->
for clazz in classes
# add the class properties
for key, value of clazz
@[key] = value
# add the instance (prototype) properties
for key, value of clazz::
@::[key] = value
package utils
import javax.mail.internet.InternetAddress
import javax.mail.util.ByteArrayDataSource
import org.apache.commons.mail._
import scala.util.{Try, Failure, Success}
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
@vdebergue
vdebergue / MultiValidation.scala
Created June 12, 2014 15:51
validate two dependants fields with play framework
val locationReadsCreate = new Reads[(String, Option[String])] {
def reads(json: JsValue) = {
val subsidiary = (json \ "subsidiary").asOpt[String]
val location = (json \ "location").asOpt[String]
(subsidiary, location) match {
case (None, _) => JsError(Seq(JsPath() \ "subsidiary" -> Seq(ValidationError("error.path.missing"))))
case (Some("Autre"), None) => JsError(Seq(JsPath() \ "location" -> Seq(locationError)))
case (Some("Autre"), loc @ Some(_)) => JsSuccess(("Autre", loc))
case (Some(sub), loc @ Some(_)) => JsError(Seq(JsPath() \ "location" -> Seq(locationError)))
}
@vdebergue
vdebergue / save.js
Last active August 29, 2015 14:05
save to file
var blob = new Blob(['foo, bar, baz'], {type: 'text/plain'});
var uri = URL.createObjectURL(blob);
myAElement.download = 'myFileName.csv';
myAElement.href = uri;
// when clicking on myAElement, it will download to the browser a file containing your string
// see also: http://stackoverflow.com/a/19328891/1926897
@vdebergue
vdebergue / equation.scala
Last active August 29, 2015 14:07
Linear Equation Solver
object Demo extends App {
val formula = Add(Mult(X, Const(2)), Add(Const(-3), X))
val solutionDicho = Formula.solveForZeroDichotomy(formula)
val solution = Formula.solve0(formula)
println(s"$formula = 0 <=> X = $solution (dichotomy => $solutionDicho)")
val fdiff = Add( Add(Mult(X,X), Mult(X, Const(2))), Const(-2) )
val extremum = Formula.solve0(fdiff.diff)
println(s"Extremum of $fdiff => X = $extremum")
}
sealed trait IOToolResponse[+A] {
def flatMap[B](f: A => IOToolResponse[B]): IOToolResponse[B]
def map[B](f: A => B): IOToolResponse[B] = this.flatMap(a => IOToolResponse.unit(f(a)))
}
object IOToolResponse {
def unit[A](a: A): IOToolResponse[A] = Authenticated(a)
}
case class Authenticated[A](data: A) extends IOToolResponse[A] {
def flatMap[B](f: A => IOToolResponse[B]) = f(data)
@vdebergue
vdebergue / troll.js
Created October 2, 2014 12:45
Troll observe
Object.observe(model, function(changes){
changes.forEach(function(change) {
// Troll observe
console.log(change.type, change.name, change.oldValue);
model[change.name] = change.oldValue;
});
});
@vdebergue
vdebergue / application.scala
Last active August 29, 2015 14:08
Play Action composition
package controllers
import scala.concurrent.Future
import play.api._
import play.api.mvc._
object Application extends Controller {
def index = Action {
import subprocess
import atexit
proc = subprocess.Popen("ping google.com", shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
# register terminate function if python script is killed
atexit.register(proc.terminate)
for line in iter(proc.stdout.readline, ""):
print "got line: '%s'" % line
@vdebergue
vdebergue / ActionPartial.scala
Last active December 14, 2015 12:36
ActionPartial
package play.api.mvc
// needed to access protected method ActionRefiner#refine
import scala.concurrent.Future
import scala.language.higherKinds
trait ActionPartial[R[_], P[_]] extends ActionRefiner[R, P] { self =>
def partial[A]: PartialFunction[R[A], Future[Either[Result, P[A]]]]
def refine[A](request: R[A]): Future[Either[Result, P[A]]] = partial(request)