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
def time() = System.currentTimeMillis() | |
val t1 = time() | |
val x = calculateSomething(args) | |
val t2 = time() | |
println(t2 - t1) //print the time it took for `x` to be computed |
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
def time(): Future[Long] = Future { System.currentTimeMillis() } | |
for { | |
t1 <- time() | |
x = calculateSomething(args) | |
t2 <- time() | |
} println(t2 - t1) |
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
val time: Future[Long] = Future { System.currentTimeMillis() } | |
for { | |
t1 <- time | |
x = calculateSomething(args) | |
t2 <- time | |
} println(t2 - t1) |
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
val time: Task[Long] = Task { System.currentTimeMillis() } | |
for { | |
t1 <- time | |
x = calculateSomething(args) | |
t2 <- time | |
} println(t2 - t1) |
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
class Calc[F[_] : Sync] { | |
def calculateSomething(args: List[String]): Int = ??? | |
def measureCalculation(args: List[String]): F[(Int, Long)] = { | |
val time: F[Long] = Sync[F].delay { System.currentTimeMillis() } | |
for { | |
t1 <- time | |
x = calculateSomething(args) | |
t2 <- time |
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
POST /views HTTP/1.1 | |
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJzY2FsYS1sYW5nLm9yZyIsImlhdCI6MTUxNjIzOTAyMn0.luv48L_lo4OkyGQIKiKN7nFK2Wypr1Hd2lwxTMPybUU | |
{ | |
displayed_at: "2018-05-16T09:00.000+01:00", | |
path: "/articles/scala-3-delayed.html?source=twitter&tracking_id=6d9386c589f1e" | |
} |
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
val viewService: HttpService[F] = HttpService { | |
case req @ (POST -> Root / "views") => | |
for { | |
viewed <- req.decodeJson[PageViewed] | |
ip = extractClientIp(req) | |
result <- pageViewService.checkAndSave(viewed, ip) | |
response <- validatedToJson(result, successResponse = Created) | |
} yield response | |
} |
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
def checkAndSave(event: PageViewed, | |
hostname: String, | |
clientIp: Option[String]): F[ValidatedNel[PVError, Unit]] |
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 PVError extends Product with Serializable | |
object PVError { | |
case object NoIp extends PVError | |
case object AlreadyTracked extends PVError | |
case object PageNotFound extends PVError | |
case object InvalidTimestamp extends PVError | |
//helper functions to upcast easily | |
val noIp: PVError = NoIp |
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
def checkAndSave(event: PageViewed, | |
hostname: String, | |
clientIp: Option[String]): F[ValidatedNel[PVError, Unit]] = { | |
val normalizedPath = normalize(event.path) | |
val uuidF: F[UUID] = Sync[F].delay { UUID.randomUUID() } | |
val currentTimeF: F[OffsetDateTime] = getCurrentTimeF | |
val ipValidationF: F[ValidatedNel[PVError, String]] = | |
clientIp |