Skip to content

Instantly share code, notes, and snippets.

@julienrf
julienrf / collections.scala
Created September 13, 2017 16:36
Signature of new operations
trait Iterable[A] {
// https://github.com/scala/collection-strawman/issues/221
/**
* Returns a collection formed by the result of applying a function to each pair of corresponding elements
* from this collection and another collection. It is semantically equivalent to `(xs zip ys) map f`.
* If one of the two collections is longer than the other, its remaining elements are ignored.
*/
def zipWith[B, C](that: Iterable[B])(f: (A, B) => C): Iterable[C]
@julienrf
julienrf / code.scala
Created January 15, 2016 15:10
dependent types + higher-kinded types + implicits
object OK {
trait `*`[A]
trait Bar {
type Qux
implicit val `*`: `*`[Qux]
}
def foo(bar: Bar): `*`[bar.Qux] = {
import bar._
@julienrf
julienrf / Values.scala
Last active November 18, 2015 08:14
Yet another Scala enumeration-like tool powered by shapeless. Inspired by https://github.com/milessabin/shapeless/blob/master/examples/src/main/scala/shapeless/examples/enum.scala
/** A typeclass giving the values of an enumeration */
@implicitNotFound("Unable to find values of ${A}. Make sure it is a sealed trait and is only extended by case objects.")
class Values[A](val values: Set[A])
/**
* The companion object contains the machinery to automatically derive the values of a sealed trait
* extended by case objects only.
*
* Basically, the derivation process is the following:
* - we are given a sort of list containing the types of the case objects that extend a sealed trait `A` ;
@julienrf
julienrf / SurgicalUpdate.scala
Last active September 29, 2021 21:38
Surgical updates in Slick
import slick.lifted.TupleShape
import slick.driver.H2Driver.api._
import scala.concurrent.Await
import scala.concurrent.duration.DurationInt
import scala.concurrent.ExecutionContext.Implicits.global
object Example {
val db = Database.forConfig("db")
@julienrf
julienrf / Controller.scala
Created July 5, 2015 08:08
Handling Etags with Play
def taggedResult(request: RequestHeader, tag: String)(result: => Result): Result =
request.headers.get(IF_NONE_MATCH) match {
case Some(t) if t == tag => NotModified
case _ => result.withHeaders(ETAG -> tag)
}
// Example of use:
val javascriptRoutes = {
val router =
JavaScriptReverseRouter("routes", None, hostname,
@julienrf
julienrf / bar.js
Created August 7, 2014 07:02
Dependency injection in JavaScript
/* A basic implementation of bar */
define(function () {
return {
plop: function () { alert('bar implementation'); }
}
});
@julienrf
julienrf / ws.scala
Last active August 29, 2015 14:04
Using the Play HTTP client without the hassle of starting a Play application
val client = new NingWSClient(new NingAsyncHttpClientConfigBuilder(DefaultWSClientConfig()).build())
client.url("…").get().foreach { response =>
client.close()
}
@julienrf
julienrf / Application.java
Last active August 29, 2015 14:00
Content negotiation in Java with Play framework
package controllers;
import play.mvc.Controller;
import play.libs.Json;
import static controllers.Render.*;
import static play.mvc.Http.MimeTypes;
public class Application extends Controller {
@julienrf
julienrf / Main.scala
Last active December 27, 2015 16:09
Play with control
object Main extends App {
// You can retrieve the application configuration from any place
val configuration = Configuration("conf/my-app.conf")
// You instantiate your controllers with their required configuration
val application = new MyAbstractController(configuration) with SomeEventualMixin
// You pass your controllers as parameters to a router factory
val router = Router("conf/routes", application)
@julienrf
julienrf / unsoundness.dart
Last active December 14, 2015 04:49
Contravariance is useful.
// Some data type definitions: the usual (and boring) zoo class hierarchy
abstract class Animal { }
abstract class Mammal extends Animal { }
class Giraffe extends Mammal { }
class Zebra extends Mammal {
num stripeCount;
Zebra(num stripeCount) {