Skip to content

Instantly share code, notes, and snippets.

Avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / .block
Last active Nov 13, 2022
Force directed graph for D3.js v4 with labelled edges and arrows
View .block
license: gpl-3.0
height: 600
@fancellu
fancellu / ShapelessCoproduct.scala
Last active Nov 5, 2022
Examples with Shapeless 2.0, easier to understand from examples. Taken from docs, more examples/comments added etc
View ShapelessCoproduct.scala
// Coproduct is extension of Either concept, to N multually exlusive choices
type ISB = Int :+: String :+: Boolean :+: CNil
val isb = Coproduct[ISB]("foo") //> isb : qaaz.ISB = foo
isb.select[Int] //> res0: Option[Int] = None
isb.select[String] //> res1: Option[String] = Some(foo)
@fancellu
fancellu / ConsumerExample.scala
Last active Oct 21, 2022
Kafka Producer/Consumer Example in Scala
View ConsumerExample.scala
import java.util
import org.apache.kafka.clients.consumer.KafkaConsumer
import scala.collection.JavaConverters._
object ConsumerExample extends App {
import java.util.Properties
@fancellu
fancellu / TryFlatten.scala
Created Apr 11, 2017
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 / CatsFlatmap.scala
Last active Aug 15, 2022
Example usage of Cats FlatMap
View CatsFlatmap.scala
import cats._
import cats.implicits._
// Example usage of Cats Flatmap
object CatsFlatmap extends App {
val listFlatMap=FlatMap[List]
val li=List(1,2,3)
@fancellu
fancellu / Fs2Queues.scala
Created Jul 14, 2022
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 / MyErrsIO.scala
Created Mar 21, 2022
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 / Insert.scala
Created Aug 30, 2015
Simple insertion into an ordered List
View Insert.scala
val li=List(1,2,10,20,30) //> li : List[Int] = List(1, 2, 10, 20, 30)
def insert[T](li:List[T],x:T)(implicit cmp:Ordering[T])={
val (first,last)=li.partition {cmp.lteq(_, x) }
first:::x::last
} //> insert: [T](li: List[T], x: T)(implicit cmp: Ordering[T])List[T]
insert(li,11) //> res0: List[Int] = List(1, 2, 10, 11, 20, 30)
insert(li,10) //> res1: List[Int] = List(1, 2, 10, 10, 20, 30)
insert(li,9) //> res2: List[Int] = List(1, 2, 9, 10, 20, 30)
@fancellu
fancellu / MergeSort.scala
Created Nov 25, 2015
MergeSort in Scala with recursive merge
View MergeSort.scala
object MergeSort {
// recursive merge of 2 sorted lists
def merge(left: List[Int], right: List[Int]): List[Int] =
(left, right) match {
case(left, Nil) => left
case(Nil, right) => right
case(leftHead :: leftTail, rightHead :: rightTail) =>
if (leftHead < rightHead) leftHead::merge(leftTail, right)
else rightHead :: merge(left, rightTail)
@fancellu
fancellu / MyIO.scala
Last active Oct 23, 2021
Simple example of an IO effect Monad, no Cats, no ZIO
View MyIO.scala
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()))