Skip to content

Instantly share code, notes, and snippets.

@sshark
sshark / ShowMeTheMoneyApp.scala
Last active July 21, 2022 14:52
Demonstrate contravariant Functor with Show typeclass without using Cats
package org.teckhooi
import scala.language.implicitConversions
trait Show[A] {
def show(a: A): String
}
trait ShowOps {
def show: String
@sshark
sshark / WhosFirst.java
Created June 27, 2022 16:03
Who is first?
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;
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;
@sshark
sshark / FooBar.scala
Created February 4, 2022 16:49
Throwing Exception vs Using Either
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
@sshark
sshark / MaxDiff.scala
Last active October 23, 2021 15:06
Max difference between increasing elemets
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)
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))
@sshark
sshark / RightTimeAndZone.java
Created June 23, 2021 03:05
Getting the right UTC at the targeted time zone
/*
* 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());
/*
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)
@sshark
sshark / simple-grouping.scala
Last active February 28, 2021 13:35
The amount of noise reduced for a simple group and map task
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)
/*
@sshark
sshark / EitherErrorHandlingInIO.scala
Created February 13, 2021 12:18
How to display the exact error at each stage
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] = {