Skip to content

Instantly share code, notes, and snippets.

View nightscape's full-sized avatar

Martin Mauch nightscape

  • Regensburg, Germany
  • 06:51 (UTC +02:00)
View GitHub Profile
@nightscape
nightscape / Http4sStreamStringUntilResponseIsReady.scala
Last active April 22, 2020 14:31
Http4s: Stream a constant String until the response is ready (might be useful for preventing connection to be closed)
def constantStringThenContent[F[_] : Timer : Concurrent, A](constantString: String, result: F[A])(
implicit W: EntityEncoder[F, A]
): Response[F] = {
val resultStream = Stream.eval(result)
val preStream = EntityEncoder.stringEncoder[F].toEntity(constantString).body
val composedStream = (Stream[F, Option[A]](None, None, None) ++ resultStream.map(Option.apply))
.switchMap[F, Byte] {
case None =>
fs2.Stream.awakeEvery[F](10.second).flatMap(_ => preStream)
case Some(v) => W.toEntity(v).body
@nightscape
nightscape / fields.sc
Created April 3, 2020 15:48 — forked from danslapman/fields.sc
Get field names of case class using shapeless
import $ivy.`com.chuusai::shapeless:2.3.3`
import shapeless._
trait Fields[T] {
def fields: List[String]
override def toString: String = fields.mkString(", ")
}
object Fields extends LabelledProductTypeClassCompanion[Fields] {
def apply[T](fs: List[String]): Fields[T] = new Fields[T] {
override def fields: List[String] = fs
case class MapIncluding[K](keys: Seq[K], optionally: Seq[K] = Seq()) {
def unapply[V](m: Map[K, V]): Option[(Seq[V], Seq[Option[V]])] =
if (keys.forall(m.contains)) {
Some((keys.map(m), optionally.map(m.get)))
} else {
None
}
}
sealed trait MapRequirements[K] {
type ResultType[V]
@nightscape
nightscape / remove_github_notifications.user.js
Created March 19, 2019 09:14
Removes Github notifications (to reduce distraction)
// ==UserScript==
// @name Remove Github notifications
// @namespace Violentmonkey Scripts
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @match https://github.com/*
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
$(".notification-indicator").remove();
import cats.effect._
import scala.concurrent.{ExecutionContext, Future}
class FutureConcurrentEffect()(implicit ec: ExecutionContext) extends FutureEffect with ConcurrentEffect[Future] {
def start[A](fa: Future[A]): Future[Fiber[Future, A]] = Future.successful {
FutureFiber(fa)
}
def racePair[A, B](fa: Future[A], fb: Future[B]): Future[Either[(A, Fiber[Future, B]), (Fiber[Future, A], B)]] = ???
@nightscape
nightscape / graphql.puml
Last active July 23, 2019 00:50
PlantUML Sprites
@startuml
sprite $graphql [48x48/16] {
000000000000000000000001100000000000000000000000
0000000000000000000002CFFC2000000000000000000000
000000000000000000002FFFFFF200000000000000000000
000000000000000000007FFFFFF700000000000000000000
00000000000000000000FFFFFFFF00000000000000000000
00000000000000000000CFFFFFFC00000000000000000000
00000000000000000004FFFFFFFF40000000000000000000
000000000000000003CF58FFFF85FC300000000000000000
import ammonite.ops._
import mill._
import mill.util.Ctx
import mill.modules.Jvm
trait FlywayModule extends JavaModule {
def flywayMigrationPaths: T[Seq[PathRef]] = T { resources() }
def flywayUser: T[String]
def flywayPassword: T[String]
def flywayJdbcUrl: T[String]
@nightscape
nightscape / FramelessRefinedInjection.scala
Created February 16, 2018 15:34
Experiment to use Frameless Injections to use Refined types with Spark
import $ivy.{
`org.typelevel::frameless-cats:0.4.0`,
`org.typelevel::frameless-dataset:0.4.0`,
`org.typelevel::frameless-ml:0.4.0`,
`org.apache.spark::spark-core:2.2.1`,
`org.apache.spark::spark-sql:2.2.1`,
`eu.timepit::refined:0.8.7`
}
import scala.language.higherKinds
@nightscape
nightscape / compareDataFrames.scala
Last active August 29, 2017 12:38
Compares two DataFrames and creates a resulting DataFrame with diffs
import org.apache.spark.sql.DataFrame
def compareDataFrames(dfA: DataFrame, dfB: DataFrame, joinFields: Array[String]): (DataFrame, Seq[String]) = {
val nonJoinFields = (dfA.schema.fieldNames ++ dfB.schema.fieldNames).distinct.diff(joinFields)
val joined = dfA.join(dfB, joinFields)
val differing = joined.where(nonJoinFields.map(f => dfA(f) =!= dfB(f)).reduce(_ || _)).cache
val differingFields = nonJoinFields.filter(f => differing.select(dfA(f)).except(differing.select(dfB(f))).count + differing.select(dfB(f)).except(differing.select(dfA(f))).count > 0)
val nonDifferingNonJoinFields = nonJoinFields.diff(differingFields)
val differingRenamed = differingFields.foldLeft(differing) { case(df, field) => df.withColumn(s"${field}_a", dfA(field)).withColumn(s"${field}_b", dfB(field)).drop(field) }
val comparisonDf = nonDifferingNonJoinFields.foldLeft(differingRenamed) { case(df, field) => df.withColumn(s"${field}_common", dfA(field)).drop(field) }
@nightscape
nightscape / TranslatorFactory1.scala
Created March 30, 2017 12:37
Eff macro-generated TranslatorFactory
trait TranslatorFactory1[Replacement[_]] {
implicit class RichKVStore[R, A](val effects: Eff[R, A]) {
def runKVStore[U](implicit m: Member.Aux[KVStore, R, U], _replacement: MemberIn[Replacement, U]): Eff[U, A] = TranslatorFactory1.this.runKVStore(effects)(m, _replacement)
}
def runKVStore[R, U, A](effects: Eff[R, A])(implicit m: Member.Aux[KVStore, R, U], _replacement: MemberIn[Replacement, U]): Eff[U, A] = {
val tr = translator[R, U](m, _replacement);
org.atnos.eff.interpret.translate(effects)(tr)
}
def translator[R, U](implicit m: Member.Aux[KVStore, R, U], _replacement: MemberIn[Replacement, U]): org.atnos.eff.Translate[KVStore, U] = {
new org.atnos.eff.Translate[KVStore, U] {