Skip to content

Instantly share code, notes, and snippets.

View julien-truffaut's full-sized avatar

Julien Truffaut julien-truffaut

View GitHub Profile
package monocle
import scala.concurrent.Future
case class State[S, A](f: S => (S, A)) {
def apply(s: S): (S, A) = f(s)
def map[B](g: A => B): State[S, B] = State[S, B] { s =>
@julien-truffaut
julien-truffaut / MonocleSymbols.md
Last active August 29, 2015 14:05
Monocle symbols
Method Current Aliases New Aliases
composeTraversal |->> ^|->>
composeOptional |-? ^|-?
composePrism <-? ^<-?
composeLens |-> ^|->
composeIso <-> ^<->
applyTraversal |->> ^^|->>
applyOptional |-? ^^|-?
applyPrism <-? ^^<-?
@julien-truffaut
julien-truffaut / composeExample.scala
Last active August 29, 2015 14:05
Multi compose
import monocle._ Monocle._
case class Order(name: String, articles: List[Article])
case class Article(properties: Map[String, String])
val order = Order("name", List(Article(Map("name" -> "door", "colour" -> "red")), Article(Map("name" -> "shoes"))))
val _articles = Lenser[Order](_.articles)
val _properties = SimpleIso[Article, Map[String, String]](_.properties, Article(_))
@julien-truffaut
julien-truffaut / TcpServerApp.scala
Last active August 29, 2015 14:11
How to run a Tcp server in the background
import java.net.InetSocketAddress
import scalaz.concurrent.Task
import scalaz.stream.tcp
import java.util.concurrent.Executors
object TcpServerApp extends App {
val executor = Executors.newSingleThreadExecutor
implicit val S = scalaz.concurrent.Strategy.DefaultStrategy
implicit val AG = tcp.DefaultAsynchronousChannelGroup
sealed trait Foo
case class A(i: Int) extends Foo
case class B(x: Double, y: Double) extends Foo
case object C extends Foo
// 1st option:
GenPrism[Foo, A]: Prism[Foo, A]
@julien-truffaut
julien-truffaut / typeclass.scala
Created January 26, 2015 16:53
type class without inheritance
abstract class Equal[T]{
def eq(v1: T, v2: T): Boolean
}
object Equal{
def apply[T](implicit ev: Equal[T]): Equal[T] = ev
def equalA[T]: Equal[T] = new Equal[T] {
def eq(v1: T, v2: T): Boolean = v1 == v2
}
@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.
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 / 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
@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