Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / MyErrsIO.scala
Created March 21, 2022 12:45
A simple example of cat-effect and error handling, with MonadTransformers
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 / TreeFunctorExample.scala
Last active February 15, 2024 11:20
Example of Tree Functor usage in Cats
import cats._
object TreeFunctorExample {
def main(args: Array[String]): Unit = {
sealed trait Tree[+T]
case class Leaf[+T](value: T) extends Tree[T]
case class Branch[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T]
@fancellu
fancellu / Intersect.scala
Created May 28, 2021 10:28
Finds the elements shared by 2 lists, FP style, recursively
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 {
@fancellu
fancellu / WaysToTraverseGrid.scala
Last active September 14, 2021 20:32
3 Methods to find how many Ways To Traverse a Grid
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 / NumberToWords.scala
Created May 12, 2021 18:31
NumberToWords converts an Int into English words
// 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 / TopBottomN.scala
Last active September 14, 2021 20:32
// Top and bottom N max/min items from an Array, with no need to sort
// 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 / scala_play_css_tip.txt
Last active February 25, 2021 13:46
Tip to have CSS with Play Twirl, if SASS isn't powerful enough for you...
def css = Action {
val css: TxtFormat.Appendable =views.txt.css()
Ok(css).as("text/css")
}
inside HTML
<link rel="stylesheet" href="@routes.HomeController.css()"/>
@fancellu
fancellu / NullBooleanReads.scala
Created August 27, 2020 10:26
NullBooleanReads for play-json. Sometimes dirty json has a boolean of "null". This takes it to mean false
implicit object NullBooleanReads extends Reads[Boolean] {
def reads(json: JsValue): JsSuccess[Boolean] = json match {
case JsBoolean(b) => JsSuccess(b)
case _ => JsSuccess(false)
}
}
@fancellu
fancellu / CatsSemigroupK.scala
Created June 10, 2020 11:33
Cats SemigroupK vs Semigroup
import cats._
import cats.implicits._
// Cats SemigroupK vs Semigroup
object CatsSemigroupK extends App {
// semigroupK doesn't care about contents
@fancellu
fancellu / CatsFlatmap.scala
Last active August 15, 2022 14:45
Example usage of Cats FlatMap
import cats._
import cats.implicits._
// Example usage of Cats Flatmap
object CatsFlatmap extends App {
val listFlatMap=FlatMap[List]
val li=List(1,2,3)