Skip to content

Instantly share code, notes, and snippets.

@densh
densh / scoped-implicit-lifetimes.md
Last active October 18, 2020 17:02
Scoped Implicit Lifetimes

Scoped Implicit Lifetimes

All things considered, our experience in Scala Native has shown that resource management in Scala is way harder than it should be. This gist presents a simple design pattern that makes it resource management absolutely hassle-free: scoped implicit lifetimes.

The main idea behind it is to encode resource lifetimes through a concept of an implicit scope. Scopes are necessary to acquire resources. They are responsible for disposal of the resources once the evaluation exits the

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.
scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
scala> scala.concurrent.Future( { println("foo"); 4 })
foo
res0: scala.concurrent.Future[Int] = Success(4)
abstract class ExtFn(m: AnyRef) {
type Out[A]
def apply: Out[m.type]
}
@noelwelsh
noelwelsh / gcounter.scala
Created July 27, 2016 16:01
GCounter CRDT implementation in Scala
import scalaz.Monoid
import scalaz.syntax.monoid._
import scalaz.syntax.traverse._
import scalaz.std.map._
import scalaz.std.anyVal._
import scalaz.std.string._
import scalaz.std.iterable._
import scala.language.higherKinds
/*
/* A Bounded Semi-lattice is a Monoid that is commutative and idempotent */
@paulp
paulp / omg.scala
Created July 19, 2016 18:31
scala is bad.
object Main {
val s1 = Set[BigDecimal](-4.872401723124452E9, 0, 1.1752011936438014, 8.696374707602505E17, -1.1752011936438014)
val s2 = Set[Double] (-4.872401723124452E9, 0, 1.1752011936438014, 8.696374707602505E17, -1.1752011936438014)
val s3: Set[BigDecimal] = s2 map (d => BigDecimal(d))
def main(args: Array[String]): Unit = {
/* false */ println(s1 == s2)
/* true */ println(s1 == s3)
/* false */ println(s2 == s3)
/* true */ println(s1.toSeq.sorted == s2.toSeq.sorted)
@OlivierBlanvillain
OlivierBlanvillain / TypeLevelBacktrack.scala
Last active June 6, 2020 23:52
Example of a workaround for the absence of backtracking in implicit resolution. Original problem statement: https://gist.github.com/atamborrino/daa451aea542e912c2d6
import shapeless.{HList, HNil, ::}
import shapeless.ops.hlist.{Selector, Prepend}
import shapeless.test.illTyped
object TypeLevelBacktrack extends App {
/** [[Parent]] / [[Child]] relationship, father side. */
trait FatherOf[Parent, Child]
/** [[Parent]] / [[Child]] relationship, mother side */
trait MotherOf[Parent, Child]
@etorreborre
etorreborre / gist:7126b0804f5d4e68fd4ad373931950ac
Created June 7, 2016 15:17
Transform your browser into a text editor, paste this in the navigation bar
data:text/html, <html contenteditable style="font-family:Courier New, Sans Serif;font-size:90px">
@milessabin
milessabin / singleton-only.scala
Created June 6, 2016 11:04
Scala type which can only be extended by an object, not by a non-abstract type ...
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
@reactormonk
reactormonk / Symbols.md
Last active November 19, 2021 14:06
Scalaz Symbol Guide
Symbol Explanation Hint
\/ Right-leaning Either Split ways, go one way or the other
-\/ Left value of \/ - is on the left side
\/- Right value of \/ - is on the right side
>>= flatMap shove result into
>> flatMap(_ => ..) shove into, but ignore the result
|@| Applicatives into Tuple Scream operator
|+| Append via Monoid + was taken
`> ` fa.map(_ =&gt; b)
@etorreborre
etorreborre / dynamic_typeclass.scala
Created June 1, 2016 06:47
"Dynamic" typeclass instance
import cats._, data._
object Test {
trait Decoder[T] {
def decode(s: String): String Xor T
}
case class Password(pw: String)
case class User(name: String)