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.data._ | |
| import cats.syntax._ | |
| import cats.implicits._ | |
| object CatsAlign extends App { | |
| val alOption=Align[Option] | |
| val iorO: Option[Ior[Int, Int]] =alOption.align(Option(1), Option(2)) |
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
| // Shows how to use okhttp and sttp in Scala asynchronously to talk to a Tor onion address via a Tor socks proxy, | |
| // without trying to resolve DNS outside of proxy (few http clients do this properly and instead return UnknownHostException) | |
| // libraryDependencies ++= Seq("com.squareup.okhttp3" % "okhttp" % "4.7.2", | |
| // "com.softwaremill.sttp.client" %% "okhttp-backend" % "2.1.2")) | |
| import java.io.IOException | |
| import java.net.{InetSocketAddress, Proxy} | |
| import okhttp3.{Call, Callback, OkHttpClient, Request, Response} |
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.data._ | |
| import cats.syntax._ | |
| import cats.implicits._ | |
| object CatFold extends App{ | |
| // list of Tuple[Int, Double] | |
| println(List((20,1.1),(29,2.2)).combineAllOption) |
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 play.api.libs.json._ | |
| object PlayJsonAnyVal extends App { | |
| final case class Address(path: String) extends AnyVal | |
| object Address { | |
| // this allows Address to be used as a map key, even though it is not a String | |
| implicit val keyReads: KeyReads[Address]= key => JsSuccess(Address(key)) | |
| implicit val keyWrites: KeyWrites[Address] = _.path |
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.data._ | |
| import cats.syntax._ | |
| import cats.implicits._ | |
| object CatFunc extends App { | |
| // a very naive linked list implementation and functor for it | |
| trait MyList[+A] | |
| case class MyNode[A]( head: A, tail: MyList[A]) extends MyList[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 simulacrum._ | |
| // Example usage of Simulacrum, v similar to math.Ordering/Numeric in standard SDK | |
| object SimType extends App { | |
| @typeclass trait Showing[T] { | |
| def show(x: T): String | |
| } | |
| @typeclass trait Ordering[T] extends Showing[T]{ |
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.Eval | |
| object Ev extends App{ | |
| // eager, method call | |
| val always: Eval[Int] =Eval.always{ | |
| println("running always") | |
| 123 | |
| } | |
| // lazy, memoized |
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 Unapp extends App { | |
| case class MyClass(age: Int, name: String) | |
| private object Age { | |
| def unapply(myClass: MyClass): Option[Int] = myClass match { | |
| case MyClass(age, _) if (age > 0) => Option(age) | |
| case _ => None | |
| } | |
| } |
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
| // Just a little example, but frankly, avoid Futures, they are eager and do not compose well | |
| import scala.concurrent.Await | |
| import scala.concurrent.Future | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| import scala.concurrent.duration._ | |
| object FutOrElse extends App{ | |
| def getSource1(s: String): Future[Option[String]]= |
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 scala.util.Random | |
| // Simple example of an IO effect Monad, no Cats | |
| object MyIO extends App{ | |
| // IO encapsulates a side effecting operation | |
| final class IO[A](val run: () => A) { | |
| def map[B](f: A => B): IO[B] = IO(f(run())) |