Method | Current Aliases | New Aliases |
---|---|---|
composeTraversal | |->> |
^|->> |
composeOptional | |-? |
^|-? |
composePrism | <-? |
^<-? |
composeLens | |-> |
^|-> |
composeIso | <-> |
^<-> |
applyTraversal | |->> |
^^|->> |
applyOptional | |-? |
^^|-? |
applyPrism | <-? |
^^<-? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(_)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package monocle.example | |
import monocle.Prism | |
import monocle.macros.Lenses | |
import monocle.function._ | |
import monocle.std.map._ | |
import monocle.std.string._ | |
import scalaz.Equal |
OlderNewer