Skip to content

Instantly share code, notes, and snippets.

Avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / .block
Last active March 2, 2023 23:30
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 February 20, 2023 10:02
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 / MergeSort.scala
Created November 25, 2015 12:12
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 / 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 / ConsumerExample.scala
Last active December 22, 2022 09:39
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 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 / CatsFlatmap.scala
Last active August 15, 2022 14:45
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 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 / 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 / Insert.scala
Created August 30, 2015 15:03
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)