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
| // 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)) |
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 java.math.BigInteger | |
| def gcd(left: Long, right: Long): Long = BigInteger.valueOf(left).gcd(BigInteger.valueOf(right)).longValue |
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 | |
| 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]] = |
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
| 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]] | |
| } | |
| } |
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 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] = ??? |
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
| "1,2,3".split(",").toList match { | |
| case first :: second :: third :: Nil => "Counting Practice" | |
| case _ => throw new Exception("This is not what I expected") | |
| } |
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 list: List[Option[Int]] = List(Some(1), None, Some(2)) | |
| val result: List[Int]=list.map( numberOpt => numberOpt match { | |
| case Some(v) => v | |
| case None => 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
| final case class DummyCaseClass(value: Int) | |
| val dummyCaseClassOpt = Option(DummyCaseClass(1)) | |
| dummyCaseClassOpt match { | |
| case Some(DummyCaseClass(v)) => v.toString | |
| case Some(_) => ??? | |
| case None => ??? | |
| } | |
| // equivalent | |
| dummyCaseClassOpt match { | |
| case v: Some[DummyCaseClass] => v.toString |
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 optionValue = Option(1) | |
| optionValue match { | |
| case Some(v) => ??? | |
| case None => ??? | |
| } |
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
| final case class IntegerParser(value: Int) | |
| val prasedInt = IntegerPraser(value = 1) | |
| parsedInt match { | |
| case IntegerParser(value) => value.toString | |
| case _ => "Invalid Int" | |
| } |
NewerOlder