Skip to content

Instantly share code, notes, and snippets.

View loicdescotte's full-sized avatar

Loïc Descotte loicdescotte

View GitHub Profile
class PrintService {
def print = println("I'm a real service")
}
trait Services {
val printService = new PrintService()
// put your other services here
}
//for example, a Play controller
var names = ['joe', 'bob'];
Object.observe(names, function(changes){
changes.forEach(function(change) {
console.log(change.type, change.name)
});
console.log("names",names);
});
@loicdescotte
loicdescotte / PlayAkkaSourceQueue.scala
Last active April 26, 2024 07:30
Play and Akka Streams source queue
class Application @Inject() (implicit actorSystem: ActorSystem, exec: ExecutionContext) extends Controller {
implicit val materializer = ActorMaterializer()
val Tick = "tick"
class TickActor(queue: SourceQueue[String]) extends Actor {
def receive = {
case Tick => queue.offer("tack")
}

How to retrieve List("a", "b") from a list of optional values

A very common use case when dealing with options...

Scala

val values = List(Some("a"), None, Some("b")).flatten

Java

@loicdescotte
loicdescotte / composeFutureOfNullable.kt
Last active May 16, 2016 19:32
Kotlin : compose futures of nullables
@Test
fun combineNullable() {
fun giveInt(x: Int):Int? = x+1
fun giveInt2(x: Int):Int? = x+2
fun combine(x: Int): Int? = giveInt(x)?.let { giveInt2(it) }
assertEquals(combine(1),4)
}
@loicdescotte
loicdescotte / structural_typing.ts
Last active June 17, 2018 07:38
typescript strucural typing
interface HasScore {
score: number;
// also works with functions instead of properties
}
let player = { score: 0 };
function addPointsToScore(player: HasScore, points: number): void {
player.score += points;
}
@loicdescotte
loicdescotte / implicitClassExtendingGenericTrait.scala
Last active June 18, 2018 08:42
Scala : make a class implement a trait
//example with Generic trait
trait Summable[T] {
def sumWith(other: T): T
}
implicit class StringSummable(s: String) extends Summable[String]{
def sumWith(other: String): String = s + other
}
@loicdescotte
loicdescotte / typeclassExample.scala
Last active September 27, 2018 11:29
Scala Typeclass Example
//typeclass example
//Note : List already has a sum method working the same way
//List(1,2,3).sum returns 6
//but this example is simpler
// Monoid = summable + neutral element
trait Monoid[T] {
def combine(a: T, b: T): T
def empty: T
@loicdescotte
loicdescotte / TaglessMonadError.scala
Last active July 10, 2018 13:17
Tagless final with MonadError
import cats._
import cats.implicits._
def loadResult[F[_]](implicit F: MonadError[F, Throwable]): F[String] = {
val resultEitherF: F[Either[Throwable, String]] = ???
val resultF: F[String] = resultEitherF.flatMap(F.fromEither)
resultF.recoverWith{
case error => F.pure(error.getMessage)
}
}
@loicdescotte
loicdescotte / pipe.scala
Created July 13, 2018 14:43
Scala pipe function operator
implicit class AnyEx[T](val v: T) extends AnyVal {
def |>[U](f: T ⇒ U): U = f(v)
}
//example
def f(l: List[Int]) = l.filter(x => x >1)
def g(l: List[Int]) = l.filter(x => x <3)
List(1,2,3) |> f |> g //List(2)