Skip to content

Instantly share code, notes, and snippets.

@sortega
sortega / monty_hall.sc
Created February 22, 2023 17:05
Discrete probability monad and an example analysis of the Monty Hall problem. Run it with `scala-cli monty_hall.sc`
//> using dep "org.typelevel::spire:0.18.0"
import spire.math._
import spire.implicits._
case class Distro[A](entries: Map[A, Rational]):
require(entries.values.qsum == Rational(1), s"Not normalized: $entries")
def apply(value: A): Rational = entries.getOrElse(value, Rational(0))
@sortega
sortega / Flattener.scala
Created November 17, 2022 18:17
What if we needed Luigi-style input/output flattening but in a type safe way?
package flattening
import scala.annotation.implicitNotFound
object Flattener {
def flatten[A]: PartiallyAppliedFlattener[A] = new PartiallyAppliedFlattener
final class PartiallyAppliedFlattener[A] {
def apply[B](value: B)(implicit ev: Flattenable[A, B]): List[A] = ev(value)
}
@sortega
sortega / search.json
Created March 1, 2021 13:31
Search config
{
"file_version": "2.0",
"preferences": {
"custom_engines": {
"ggl": {
"name": "Google Search",
"url": "https://www.google.co.in/search?q={searchTerms}",
"category": "",
"description": "Google Search"
},
@sortega
sortega / connect-status-csv.sh
Created January 7, 2021 16:56
Export connect status to CSV
#!/bin/bash
CONNECT_URL=${1:-${CONNECT_URL:-http://localhost:8083}}
CONNECTOR_LIST=$(curl -s -L -X GET $CONNECT_URL/connectors | jq -r '.[]')
for CONNECTOR in $CONNECTOR_LIST; do
curl -s -L -X GET $CONNECT_URL/connectors/$CONNECTOR/status \
| jq -r "(.tasks[] | [\"task\", \"$CONNECTOR\", .id, .state, .worker_id]), [\"connector\", \"$CONNECTOR\", .type, .connector.state, .connector.worker_id] | @csv" &
done
wait
@sortega
sortega / pre-commit
Last active March 31, 2020 12:59
Place this in .git/hooks to avoid commiting malformed terraform sources
#!/bin/bash
set -e
MODULES=`find . -name '*.tf' -print0 | xargs -0 -n1 dirname | sort --unique | grep -v .terraform`
echo -n "fmt check"
for MODULE in $MODULES; do
(terraform fmt -check && echo -n ".") &
done
wait
echo " OK"
@sortega
sortega / CodingExercise.scala
Created September 10, 2019 08:03
Coding exercise
package mentoring
object CodingExercise {
final case class Pos(x: Int, y: Int) {
def +(other: Pos) = Pos(x + other.x, y + other.y)
}
object Pos {
val Origin = Pos(0, 0)
}
object ConnectedStreamsApp extends App {
def writer(outputStream: OutputStream): Unit = {
val writer = new PrintStream(outputStream)
(1 to 1000).foreach { i =>
writer.println(s"line $i")
Thread.sleep(50)
}
writer.close()
}
@sortega
sortega / Json.scala
Last active August 22, 2019 12:40
Typeclasses sample code
package fp.typeclasses
import org.apache.commons.text.StringEscapeUtils
sealed abstract class Json
object Json {
type JField = (String, Json)
case object JNull extends Json {
package day22
import scalaz._, Scalaz._
object Day22 extends App {
sealed trait Equipment
object Equipment {
case object Climbing extends Equipment
case object Torch extends Equipment
@sortega
sortega / FailFastTraverse.scala
Last active January 8, 2018 13:54
Future.traverse is not fail fast
package sandbox.futures
import scala.collection.generic.CanBuildFrom
import scala.concurrent._
import scala.concurrent.duration._
import scala.language.higherKinds
import scala.util.{Random, Success}
import scala.util.control.NoStackTrace
object FailFastTraverse {