Skip to content

Instantly share code, notes, and snippets.

View Tvaroh's full-sized avatar
😻
Happy programming

Aliaksandr Siamionau Tvaroh

😻
Happy programming
View GitHub Profile
@Tvaroh
Tvaroh / intersperse.scala
Created April 27, 2021 08:01
Intersperse in Scala
implicit class SeqExtensions[A](val as: Seq[A]) extends AnyVal {
def intersperse(a: A): Seq[A] = {
val b = Seq.newBuilder[A]
val it = as.iterator
if (it.hasNext) {
b += it.next()
while(it.hasNext) {
b += a
import java.sql.SQLException
import cats.MonadError
import cats.implicits._
trait SqlStateMapping {
val constraintViolation: String
val foreignKeyViolation: String
val uniqueViolation: String
import cats.implicits._
import cats.Applicative
import tofu.syntax.handle._
import tofu.syntax.raise._
import tofu.{Handle, Raise}
sealed trait Restorable[A]
object Restorable {
@Tvaroh
Tvaroh / TaskAsync.scala
Last active November 12, 2019 14:31
Monix Task's Async instance for Korolev
import korolev.Async
import korolev.Async.Promise
import monix.eval.Task
import monix.execution.Scheduler
import scala.util.{Failure, Success, Try}
class TaskAsync(implicit scheduler: Scheduler) extends Async[Task] {
override val unit: Task[Unit] = Task.unit

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@Tvaroh
Tvaroh / playground.scala
Created March 1, 2019 10:31
ZIO approach with TF
import cats.~>
object playground {
// typeclasses
trait MIO[F[_], R, +E, +A] {
def map[B](f: A => B): MIO[F, R, E, B]
def flatMap[R1 <: R, E1 >: E, B](k: A => MIO[F, R1, E1, B]): MIO[F, R1, E1, B]
import scala.concurrent.Future
/** Typeclass for interoperating with legacy code which uses Scala futures. */
trait ExecuteUnsafe[F[_]] {
def unsafeToFuture[T](mt: F[T]): Future[T]
}
object ExecuteUnsafe {
import cats.effect.Async
import cats.implicits._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
/** Typeclass to wrap Scala futures. */
trait Execute[F[_]] {
implicit def async: Async[F]
import java.util.UUID
import cats.data.EitherT
import cats.effect.IO
import cats.implicits._
import cats.{Applicative, ApplicativeError}
import io.circe.syntax._
import io.circe.{Encoder, Json}
object example extends App {
import cats.data.ReaderT
import cats.implicits._
import cats.{Applicative, Functor}
trait ContextRead[F[_], Ctx] {
def read: F[Ctx]
}