Skip to content

Instantly share code, notes, and snippets.

@ivan-klass
ivan-klass / labyrinth.scala
Created February 28, 2022 11:15
Shortest labyrinth path
type Point = (Int, Int)
case class StateVars(pathLength: Int, seen: Set[Point], starts: Set[Point])
// lab is 2-dimension array either of enterable cell (true) or obsticle (false)
def solve(start: Point, end: Point, lab: Vector[Vector[Boolean]]): Int = {
State[StateVars, Boolean] { case StateVars(pathLength, seen, expandFrom) =>
val nextArea = expandFrom.flatMap { case (x, y) =>
Set((x - 1, y), (x + 1, y), (x, y + 1), (x, y - 1)).filterNot(seen).filter {
case (nx, ny) => nx >= 0 && nx < lab.size && ny >=0 && ny < lab(0).size && lab(nx)(ny)
}
}
@ivan-klass
ivan-klass / TagLessFinalMock.scala
Last active April 2, 2021 20:13
Using cats.tagless for mocking TF-algebras
import cats.{ApplicativeError, ~>}
import cats.tagless.FunctorK
object NotImplemented {
private def notImplemented[F[_]](implicit F: ApplicativeError[F, _ >: NotImplementedError]): F ~> F =
new (F ~> F){
override def apply[A](fa: F[A]): F[A] = F.raiseError(new NotImplementedError())
}
@ivan-klass
ivan-klass / BugExample.sc
Last active June 9, 2020 03:08
Magnolia typename typeArguments bug
import magnolia._
import scala.language.experimental.macros
case class TypeArguments[T](names: Vector[String]) {
override def toString: String = names.mkString("__")
}
object Bug {
@ivan-klass
ivan-klass / toggle-mic.sh
Created July 9, 2019 09:14
Ubuntu toggle USB microphone shortcut
#!/bin/sh
amixer -c 1 sset Mic toggle | grep '\[off\]' && notify-send "❌ MIC switched OFF ❌" || notify-send "🎤 MIC switched ON 🎤"
@ivan-klass
ivan-klass / package.scala
Created February 27, 2019 09:35
prometheus type-safe
package ru.scala
import io.prometheus.client.{Collector, Counter, Gauge, SimpleCollector}
import shapeless.{HNil, LabelledGeneric}
import shapeless._
import shapeless.labelled.FieldType
import scala.annotation.implicitNotFound
package object prometheus {