Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / TryFlatten.scala
Created April 11, 2017 00:44
A few ways to flatten down a Seq[Try] to only Success values
View TryFlatten.scala
import scala.util.{Success, Failure}
val seq=Seq(Success(1), Failure(new Exception("bang")), Success(2))
// all emit List(1, 2)
seq.map(_.toOption).flatten
seq.flatMap(_.toOption)
seq.filter(_.isSuccess).map(_.get)
seq.collect{case Success(x) => x}
@fancellu
fancellu / .block
Last active August 3, 2023 01:59
Force directed graph for D3.js v4 with labelled edges and arrows
View .block
license: gpl-3.0
height: 600
@fancellu
fancellu / EmberClientCall.scala
Last active July 20, 2023 10:19
EmberClientCall.scala (trying to work out why we get a stack trace, seems that this is a known issue)
View EmberClientCall.scala
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 {
@fancellu
fancellu / MyErrsIO.scala
Created March 21, 2022 12:45
A simple example of cat-effect and error handling, with MonadTransformers
View MyErrsIO.scala
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
@fancellu
fancellu / Fs2Queues.scala
Created July 14, 2022 11:27
FS2 cats.effect.Queue example using flatMap or for comprehension
View Fs2Queues.scala
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 {
@fancellu
fancellu / Scala3TypeClassWithUsingGiven
Created January 1, 2023 19:59
Scala3 TypeClass example with using/given
View Scala3TypeClassWithUsingGiven
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 = {
@fancellu
fancellu / random_quote.py
Created June 9, 2023 13:29
Retrieves a random quote, uses requests, json and pydantic
View random_quote.py
import requests as requests
from requests import Response
from pydantic import BaseModel
class Quote(BaseModel):
content: str
author: str
@fancellu
fancellu / xorplot.py
Created July 3, 2023 13:12
Plotting an XOR function in matplotlib
View xorplot.py
## 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);
@fancellu
fancellu / Repl.scala
Created July 17, 2023 15:29
Cats effect 3.x Repl, useful when creating a custom Repl for your Cats effect code
View Repl.scala
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)
@fancellu
fancellu / Repl2.scala
Created July 17, 2023 16:15
Cats effect 3.x Repl that keeps a running sum
View Repl2.scala
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] = {