Skip to content

Instantly share code, notes, and snippets.

Daniel Spiewak djspiewak

Block or report user

Report or block djspiewak

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
View ConcurrentLaws.scala
/*
* Copyright 2020 Daniel Spiewak
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View evaluator-architecture.md

Query Evaluator Architectural Abstractions

The architecture of all query evaluators can be described in the following fashion. The types in each case may be more or less precise, representations may vary and be more or less first-class, constraints may be statically or dynamically represented, but to a first-order, the architectures all look like this.

Overall Architecture

At the highest level, we can consider a query evaluator to be a function:

type Evaluator = Query => F[DataOut]
View kleisli-violates-monad-laws.scala
import cats.{Id, MonadError}
import cats.data.Kleisli
import cats.implicits._
import cats.free.FreeT
type F[A] = Kleisli[FreeT[Id, Either[Int, ?], ?], Unit, A]
val F = MonadError[F, Int]
def run[A](fa: F[A]): Either[Int, A] =
fa.run(()).runM(ft => Right(ft))
View wat2.txt
scala> run(F.handleErrorWith(F.map(fa)(_.asRight[Int]))(e => F.pure(e.asLeft[String])))
res2: ce3.ExitCase[Option,Int,Either[Int,String]] = Errored(42)
scala> run(F.handleErrorWith(fa.map(_.asRight[Int]))(e => F.pure(e.asLeft[String])))
handling 42
res3: ce3.ExitCase[Option,Int,Either[Int,String]] = Completed(Some(Left(42)))
View wat.txt
scala> run(F.raiseError[String](42).map(_.asRight[Int]).handleErrorWith(_.asLeft[String].pure[PureConc[Int, ?]]))
res11: ce3.ExitCase[Option,Int,Either[Int,String]] = Completed(Some(Left(42)))
scala> run(F.attempt(F.raiseError[String](42)))
res12: ce3.ExitCase[Option,Int,Either[Int,String]] = Errored(42)
View ExitCase.scala
/*
* Copyright 2019 Daniel Spiewak
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View extension.scala
class Foo {
def bar(i: Int): String = i.toString
}
object Foo {
implicit class Syntax(val self: Foo) extends AnyVal {
def baz(b: Boolean): String = b.toString
}
}
View scala-project-bootstrap.md

Easy Scala Publication

The following describes how you can publish artifacts for any sbt project using the GitHub Package Registry and the sbt-github-packages plugin.

Step 1: Create a GitHub Token

In your GitHub account, go to Settings > Developer settings > Personal access tokens, then click on Generate new token (or click here). Fill in some sort of meaningful name (I chose Dev) and click on the write:packages checkbox:

the new personal access token page with the above steps having been followed

View RateLimiter.scala
final class RateLimiter[F[_]: Sync: Timer] private () extends (A => F[Unit]) {
// make these things clustering-aware
private val configs: TrieMap[Key[_], Config] = new TrieMap[Key[_], Config](RateLimiter.toHashing[Key[_]], RateLimiter.toEqiv[Key[_]])
private val states: TrieMap[Key[_], Ref[F, State]] = new TrieMap[Key[_], Ref[F, State]](RateLimiter.toHashing[Key[_]], RateLimiter.toEqiv[Key[_]])
def apply[A: Hash](key: A, max: Int, caution: Double, window: FiniteDuration): F[F[Unit]] =
for {
config <- Sync[F] delay {
val c = Config(max, caution, window)
configs.putIfAbsent(key, c).getOrElse(c)
View RateLimiter.scala
final class RateLimiter[F[_]: Sync: Timer] private (chunks: Int, window: FiniteDuration, now: FiniteDuration, caution: Double) {
import RateLimiter.nowF
private val state = Ref.of[F, (Int, FiniteDuration)]((0, now))
// implements hard limiting, rather than graceful and cooperative backoff
def limiter[A]: Pipe[F, A, A] = { in =>
val actualKey = (key, window)
val cap = math.max((chunks * caution).toInt, 1)
You can’t perform that action at this time.