Skip to content

Instantly share code, notes, and snippets.

View kubukoz's full-sized avatar
😱
I might take a week to respond. Or a month.

Jakub Kozłowski kubukoz

😱
I might take a week to respond. Or a month.
View GitHub Profile
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
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
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
def time(): Future[Long] = Future { System.currentTimeMillis() }
for {
t1 <- time()
x = calculateSomething(args)
t2 <- time()
} println(t2 - t1)
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
val time: Future[Long] = Future { System.currentTimeMillis() }
for {
t1 <- time
x = calculateSomething(args)
t2 <- time
} println(t2 - t1)
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
val time: Task[Long] = Task { System.currentTimeMillis() }
for {
t1 <- time
x = calculateSomething(args)
t2 <- time
} println(t2 - t1)
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
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
@kubukoz
kubukoz / post.sh
Last active May 16, 2018 13:16
the description for this gist
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"
}
@kubukoz
kubukoz / post.scala
Last active May 16, 2018 13:16
the description for this gist
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
}
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
def checkAndSave(event: PageViewed,
hostname: String,
clientIp: Option[String]): F[ValidatedNel[PVError, Unit]]
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
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
@kubukoz
kubukoz / post.scala
Created May 16, 2018 13:10
the description for this gist
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