Skip to content

Instantly share code, notes, and snippets.

View nightscape's full-sized avatar

Martin Mauch nightscape

  • Regensburg, Germany
  • 09:02 (UTC +02:00)
View GitHub Profile
@nightscape
nightscape / SequentialRandomSampling.scala
Created May 5, 2020 08:30
A not yet working Scala implementation of Sequential Random Sampling from Vitter
// Translated from https://github.com/gliese1337/vitter-sample/blob/master/src/index.ts
// TODO: The returns were yields and the entire algorithm should create a Seq or Iterable of Ints
import scala.util.control.Breaks._
object SequentialRandomSampling {
val negalphainv = -13
def skip(_k: Int, _N: Int) {
var k = _k
var N = _N
var qu1 = N - k + 1
var S = 0
@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) }