Skip to content

Instantly share code, notes, and snippets.

@sshark
sshark / Foo.java
Last active February 18, 2024 17:04
Controlled threads execution
package th.lim;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Suppose we have the following code:
*
* class Foo {
* void first() {...}
@sshark
sshark / readme.md
Last active May 2, 2025 17:10
Tagless final vs tagless initial vs tagged initial

Tagless Final Summary

I create this gist to summarize my understanding on tagless final that I read in the article A "quick" introduction to Tagless Final.

Tagless

The first word "Tagless" makes reference to the return types that are not tagged or grouped using GADT, ADT or union types, Int | String. Yet. the return types can be "anything".

Final

Initial encoding uses ADT etc. to encode the expressions, Then use an interpretor to evaluate the expressions. On the other hand, final encoding uses functions to model the expression. With functions, tags and interpretors are not required.

@sshark
sshark / CancellableTask.scala
Last active November 28, 2023 15:42
Demonstrate cancellable tasks are cancelled while they are sleeping
package org.teckhooi.dojo3.concurrent
import cats.effect.{IO, IOApp}
import scala.concurrent.duration.*
object CancellableTask extends IOApp.Simple {
def task(i: Int, d: Duration): IO[Unit] = IO.sleep(d) *> IO.println(s"Task $i completed")
override def run: IO[Unit] = for {
package org.teckhooi.concurrent
import cats.effect.{IO, IOApp}
import cats.implicits.catsSyntaxFlatMapOps
object ParTraverseApp extends IOApp.Simple {
override def run: IO[Unit] = {
def parTraverse[A, B](as: List[A])(f: A => IO[B]): IO[List[B]] =
as.map(a => f(a).start)
.foldLeft(IO.pure(List.empty[B]))((ioList, ioFibre) =>
@sshark
sshark / ListMapDeepCopy.java
Last active May 27, 2023 16:44
deep copy list of maps
import java.util.*;
public class ListMapDeepCopy {
static <A, B> void deepCopy(List<Map<A,B>> target, List<Map<A,B>> source ) {
target.clear();
source.forEach(m -> target.add(new HashMap<>(m)));
}
public static void main(String[] args) {
Map<String, Integer> foobar1 = new HashMap<>();
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._