Skip to content

Instantly share code, notes, and snippets.


Dino Fancellu fancellu

View GitHub Profile
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 / 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[Int] //> res0: Option[Int] = None[String] //> res1: Option[String] = Some(foo)
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 / 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.collect{case Success(x) => x}
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 / 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 / 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{Invalid, Valid}
import{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 / 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) }
} //> 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 / 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 / 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()))