Skip to content

Instantly share code, notes, and snippets.

@sshark
sshark / RealAsync.java
Created December 17, 2020 16:13
Writing a functioning CompletableFuture
package org.teckhooi;
import java.util.concurrent.CompletableFuture;
public class RealAsync {
public static void main(String[] args) {
long waitTime = 2000;
System.out.println("Test 1 starts...");
long timeTaken = time(() -> {
@sshark
sshark / KMPPatternSearch.scala
Last active March 23, 2020 14:35
KMP (Knuth Morris Pratt) pattern search implementation
import scala.annotation.tailrec
/**
* KMP (Knuth Morris Pratt) pattern search implementation
*/
import KMPPatternSearch._
assert(auxPattern("abcdabca".toCharArray) == List(0, 0, 0, 0, 1, 2, 3, 1))
assert(auxPattern("aabaabaaa".toCharArray) == List(0, 1, 0, 1, 2, 3, 4, 5, 2))
@sshark
sshark / fizzbuzz.sc
Created March 21, 2020 16:54
Classic fizz buzz lazy implementation
val fizz: LazyList[String] = "" #:: "" #:: "fizz" #:: fizz
val buzz: LazyList[String] = "" #:: "" #:: "" #:: "" #:: "buzz" #:: buzz
val fizzbuzz = fizz.zip(buzz).zipWithIndex.map{ case (((s, t), i)) => if ((s + t).length == 0) (i + 1) else s + t}
fizzbuzz.take(20).foreach(println)
case class Bonus[A](amt: A)
def tax10(amt: Double): Double = if (amt > 100) amt * 0.9D else 0D
def tax20(amt: Double): Double = if (amt > 200) amt * 0.8D else 0D
trait Mappable[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
implicit val bonusMappable: Mappable[Bonus] = new Mappable[Bonus] {
@sshark
sshark / PascalTri.scala
Created November 14, 2019 16:47
Pascal Triangle
object PascalTri {
def triangle(level: Int): List[List[Int]] = {
def triGen(loop: Int, xs: List[Int], acc: List[List[Int]]): List[List[Int]] =
if (loop == 0) acc
else {
val ts = 1 +: xs.sliding(2).map(_.sum).toList :+ 1
triGen(loop - 1, ts, acc :+ ts)
}
triGen(level - 2, List(1, 1), List(List(1), List(1, 1)))
}
@sshark
sshark / CustomSyncImplExample.scala
Created October 20, 2019 04:35
An example of using Simulacrum and how NOT to include a certain typeclass in the function type declaration
/*
* This example demonstrates how NOT to include Monad[F[_]] in the function `foo`
* type declaration but yet it is implicitly part of the for-comprehension. In
* additional, it shows how Simulacrum reduces the amount of boilerplate required.
*/
package org.teckhooi
import simulacrum._
@sshark
sshark / cats-effect.scala
Created April 25, 2019 15:30
Cats Effect worksheet with amm
import $ivy.`org.typelevel::cats-effect:1.2.0`
import cats.effect._
import cats.effect.concurrent._
import cats.syntax.all._
import scala.concurrent.ExecutionContext.global
import cats.effect.ContextShift
implicit val cs = IO.contextShift(global)
@sshark
sshark / fs2-fibs,scala
Created April 22, 2019 13:58
Fibonacci sequence using FS2
import fs2._
val fibs:Stream[Pure, Int] = Stream(0) ++ fibs.scan(1)(_ + _)
fibs.take(10).compile.toList.foreach(println)
@sshark
sshark / MyThing.scala
Created March 29, 2019 11:42
Not really my thing, it is copied from https://github.com/ChristopherDavenport/log4cats and modified to compile and run
import cats.effect.{ExitCode, IO, IOApp, Sync}
import io.chrisdavenport.log4cats.Logger
import io.chrisdavenport.log4cats.slf4j.Slf4jLogger
object MyThing extends IOApp {
// Arbitrary Local Function Declaration
def doSomething[F[_]: Sync]: F[Unit] = {
// Impure But What 90% of Folks I know do with log4s
implicit def unsafeLogger[F[_]: Sync] = Slf4jLogger.getLogger[F]
@sshark
sshark / LightningFastCoinChange.scala
Created February 24, 2019 17:46
Execution speed comparison between mutable and immutable lists using Dynamic Programming method
// Fast solution using mutable list i.e. mutable.ArrayBuffer
import scala.annotation.tailrec
import collection.mutable.ArrayBuffer
object LightningFastCoinChange extends App {
def coinChange(coins: Array[Int], amount: Int): Int = {
val acc = ArrayBuffer.fill(amount + 1)(0)
@tailrec