Skip to content

Instantly share code, notes, and snippets.

package org.teckhooi.fp.dojo2.fpis.iostream
import org.teckhooi.fp.dojo2.fpis.iostream.Process.{Await, Emit, Halt}
sealed trait Process[I, O] {
import Process._
def apply(s: LazyList[I]): LazyList[O] = this match {
case Halt() => LazyList()
case class Config(name: String, age: Int)
case class Name(firstName: String, lastName: String)
case class Age(age: Int) extends AnyVal
case class Person(name: Name, age: Age)
object Configs:
type Configured[T] = Config ?=> T
def config: Configured[Config] = summon[Config]
object Exceptions:
@sshark
sshark / cata.sc
Last active September 16, 2022 19:03
Catamorphim with F-Algebra, Fix and, Functor
// Non recursive data structure
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
sealed trait ExpressionF[A]
case class ValueF[A](v: Int) extends ExpressionF[A]
case class AddF[A](e1: A, e2: A) extends ExpressionF[A]
case class MultF[A](e1: A, e2: A) extends ExpressionF[A]
package org.teckhooi
import cats.effect.{ExitCode, IO, IOApp}
import cats.effect.IO.ioParallel // requires for UptimeService[IO]
import cats.{Id, Monad, Parallel, Show}
import cats.syntax.parallel._
import cats.syntax.functor._
import cats.syntax.traverse._ // requires for traverse
import cats.syntax.show._
import cats.syntax.flatMap._ // requires for UptimeService[Id]
@sshark
sshark / CofreeExample.scala
Last active August 21, 2022 17:30
Doobie Cofree
package org.teckhooi
import atto.Atto._
import atto._
import cats._
import cats.data.Kleisli
import cats.effect._
import cats.free.Cofree
import cats.implicits._
import doobie._
@sshark
sshark / ShowMeTheMoneyApp.scala
Last active July 21, 2022 14:52
Demonstrate contravariant Functor with Show typeclass without using Cats
package org.teckhooi
import scala.language.implicitConversions
trait Show[A] {
def show(a: A): String
}
trait ShowOps {
def show: String
@sshark
sshark / WhosFirst.java
Created June 27, 2022 16:03
Who is first?
package org.teckhooi.foo;
import java.util.Objects;
public class WhosFirst {
static private Long t1 = null;
static private Long t2 = null;
static private Object lock = new Object();
static private long startMillis = 0;
package org.teckhooi.bar;
import java.util.Map;
import java.util.Objects;
public class Leaf implements Tree {
private String value;
public Leaf(String value) {
this.value = value;
@sshark
sshark / FooBar.scala
Created February 4, 2022 16:49
Throwing Exception vs Using Either
object FooBar {
// Basically Java code written using Scala syntax. Similar to using if to check for errors i.e. if (err != nil) {...}
object Foo extends App {
def addMin(a: Int, b: Int): Int =
if (a + b < 10) throw new Exception("Sum is less than 10") else a + b
def mulMin(a: Int, b: Int): Int =
if (a * b < 100) throw new Exception("Product is less than 100") else a * b
@sshark
sshark / MaxDiff.scala
Last active October 23, 2021 15:06
Max difference between increasing elemets
import Math.*
def maxDiff(xs: List[Int]): Int =
xs.scanLeft(Int.MaxValue)(min)
.tail
.zip(xs)
.map(x => x._2 - x._1)
.filter(_ > 0)
.fold(-1)(max)