Skip to content

Instantly share code, notes, and snippets.

View emmettna's full-sized avatar

Emmett Na emmettna

  • Ottawa
View GitHub Profile
val five = 5
five match {
case v if v > 5 => ???
case v if v == 5 => ???
case v if v < 5 => ???
}
// more practical example
final case class FeeInquiry(user: UserId, amount: BigDecimal, currency: Currency)
val requestedFee: Option[FeeInquiry] = ???
final case class Human(name: String, age: Int)
trait SimpleCrud[F[_]] {
val simpleCrudService: SimpleCrud.Service[F]
}
object SimpleCrud {
trait Service[F[_]] {
def add: F[Unit]
def find(name: Name): F[Option[Human]]
}
}
import cats.Applicative
import cats.effect.concurrent.Ref
import cats.implicits._
import cats.effect._
class InMemorySimpleCrud[F[_]: Applicative](ref: Ref[F, Map[String, Human]])
extends SimpleCrud.Service[F] {
override def add(human: Human): F[Unit] =
ref.update(_.updated(human.name, human))
override def find(name: String): F[Option[Human]] =
@emmettna
emmettna / gcd.scala
Created March 11, 2021 02:03
greaest common divisor in scala using java math
import java.math.BigInteger
def gcd(left: Long, right: Long): Long = BigInteger.valueOf(left).gcd(BigInteger.valueOf(right)).longValue
@emmettna
emmettna / speared_square_cell_numbers.scala
Created March 11, 2021 02:11
calculate cell numbers hidden behind a spear line
// porivided width * height
// calculate the number of empty cells
// ex) [ ][ ][/]
// [ ][/][ ]
//. [/][ ][ ]
//
// gcd : https://gist.github.com/emmettna/702d972126d381dcceb8d8122ba7893c
def calcEmptyCells(width: Int, heght: Int): Int = width + heght - (gcd(width, heght))