Skip to content

Instantly share code, notes, and snippets.

View mathieuancelin's full-sized avatar

Mathieu ANCELIN mathieuancelin

View GitHub Profile
@mathieuancelin
mathieuancelin / Lens.java
Last active March 7, 2023 02:23
Lenses with Java 8
package bar.foo.lenses;
import java.util.function.BiFunction;
import java.util.function.Function;
public class Lens<A, B> {
private final Function<A, B> getter;
private final BiFunction<A, B, A> setter;
@mathieuancelin
mathieuancelin / UniqueIds.java
Created January 28, 2015 13:41
Générateur d'id uniques ordonnés distribués (~1000 id générés par milliseconde par noeud)
public class KOrderedIdGenerator {
private final JsonConfiguration config = JsonConfiguration.fromBlob("...."); // à prendre dans la conf play
private final Long generatorId = config.getLong("generator.id", 1L); // propre à chaque noeud, d'où le k-ordered
private final Long minus = 1288834974657L;
private final AtomicLong counter = new AtomicLong(-1L);
private final AtomicLong lastTimestamp = new AtomicLong(-1L);
{{
@mathieuancelin
mathieuancelin / answer.md
Last active August 29, 2015 14:10
Issue with UI events when rendering React component inside WebComponent

And the aswer is ...

because of the way Shadow DOM is handling events, React does not attach its listener on the right container (it attaches the listener on the owner document of the container instead of the container itself).

A pending PR will fix this behavior : facebook/react#1877 by patching src/browser/ui/ReactDOMComponent.js like the following :

-    var doc = container.nodeType === ELEMENT_NODE_TYPE ?
- container.ownerDocument :
@mathieuancelin
mathieuancelin / Elem.js
Last active August 29, 2015 14:10
No more string concat. Need jquery, underscore.js and sugar.js
var Elem = Elem || {};
(function(exports) {
function styleToString(attrs) {
if (_.isUndefined(attrs)) return '';
var attrsArray = _.map(_.keys(attrs), function(key) {
var keyName = key.dasherize();
if (key === 'className') {
keyName = 'class';
}
@mathieuancelin
mathieuancelin / Component.js
Last active August 29, 2015 14:09
Experiments with mithril.js
var Component = Component || (function() {
return {
create: function(initial, init, render) {
var ctrl = {};
if (initial && !init && !render) {
ctrl = initial; // object oriented construction, other are closure oriented construction
} else if (initial && init && !render && _.isFunction(initial)) {
ctrl.initialState = initial();
ctrl.render = init;
} else if (initial && init && !render && !_.isFunction(initial)) {
import org.reactivecouchbase.ReactiveCouchbaseDriver
import scala.concurrent.Future
import play.api.libs.json._
object Application extends App {
val driver = ReactiveCouchbaseDriver()
val bucket = driver.bucket("default")
implicit val ec = ExecutionContext.fromExecutor(Executors.newSingleThreadExecutor())
implicit val keys = Seq("key-1", "key-2", "key-3")
package controllers
import env.Env
import play.api.libs.json.JsArray
import play.api.mvc._
import play.api.libs.concurrent.Execution.Implicits._
// here's where the injection takes place
class Application(env: Env) extends Controller {
import env._ // to avoid writing something like env.WS.url() or env.configuration.get...
package fwk
import scala.concurrent.{Future, ExecutionContext}
import play.api.libs.json.{JsUndefined, Json, JsObject, Format}
import play.modules.reactivemongo.json.collection.JSONCollection
import reactivemongo.bson.BSONObjectID
import reactivemongo.core.commands.LastError
import play.modules.reactivemongo.ReactiveMongoPlugin
import play.modules.reactivemongo.json.BSONFormats._
package controllers
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.json._
import play.api.libs.ws._
import play.api.mvc._
import play.api.libs._
import play.api.libs.iteratee._
import play.Play
import scala.util.control.NoStackTrace
package object FlatFuture {
case object EmptyOption extends RuntimeException("Current option is empty :'(") with NoStackTrace
implicit final class futureOfOptionToFuture[A](future: Future[Option[A]]) {
def flatten(implicit ec: ExecutionContext): Future[A] = {
future.flatMap {
case Some(something) => Future.successful(something)