Skip to content

Instantly share code, notes, and snippets.

@nrinaudo
nrinaudo / GenLenses.scala
Created February 7, 2019 20:37
Using optics to patch `Gen` instances
import monocle.macros._
import org.scalacheck._
import scalaz.scalacheck.ScalaCheckBinding._
// Deep hierarchy of product types.
case class Document(author: Author)
case class Author(firstName: String, lastName: String, city: City)
case class City(name: String, country: Country)
case class Country(name: String, continent: Continent)
case class Continent(name: String)
title layout
Start independent Futures outside of a for-comprehension
article

When working with independent [Futures][Future], make sure not to initialise them inside a for-comprehension.

Reason

For-comprehension will create a dependency between your [Futures][Future], turning your code synchronous behind your back.

object Test extends App {
val benchIterations = 10
val warmupIterations = 10
val repeat = 100000
val a = "foo"
val b = 123
def benchOne[U](a : => U): Long =
(0 to repeat).foldLeft(0L) { case (total, _) =>
@nrinaudo
nrinaudo / ReaderDemo.scala
Created August 13, 2018 21:17
Demo of how Reader works
import cats._
import cats.instances.all._
import cats.syntax.all._
final case class Configuration()
final case class Database()
final case class HttpServer()
object Test extends App {
@nrinaudo
nrinaudo / Or.scala
Created January 24, 2018 22:42
Semi-automatic derivation
import io.circe._
import io.circe.generic.semiauto._
import org.scalacheck._, derive._
sealed trait Or[+A, +B] extends Product with Serializable
final case class Left[A](a: A) extends Or[A, Nothing]
final case class Right[B](b: B) extends Or[Nothing, B]
object Or {
@nrinaudo
nrinaudo / Implicits.scala
Created July 1, 2017 14:04
Shows how implicit name conflicts even if types don't
object a1 {
implicit val foo: Int = 1
}
object a2 {
implicit val foo: String = "foo"
}
{
import a1._, a2._
@nrinaudo
nrinaudo / Demo.scala
Created March 5, 2017 20:18
Non-final case classes
case class Foo(a: Int)
class Bar(a: Int, b: Int) extends Foo(a)
// Returns false
new Bar(1, 2) == new Bar(2, 2)
// Returns true!!
new Bar(1, 2) == new Bar(1, 3)
import org.scalatest.FunSuite
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import java.text.SimpleDateFormat
import java.util.{Date, Locale}
import org.scalacheck._
class Repr extends FunSuite with GeneratorDrivenPropertyChecks {
implicit val cogenDate: Cogen[Date] = Cogen(_.getTime)
implicit val formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH)
@nrinaudo
nrinaudo / _config.yml
Created June 6, 2016 21:26
Jekyll sort on `sort` field issue
markdown: kramdown
highlighter: rouge
collections:
tut:
output: true
import ops1._
import ops2._
object ops1 {
implicit class TestOps(val str: String) {
def op1: String = str
}
}
object ops2 {