Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lens の実用例
import sbt._
name := "monocle-template"
scalaVersion := "2.11.8"
licenses := Seq("MIT License" -> url("http://opensource.org/licenses/mit"))
scalacOptions ++= Seq(
"-deprecation",
"-feature",
"-Xlint",
"-Ywarn-dead-code",
"-language:higherKinds",
"-language:postfixOps",
"-language:implicitConversions"
)
resolvers += Resolver.sonatypeRepo("releases")
resolvers += Resolver.sonatypeRepo("snapshots")
val libraryVersion = "1.2.2"
libraryDependencies ++= Seq(
"com.github.julien-truffaut" %% "monocle-core" % libraryVersion,
"com.github.julien-truffaut" %% "monocle-generic" % libraryVersion,
"com.github.julien-truffaut" %% "monocle-macro" % libraryVersion,
"com.github.julien-truffaut" %% "monocle-state" % libraryVersion,
"com.github.julien-truffaut" %% "monocle-law" % libraryVersion % "test"
)
// for @Lenses macro support
addCompilerPlugin("org.scalamacros" %% "paradise" % "2.1.0" cross CrossVersion.full)
val data = CampaignStatus(
999L,
MonthlyStatus(
Win(CPM(2000)),
Bid(CPM(2000))),
DailyStatus(
Win(CPM(1000)),
Bid(CPM(1000))),
List("http://example.com"),
new Date()
)
val data1 = newCampaignStatus(999L, new Date())
.setMonthlyWinStatus(1000)
.setDailyWinStatus(100)
.addUrl("http://example.com")
.addUrl("http://example.org")
val data2 = data1
.modifyMonthlyWinStatus(_ * 2)
.modifyDailyWinStatus(_ * 2)
val data3 = newCampaignStatus(999L, new Date())
.dailyBidStatusLens.set(100)
.dailyBidStatusLens.modify(_ + 200)
import java.util.Date
import monocle.Lens
import monocle.macros.GenLens
import monocle.function.all._
import monocle.syntax.apply._
object CampaignStatusBuilder {
def newCampaignStatus(id: Long, readAt: Date): CampaignStatus =
CampaignStatus(
id,
MonthlyStatus(
Win(CPM(0)),
Bid(CPM(0))),
DailyStatus(
Win(CPM(0)),
Bid(CPM(0))),
List.empty[String],
readAt
)
implicit def toCampaignStatusBuilder(status: CampaignStatus): CampaignStatusBuilder =
new CampaignStatusBuilder(status)
lazy val _dailyStatus = GenLens[CampaignStatus](_.dailyStatus)
lazy val _bid = GenLens[DailyStatus](_.bid)
lazy val _cpm = GenLens[Bid](_.cpm)
lazy val _value = GenLens[CPM](_.value)
}
class CampaignStatusBuilder(status: CampaignStatus) {
import CampaignStatusBuilder._
// === copy method
def modifyMonthlyWinStatus(f: Long => Long): CampaignStatus =
status.copy(
monthlyStatus = status.monthlyStatus.copy(
win = status.monthlyStatus.win.copy(
cpm = status.monthlyStatus.win.cpm.copy(
value = f(status.monthlyStatus.win.cpm.value)
)
)
)
)
def setMonthlyWinStatus(v: Long): CampaignStatus =
modifyMonthlyWinStatus(_ => v)
// === lens
def modifyDailyWinStatus(f: Long => Long): CampaignStatus =
GenLens[CampaignStatus](_.dailyStatus.win.cpm.value).modify(f)(status)
def setDailyWinStatus(v: Long): CampaignStatus =
status &|-> GenLens[CampaignStatus](_.dailyStatus.win.cpm.value) set v
def addUrl(url: String): CampaignStatus =
status &|-> GenLens[CampaignStatus](_.urls) modify (url :: _)
def dailyBidStatusLens =
status &|-> _dailyStatus ^|-> _bid ^|-> _cpm ^|-> _value
}
import java.util.Date
case class CampaignStatus(
id: Long,
monthlyStatus: MonthlyStatus,
dailyStatus: DailyStatus,
urls: List[String],
readAt: Date
)
case class MonthlyStatus(win: Win, bid: Bid)
case class DailyStatus(win: Win, bid: Bid)
case class Win(cpm: CPM)
case class Bid(cpm: CPM)
case class CPM(value: Long)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment