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.effect.{IO, IOApp} | |
| import org.http4s.{Headers, MediaType, Method, Request} | |
| import org.http4s.ember.client.EmberClientBuilder | |
| import org.http4s.headers.Accept | |
| import org.http4s.implicits.http4sLiteralsSyntax | |
| import org.typelevel.log4cats.LoggerFactory | |
| import org.typelevel.log4cats.slf4j.Slf4jFactory | |
| object EmberClientCall extends IOApp.Simple { |
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.effect.{IO, IOApp} | |
| // Emits intro text, then prompts for integers, keeping a running sum. Will exit upon 'exit' | |
| object Repl2 extends IOApp.Simple { | |
| def parseInput(input: String): Int = | |
| scala.util.Try(input.toInt).toOption.getOrElse(0) | |
| def repl(total: Int): IO[Int] = { |
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.effect.{IO, IOApp} | |
| // Emits intro text, then prompts for some command. Will exit upon 'exit' | |
| // Note, these programs are values, as expected | |
| object Repl extends IOApp.Simple { | |
| val repl: IO[Unit] = { | |
| for { | |
| input <- IO.println(">>> ") *> IO.readLine | |
| _ <- IO.println(s"You entered: $input") *> (if (input == "exit") IO.unit else repl) |
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
| ## Plotting an XOR function | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| X=np.random.random((1000,2))*2-1 # between -1 and 1 | |
| Y=[ (x[0]>0) !=(x[1]>0) for x in X] # xor is true if booleans are unequal | |
| plt.scatter(X[:,0], X[:,1],c=Y); |
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 requests as requests | |
| from requests import Response | |
| from pydantic import BaseModel | |
| class Quote(BaseModel): | |
| content: str | |
| author: str | |
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 Scala3TypeClassWithUsingGiven { | |
| trait MyMonoid[A] { | |
| def combine(x: A, y: A): A | |
| def empty: A | |
| } | |
| def combineList[A](li: List[A])(using monoid: MyMonoid[A]): A = li.foldLeft(monoid.empty)(monoid.combine) | |
| def main(args: Array[String]): Unit = { |
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.effect._ | |
| import fs2._ | |
| import scala.concurrent.duration._ | |
| import cats.effect.std._ | |
| // FS2 cats.effect.Queue example using flatMap or for comprehension | |
| // Both streams emit nothing, but are effectful, communicating via the queue and updating a sum via the ref | |
| object Fs2Queues extends IOApp.Simple { |
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 MyErrsIO.Controller.{Request, postTransfer} | |
| import MyErrsIO.Models.Account | |
| import cats.data.Validated.{Invalid, Valid} | |
| import cats.data.{EitherT, OptionT, Validated, ValidatedNec} | |
| import cats.effect._ | |
| import cats.implicits._ | |
| import scala.util.control.NonFatal | |
| // A simple example of cat-effect and error handling, with MonadTransformers |
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._ | |
| object TreeFunctorExample { | |
| def main(args: Array[String]): Unit = { | |
| sealed trait Tree[+T] | |
| case class Leaf[+T](value: T) extends Tree[T] | |
| case class Branch[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[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
| object Intersect extends App{ | |
| import scala.annotation.tailrec | |
| // finds the elements shared by 2 lists, FP style, recursively | |
| // O(n log n) | |
| @tailrec | |
| def scan(x: List[Int], y: List[Int], out: List[Int] = List.empty[Int]): List[Int] = | |
| (x, y) match { |