Skip to content

Instantly share code, notes, and snippets.

@erdeszt
erdeszt / 0RaiseV2.kt
Last active September 25, 2025 22:11
Kotlin checked exceptions
package org.gl.common
sealed interface Result<out Error, out Value> {
fun <Value1> map(f: (Value) -> Value1): Result<Error, Value1> = when (this) {
is Failure -> Failure(error)
is Success -> Success(f(value))
}
fun <Error1> leftMap(f: (Error) -> Error1): Result<Error1, Value> = when (this) {
is Failure -> Failure(f(error))
@erdeszt
erdeszt / 1_Solver.scala
Last active December 6, 2024 16:05
Advent of code "framework"
import scala.util.Using
trait Solver[Day <: Int: ValueOf, Part <: Int: ValueOf]:
/** Solves the `Part` of the solution for the `Day`
*
* @param input
* The lines of the input file
* @return
* The solution
@erdeszt
erdeszt / Direct.scala
Last active May 10, 2024 06:59
Direct style scala effect tracking
package direct
import java.util.UUID
import scala.language.experimental.saferExceptions
import Contains.given
import izumi.reflect.macrortti.LightTypeTag
import izumi.reflect.{*, given}
import sttp.tapir.*
@erdeszt
erdeszt / zats.scala
Last active January 9, 2023 23:57
ZIO flavored CE error handling
package zats
import cats.effect.{IO, IOApp}
import cats.effect.std.Console
import cats.{Applicative, Functor, MonadError}
import cats.syntax.functor.*
import cats.syntax.flatMap.*
import scala.annotation.implicitNotFound
type App[F[_]] = MonadError[F, Throwable]
@erdeszt
erdeszt / SafeServiceWith.scala
Last active December 19, 2022 23:34
Safe ZIO.serviceWith
trait ZIO[-R, +E, +A] {
}
object ZIO {
def serviceWithZIO[S]: SWZIOP[S] = new SWZIOP[S]
def serviceWith[S]: SWP[S] = new SWP[S]
@erdeszt
erdeszt / editAgreement.dot
Last active November 25, 2022 09:42
graph
digraph {
editAgreement [shape=box];
addCoInsureeToAgreement [fillcolor=aquamarine, style=filled];
updateRelatedUser [fillcolor=aquamarine, style=filled];
addAgreementCoInsuree [fillcolor=aquamarine, style=filled];
updateAgreementCoInsuree [fillcolor=aquamarine, style=filled];
@erdeszt
erdeszt / RichNewtypeScala3.scala
Last active November 14, 2022 21:31
Scala 3 zio rich newtype improvements
import zio.prelude.Newtype
import cats.Contravariant
import cats.Functor
import cats.syntax.contravariant.*
import cats.syntax.functor.*
import io.circe.*
import io.circe.syntax.*
import io.circe.parser.decode
@erdeszt
erdeszt / gpio.zig
Last active March 8, 2021 15:28
Zig on AVR demo
const Direction = packed enum (u1) {
Output = 0,
Input = 1,
};
const Level = packed enum(u1) {
Low = 0,
High = 1,
};
@erdeszt
erdeszt / Invariant.idr
Last active December 15, 2020 22:27
Idris datatype invariant
module Main
import Data.String
data CorrectSize : Nat -> Nat -> Nat -> Type where
SmallerThan150 : (x : Nat) -> (y : Nat) -> (z : Nat) -> {auto prf : x + y + z <= 150 = True} -> CorrectSize x y z
Show (CorrectSize x y z) where
show (SmallerThan150 x y z) = "CorrectSize " ++ show (x, y, z)
showPrec _ size = show size
@erdeszt
erdeszt / Intro.v
Last active June 6, 2020 14:48
Coq intro
Module Datatypes.
Inductive bool : Type := true | false.
(* Scala:
sealed trait Bool
case class True() extends Bool
case class False() extends Bool
*)