This file contains hidden or 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 cats._ | |
| import cats.instances._ | |
| import cats.syntax._ | |
| import cats.data.Validated._ | |
| type ErrorOr[A] = Validated[String, A] | |
| def logDivide(a: Int, b: Int) = WriterT[ErrorOr, String, Int]( | |
| divide[ErrorOr](a,b) match { | |
| case Invalid(e) => Invalid("Division by zero\n") |
This file contains hidden or 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
| // kaishh [4:20 AM] | |
| // Found a neat way to always have typeclass syntax available without any wildcards imports: | |
| package mypkg { | |
| trait Functor[F[_]] { | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } | |
| final class FunctorOps[F[_], A](private val fa: F[A]) extends AnyVal { | |
| def map[B](f: A => B)(implicit F: Functor[F]): F[B] = F.map(fa)(f) | |
| } |
This file contains hidden or 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 fs2._ | |
| import cats._ | |
| import cats.implicits._ | |
| object Fs2FizzBuzz { | |
| val fb = Stream.emits(List(None, None, "Fizz".some)). | |
| repeat. | |
| zip(Stream.emits(List(None, None, None, None, "Buzz".some)). | |
| repeat). |
This file contains hidden or 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
| @ val fb = Stream.emits(List("", "", "fizz")).repeat.zip(Stream.emits(List("", "", "", "", "buzz")).repeat).map{case (a,b) => a |+| b} | |
| fb: Stream[Nothing, String] = Stream(..) | |
| @ fb.take(30).toList | |
| res37: List[String] = List( | |
| "", | |
| "", | |
| "fizz", | |
| "", | |
| "buzz", |
This file contains hidden or 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
| val fb = Stream.emits(List(false, false, true)).repeat.zip(Stream.emits(List(false,false,false,false,true)).repeat).map{ | |
| case (true,true) => "fizzbuzz" | |
| case (true,false) => "fizz" | |
| case (false,true) => "buzz" | |
| case (false,false) => "" | |
| } | |
| //fb: Stream[Nothing, String] = Stream(..) | |
| // @ fb.take(30).toList | |
| // res27: List[String] = List( |
This file contains hidden or 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
| // "com.lihaoyi" %% "upickle" % "0.7.5", | |
| import upickle.default.{read,write} | |
| import upickle.default.{ReadWriter => RW, macroRW} | |
| object PickleMap { | |
| case class KeyClass(s: String, i: Int) | |
| object KeyClass{ | |
| implicit val rw: RW[KeyClass] = macroRW |
This file contains hidden or 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 ComposePlay { | |
| // Compose takes two Kleisli arrows ( A => F[B], B => F[C] ) and returns A => F[C] | |
| // Here I implement unit and compose for Option | |
| // Then flatMap is implemented using compose showing that you can make a Monad | |
| // with unit and compose... | |
| def unitOption[A](a : A) : Option[A] = Some(a) |
This file contains hidden or 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 zio._ | |
| import zio.console._ | |
| import zio.{Queue, UIO} | |
| import akka.actor._ | |
| import com.typesafe.config.ConfigFactory | |
| import java.util.concurrent.Executors | |
| import scala.concurrent.{ExecutionContext} | |
| import scala.concurrent.duration.FiniteDuration | |
| import java.util.concurrent.{Executors, TimeUnit} | |
| import java.util.concurrent.{ExecutorService, Executors, ThreadFactory} |
This file contains hidden or 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 cats._ | |
| import cats.implicits._ | |
| import cats.data.StateT | |
| object Radio { | |
| case class Radio(volume: Int) | |
| type RadioError = String | |
| type RadioEither[A] = Either[RadioError, A] | |
| type RadioStateEitherT[A] = StateT[RadioEither, Radio, A] |
This file contains hidden or 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
| // Comonad is the dual of Monad | |
| // example for non empty list, extract is the head of the list | |
| // coflatMap gives you successive lists from the whole list, the tail, the tail of that | |
| // and you must return a single value for each list... | |
| @ Comonad[NonEmptyList].extract(NonEmptyList.of(1,2,3)) | |
| res26: Int = 1 | |
| @ Comonad[NonEmptyList].extract(NonEmptyList.of(1)) | |
| res27: Int = 1 |