Skip to content

Instantly share code, notes, and snippets.

View julien-truffaut's full-sized avatar

Julien Truffaut julien-truffaut

View GitHub Profile
import io.circe.Json
import io.circe.optics.all._
import io.circe.parse.parse
import monocle.Iso
import monocle.function.Plated
import monocle.function.all._
object PlatedExample extends App {
val json =
"""{
@julien-truffaut
julien-truffaut / Monocle 1.2.0-M2.md
Last active November 22, 2015 20:42
WIP - Release note for Monocle 1.2.0-M2

1.2.0-M2

Thanks to all the 14 contributors since 1.2.0-M1

Addition

  • add only Prism to match a single value see
  • add below Prism to lift a Prism in a Traverse see
  • add length for Fold and Traversal #236 (thanks to aoiroaoino)
  • add optics for scalaz.Either3 #242 (thanks to aoiroaoino)
import remotely.{Type, Field, Protocol}
import remotely.codecs._
import remotely._
import remotely.transport.netty.NettyTransport
object protocol {
val definition = Protocol.empty
.codec[Int]
.specify1("factorial", Field.strict[Int]("in"), Type[Int])
}
import monocle.MonocleSuite
import monocle.generic.internal.TupleGeneric
class ProductExample extends MonocleSuite {
case class Example(i : Int, s: String, b: Boolean)
implicit val gen = TupleGeneric[Example]
test("productToTuple creates an Iso between a Product and a Tuple") {
@julien-truffaut
julien-truffaut / Van_Laarhoven_Lens.md
Last active October 11, 2015 21:15
Abstract for Scala Matsuri 2016

Van Laarhoven Lens

A Lens is a functional concept that permits to zoom in a data structure. A key point of Lenses is that they compose together such as you can drill deeper and deeper in your data. Once you arrive at the level you are interested in, you can either extract or modify (even effectfully!) the value you targeted.

In this presentation, I would like to go through a specific Lens implementation known as Van Laarhoven Lenses. This implementation is particularly interesting because it defines Lenses as a unique function! The game is to find out how can we derive all Lens methods from this simple function.

@julien-truffaut
julien-truffaut / build.sbt
Last active September 9, 2015 11:53
gatling custom test config
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"io.gatling.highcharts" % "gatling-charts-highcharts" % "2.1.7" % "test",
"io.gatling" % "gatling-test-framework" % "2.1.7" % "test"
)
lazy val LoadTest = config("load") extend Test
lazy val LoadTest = config("load") extend Test
@julien-truffaut
julien-truffaut / HttpOptics.scala
Last active August 29, 2015 14:27
Optics for Http request
package monocle.example
import monocle.Prism
import monocle.macros.Lenses
import monocle.function._
import monocle.std.map._
import monocle.std.string._
import scalaz.Equal
@julien-truffaut
julien-truffaut / ClientServer.scala
Last active August 29, 2015 14:24
Client error
import org.http4s.Uri.{RegName, Authority, IPv4}
import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.dsl._
import org.http4s.server.HttpService
import org.http4s.server.blaze.BlazeBuilder
import org.http4s.{Request, Response, Uri}
import scala.concurrent.duration.Duration
import monocle.function._
import monocle.std.list._
import monocle.Lens
import monocle.syntax.apply._
case class Person(name: String, age: Int)
val ps = List(Person("John", 25), Person("Paul", 32))
val _age = Lens[Person, Int](_.age)(a => p => p.copy(age = a))
@julien-truffaut
julien-truffaut / abstract_beyond_scala_lenses
Created February 26, 2015 16:10
Beyond Scala Lens abstract
Beyond Scala Lens
A Lens is a functional concept which solves a very common problem: how to update a complex immutable structure. This is probably the reason why Lenses are relatively well known in functional programming languages such as haskell or scala. However, there are far less resources
available on the generalisation of Lenses known as "optics". Today, I would like to go through a few of these optics namely Iso, Prism and Optional by showing how they relate to each other as well as how to use optics in a day to day programming job.
Julien Truffaut is the author of Monocle, an optics library for scala mainly inspired by haskell lens. He also partcipates in a few open source project such as scalaz, cats or http4s.