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
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 |
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 ipValidationF: F[ValidatedNel[PVError, String]] = | |
clientIp | |
.toValidNel(PVError.noIp) | |
.traverse(checkIp(_, hostname, normalizedPath)) | |
.map(_.andThen(identity) |
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 pathValidationF: F[ValidatedNel[PVError, String]] = | |
pageExists(hostname, normalizedPath).map( | |
Validated.condNel(_, normalizedPath, PVError.pageNotFound)) |
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
(uuidF, currentTimeF, ipValidationF, pathValidationF).traverseN { | |
(uuid, currentTime, ipValidation, pathValidation) => ... | |
} |
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 timestampV: ValidatedNel[PVError, OffsetDateTime] = event | |
.displayedAt | |
.valid | |
.ensure(PVError.invalidTimestamp)(_.isBefore(currentTime)) | |
.toValidatedNel | |
( | |
uuid.valid, | |
pathValidation, | |
timestampV, |