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 scala.language.implicitConversions | |
| trait Show[A] { | |
| def show(a: A): String | |
| } | |
| trait ShowOps { | |
| def show: String |
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.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; |
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.bar; | |
| import java.util.Map; | |
| import java.util.Objects; | |
| public class Leaf implements Tree { | |
| private String value; | |
| public Leaf(String value) { | |
| this.value = value; |
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 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 |
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 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) |
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.{Applicative, ApplicativeError} | |
| import cats.implicits._ | |
| object ApplicationErrorExampleApp extends App { | |
| val f: List[Int] => Int = (xs: List[Int]) => xs.sum | |
| val g: List[Int] => Int = (xs: List[Int]) => xs.size | |
| val xs = List(List(1, 2, 3)) |
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
| /* | |
| * Setting the right time for the targeted zone e.g. Asia/Singapore | |
| * LocalDateTime.now(ZoneId.of("Asia/Singapore")) // set the time of the targeted time zone | |
| * .atZone(ZoneId.of("Asia/Singapore")) // set the time zone ONLY for the time above. Doesn't affect time | |
| * .toEpochSecond() // find the UTC time for the time at the timezone and convert it to seconds | |
| */ | |
| System.out.println(LocalDateTime.now(ZoneId.of("Asia/Singapore")).atZone(ZoneId.of("Asia/Singapore")).toEpochSecond()); | |
| /* |
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
| 2021-05-19 03:07:06,927 ERROR dev.miku.r2dbc.mysql.client.ReactorNettyClient [reactor-tcp-epoll-1] Error: readAddress(..) failed: Connection reset by peer | |
| io.netty.channel.unix.Errors$NativeIoException: readAddress(..) failed: Connection reset by peer | |
| 2021-05-19 03:07:06,928 ERROR reactor.core.publisher.Operators [reactor-tcp-epoll-1] Operator called default onErrorDropped | |
| dev.miku.r2dbc.mysql.client.MySqlConnectionClosedException: Connection closed | |
| at dev.miku.r2dbc.mysql.client.ClientExceptions.expectedClosed(ClientExceptions.java:36) | |
| at dev.miku.r2dbc.mysql.client.ReactorNettyClient.handleClose(ReactorNettyClient.java:262) | |
| at dev.miku.r2dbc.mysql.client.ReactorNettyClient.access$400(ReactorNettyClient.java:53) | |
| at dev.miku.r2dbc.mysql.client.ReactorNettyClient$ResponseSubscriber.onComplete(ReactorNettyClient.java:306) | |
| at reactor.core.publisher.Operators$MultiSubscriptionSubscriber.onComplete(Operators.java:2016) | |
| at reactor.core.publisher.Operators$MonoSubscriber.onComplete(Operators.java:1824) |
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 Foo(title: String, ids: Set[String]) | |
| val xs = | |
| List((123, "aa", "bb"), (233, "aa", "cc"), (233, "aa", ""), (233, "aa", "cc"), (334, "bb", "ee")) | |
| val m = xs.groupBy(_._2).map { case (k, v) => Foo(k, v.map(_._3).filter(_.nonEmpty).toSet) } | |
| println(m) | |
| /* |
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 cats.effect.{ExitCode, IO, IOApp} | |
| import cats.implicits.catsSyntaxEitherId | |
| import scala.util.Try | |
| object EitherErrorHandlingInIO extends IOApp { | |
| override def run(args: List[String]): IO[ExitCode] = { |