Skip to content

Instantly share code, notes, and snippets.

Avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / ConsumerExample.scala
Last active May 30, 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 / 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 / .block
Last active Feb 10, 2022
Force directed graph for D3.js v4 with labelled edges and arrows
View .block
license: gpl-3.0
height: 600
@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()))
@fancellu
fancellu / TopBottomN.scala
Last active Sep 14, 2021
// Top and bottom N max/min items from an Array, with no need to sort
View TopBottomN.scala
// Top and bottom N max/min items from an Array, with no need to sort
object TopBottomN extends App {
def bottomN(a: Array[Int], n: Int): Array[Int] = {
val smallestVals = Array.fill(n)(Int.MaxValue)
a.foreach { number =>
var inserted=false
(n - 1 to 0 by -1).foreach { pos =>
@fancellu
fancellu / NumberToWords.scala
Created May 12, 2021
NumberToWords converts an Int into English words
View NumberToWords.scala
// NumberToWords converts an Int into English words
object NumberToWords extends App{
val num2Word=Map((0, "Zero"), (1, "One"),
(2, "Two"), (3, "Three"),
(4, "Four"), (5, "Five"),
(6, "Six"), (7, "Seven"),
(8, "Eight"), (9, "Nine"),
(10, "Ten"), (11, "Eleven"),
@fancellu
fancellu / WaysToTraverseGrid.scala
Last active Sep 14, 2021
3 Methods to find how many Ways To Traverse a Grid
View WaysToTraverseGrid.scala
import scala.annotation.tailrec
/** width by height grid
starting top left
how many ways to travel to bottom right?
can only go right or down
3 methods shown
**/
object WaysToTraverseGrid extends App{
@fancellu
fancellu / Intersect.scala
Created May 28, 2021
Finds the elements shared by 2 lists, FP style, recursively
View Intersect.scala
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 {