Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / extensions.scala
Created April 6, 2019 16:59
Type class constraint in extension classes.
trait Ops[A] {
def typeClassInstance: Show[A]
def self: A
def show: String = typeClassInstance.show(self)
}
trait ToShowOps {
implicit def toShow[A](target: A)(implicit tc: Show[A]): Ops[A] = new Ops[A] {
val self = target
val typeClassInstance = tc
@monadplus
monadplus / trampoline.scala
Created April 17, 2019 20:44
Trampoline in Scala
package trampoline
import scala.annotation.tailrec
/**
* Trampoline is a way to make non-tail recursive functions stack-safe
*
* Author: Rúnar Bjarnason
* Source: http://blog.higher-order.com/assets/trampolines.pdf
*/
@monadplus
monadplus / last.scala
Created May 9, 2019 06:50
Last element of a product
import shapeless._
import scala.annotation.implicitNotFound
trait DeepFn1[T] {
type Out
def apply(t: T): Out
}
@implicitNotFound("Implicit not found: Last[${L}]. ${L} is empty, so there is no last element.")
trait Last[L] extends DeepFn1[L]
@monadplus
monadplus / semaphore.scala
Created May 15, 2019 21:10
Exercise: implement a concurrent semaphore
abstract class Semaphore[F[_]] {
/**
* Returns the number of permits currently available. Always non-negative.
*
* May be out of date the instant after it is retrieved.
* Use `[[tryAcquire]]` or `[[tryAcquireN]]` if you wish to attempt an
* acquire, returning immediately if the current count is not high enough
* to satisfy the request.
*/
@monadplus
monadplus / tokenBucket.scala
Created May 15, 2019 22:09
Concurrent Tocket Bucket
package exercises
import cats._, cats.data._, cats.implicits._
import cats.effect._, cats.effect.implicits._, cats.effect.concurrent._
import fs2._
import scala.concurrent.duration._
import scala.util._
import java.util.concurrent.atomic._
import scala.concurrent._
@monadplus
monadplus / akkaActor.txt
Last active May 21, 2019 18:19
About (akka) Actor System model
Daniel Spiewak (2019-05-21)(https://gitter.im/typelevel/cats-effect?at=5ce427229404bf2aed85b08b):
Actors elevate the message and the message handlers to "first-class" status in the abstraction
But messages and handlers are almost never the hard things about a system.
The hard thing to understand and control is always the data flow, but actors make this actively
worse because the emergent properties become incredibly difficult to trace and reason about.
Actors are, to me, a failed abstraction in every case except when you're literally
designing a system which… sends messages. So a telecom. Applying them to a broader class
of things was a really neat idea, but frankly one which doesn't carry its weight in practice.
@monadplus
monadplus / bracketResource.md
Last active May 22, 2019 07:33
Bracket and resource.

Daniel Spiewak on gitter

Think about the closing action of a bracket. I'll use IO and Resource here as the archetypical examples, but Stream is also a very good exemplar, and obviously all of the cats-effect implementors have IO-like semantics in this regard.

So the closing action of a bracket with IO will be sequenced after the result is produced (or an error, or cancelation). Thus, imagine a chain like fa1 *> fa2 *> fa3. If fa1 is created by bracket, then the finalizer will be sequenced before fa2. Thus, associating to the left over bind.

@monadplus
monadplus / gc1.txt
Last active June 2, 2019 18:55
G1 Garbage Collector
GC1 is not the default GC in java_1.8
You can check it at:
`$ java -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -version`
To set G1GC:
$ java -XX:+PrintCommandLineFlags -XX:+PrintGCDetails -XX:+UseG1GC -version
@monadplus
monadplus / jmh.md
Last active June 12, 2019 15:03
JMH: how to
@monadplus
monadplus / fibers.md
Last active June 9, 2019 14:30
cats.effect.fiber: join and cancel

Join and Cancel on a fiber

When dealing with fibers you must be very careful not to cancel before joining, otherwise the join will leak.

Look at the following example:

for {
 fiber <- IO.sleep(2.seconds).start