This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(() -> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import fs2._ | |
| val fibs:Stream[Pure, Int] = Stream(0) ++ fibs.scan(1)(_ + _) | |
| fibs.take(10).compile.toList.foreach(println) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |