Skip to content

Instantly share code, notes, and snippets.

Jakub Kozłowski kubukoz

Block or report user

Report or block kubukoz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@kubukoz
kubukoz / Playground.scala
Created Aug 30, 2019
TCP server dumping input to stdout with fs2
View Playground.scala
import cats.effect._
import java.net.InetSocketAddress
import java.nio.channels.AsynchronousChannelGroup
import java.util.concurrent.Executors
import fs2.Stream
import cats.implicits._
@kubukoz
kubukoz / shits.scala
Created Apr 7, 2019
Repeated calls to a function with one constant argument in Scala.
View shits.scala
case class Event()
def doShit(a: String, b: Int): Event => Boolean = e => true
def doShits: List[Boolean] = {
val shits = List(
doShit("foo", 42),
doShit("ffafoo", 4),
doShit("fofasdfo", 2),
@kubukoz
kubukoz / SlickRunIO.scala
Last active Jan 14, 2019 — forked from Daenyth/SlickRunIO.scala
SlickRunIO.scala
View SlickRunIO.scala
trait SlickRunIOImplicits { self: API =>
implicit class SlickRunIO(db: Database) {
/** Return an effect that runs a DB action. */
def runIO[R](a: DBIOAction[R, NoStream, Nothing])(implicit cs: ContextShift[IO]): IO[R] =
IO.fromFuture(IO(db.run(a))).guarantee(cs.shift)
/** Return an effect that runs a DB action with timing metrics collected */
def runIO[R](a: DBIOAction[R, NoStream, Nothing], timerName: String)(
implicit metric: MetricsF
@kubukoz
kubukoz / HasKF.scala
Last active Jun 9, 2018
One of the many workarounds for "implicit type parameters" in Scala
View HasKF.scala
class HasKF[F[_]] private {
type KF[A, B] = Kleisli[F, A, B]
}
object HasKF {
private val s: HasKF[Id] = new HasKF[Id]
def apply[F[_]]: HasKF[F] = s.asInstanceOf[HasKF[F]]
}
class AService[F[_] : Applicative] {
View inlining-pitfall.scala
def sideEffecting(): Map[String, List[String]] = { println("bazinga"); Map("a" -> List("a", "b")) }
//doesn't print anything here
val x1: String => List[String] = sideEffecting().getOrElse(_, Nil)
//but it'll print bazinga any time you call one of these:
x1("a") //here
x1("a") //also here
x1("b") //here too
@kubukoz
kubukoz / post.scala
Created May 16, 2018
the description for this gist
View post.scala
def persist(pageView: PageView): F[Unit] =
sql"""
insert into page_views(id, path, displayed_at, hostname, client_ip) value
(${pageView.id}, ${pageView.path}, ${pageView.displayedAt}, ${pageView.hostname}, ${pageView.clientIp})
""".update.run.transact(transactor).void
@kubukoz
kubukoz / post.json
Created May 16, 2018
the description for this gist
View post.json
| Column | Type |
------------------------------------------+
| id | uuid |
| path | text |
| displayed_at | timestamp with time zone |
| hostname | text |
| client_ip | text |
@kubukoz
kubukoz / post.scala
Created May 16, 2018
the description for this gist
View post.scala
(uuidF, currentTimeF, ipValidationF, pathValidationF).traverseN {
(uuid, currentTime, ipValidation, pathValidation) =>
val timestampV = event
.displayedAt
.valid
.ensure(PVError.invalidTimestamp)(_.isBefore(currentTime))
.toValidatedNel
(
uuid.valid,
@kubukoz
kubukoz / post.scala
Created May 16, 2018
the description for this gist
View post.scala
val timestampV: ValidatedNel[PVError, OffsetDateTime] = event
.displayedAt
.valid
.ensure(PVError.invalidTimestamp)(_.isBefore(currentTime))
.toValidatedNel
(
uuid.valid,
pathValidation,
timestampV,
@kubukoz
kubukoz / post.scala
Created May 16, 2018
the description for this gist
View post.scala
(uuidF, currentTimeF, ipValidationF, pathValidationF).traverseN {
(uuid, currentTime, ipValidation, pathValidation) => ...
}
You can’t perform that action at this time.