This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |