Skip to content

Instantly share code, notes, and snippets.

View Ghurtchu's full-sized avatar
👽
λ implicitly λ

Anzori (Nika) Ghurtchumelia Ghurtchu

👽
λ implicitly λ
View GitHub Profile
@Ghurtchu
Ghurtchu / ConcurrentInMemoryCache.scala
Last active April 2, 2023 22:56
In-Memory Concurrent Key-Value Cache
object ConcurrentInMemoryCacheImpl {
trait Cache[K, V] {
type F
def put(k: K, v: V): Unit
def get(k: K): Option[V]
def remove(k: K): Option[V]
def update(k: K, v: V): Unit
def value: F
}
@Ghurtchu
Ghurtchu / OptionTransformer.scala
Last active April 2, 2023 17:34
OptionT - Option Monad Transformer
object OptionTransformerImpl {
final case class OptionT[F[_] : Monad, A](value: F[Option[A]]) {
def map[B](f: A => B): OptionT[F, B] =
OptionT(value.map(_.map(f)))
def flatMap[B](f: A => OptionT[F, B]): OptionT[F, B] =
OptionT(value.flatMap(_.fold(Option.empty[B].pure[F])(a => f(a).value)))
}
@Ghurtchu
Ghurtchu / TaglessFinalGuessGame.scala
Last active April 1, 2023 16:59
Tagless Final 101
import scala.util.Try
object TaglessFinalGuessGame {
trait Monad[F[_]] {
def pure[A](a: A): F[A]
def map[A, B](fa: F[A])(f: A => B): F[B] = flatMap(fa)((f andThen pure)(_))
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]
}
@Ghurtchu
Ghurtchu / ValidatorImpl.scala
Last active April 4, 2023 21:44
Accumulative Validator
object ValidatorImpl {
sealed trait Validated[+E, +S]
object Validated {
final case class Passed[S](value: S) extends Validated[Nothing, S]
final case class Failed[E](errors: List[E]) extends Validated[E, Nothing]
}
trait Validator[+E, S] { previous =>
@Ghurtchu
Ghurtchu / Attempt.scala
Last active October 19, 2022 12:38
Mirror of scala.util.Try
sealed trait Attempt[+A] {
def value: A
def isSuccess: Boolean
def isFailure: Boolean
def map[B](f: A => B): Attempt[B]
def flatMap[B](f: A => Attempt[B]): Attempt[B]
def filter(f: A => Boolean): Attempt[A]
def withFilter(f: A => Boolean): Attempt[A]
def fold[B](ifSuccessful: A => B)(ifFailed: B): B
def foreach(f: A => Unit): Unit
@Ghurtchu
Ghurtchu / Stream.scala
Last active October 19, 2022 12:40
Here's to the lazy ones...
enum Stream[+A]:
case Empty
case Cons(h: () => A, t: () => Stream[A])
def headOption: Option[A] = this match
case Empty => None
case Cons(h, t) => Some(h())
def filter(f: A => Boolean): Stream[A] = this match
@Ghurtchu
Ghurtchu / MaybeImpl.scala
Created August 10, 2022 11:11
is it Option? Maybe!..
object MaybeImpl extends scala.App {
sealed trait Maybe[+A] {
def get: A
def map[B >: A](f: A => B): Maybe[B]
def flatMap[B >: A](f: A => Maybe[B]): Maybe[B]
def filter(p: A => Boolean): Maybe[A]
def isEmpty: Boolean
def fold[B](ifEmpty: => B)(f: A => B): B
}
@Ghurtchu
Ghurtchu / TreeImpl.scala
Created July 2, 2022 07:35
Binary Tree
import TreeImpl.Tree.{leaf, node}
object TreeImpl {
sealed trait Tree[+A] {
def value: A
def map[B >: A](f: A => B): Tree[B]
def flatMap[B >: A](f: A => Tree[B]): Tree[B]
def withFilter(f: A => Boolean): Tree[Option[A]]
def toList: List[A]
@Ghurtchu
Ghurtchu / ArithmeticExpressionsDSL.scala
Last active June 26, 2022 13:48
ArithmeticExpressionsDSL.scala
import scala.language.implicitConversions
object ArithmeticExpressionsDSL extends scala.App {
sealed trait Expr
object Expr {
final case class Literal private(n: Int) extends Expr
final case class Negate private(expr: Expr) extends Expr
final case class Add private (left: Expr, right: Expr) extends Expr