Skip to content

Instantly share code, notes, and snippets.

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.*
def delay(delay: Long)(id: String) = Future {
Thread.sleep(delay)
println(s"$id done")
id
}
def fast(id: String) = delay(500)(id)
//import cats.Applicative
//import cats.implicits.*
/*
def sequence[A, F[_], G[_]](fga: F[G[A]])(using Traverse[F], Applicative[G]): G[F[A]] =
traverse(fga)(identity)
trait Traverse[F[_]]:
def traverse[A, B, G[_]](fa: F[A])(f: A => G[B])(using Applicative[G]): G[F[B]]
*/
package th.lim.dojo3.concurrent
import cats.effect.std.{Console, Random, SecureRandom}
import cats.effect.{IO, IOApp}
import cats.syntax.all.{toFlatMapOps, toFunctorOps}
import cats.{FlatMap, Functor}
/** An example of using Random[F] in additional to
* https://typelevel.org/cats-effect/docs/std/random#using-random
*/
@sshark
sshark / ConsumerProducerQ.java
Last active February 25, 2024 09:03
Uses flags or poison pill to synchronize producers and consumers
package org.teckhooi;
import java.time.Duration;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@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 / tagless-final.sc
Last active February 16, 2024 17:38
Tagless final vs tagless initial vs tagged initial
/** This is a summary of this website
* https://www.foxhound.systems/blog/final-tagless/ with the example written in
* Scala.
*
* This is an example of tagged initial encoding. It is tagged because
* SqlExprResult is used to streamlined the returned values. It is initial
* encoding because eval has to perform pattern matching.
*
* Use GADT (Generalized Algebraic Data Type) to make it tag-less initial
* encoding.
@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()