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 zoneId = ZoneId.systemDefault() | |
| def timing(i: Int): IO[Unit] = | |
| IO.realTimeInstant.flatMap { start => | |
| IO.sleep(i.seconds) >> IO.realTimeInstant.flatMap { end => | |
| IO.println(s"$i ${start.atZone(zoneId).toLocalTime} ${end.atZone(zoneId).toLocalTime}") | |
| } | |
| } | |
| def run: IO[Unit] = | |
| Stream |
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
| test("dispatcher.parallel") { | |
| val a1 = IO.println("a1 start") >> IO.sleep(3.seconds) >> IO.println("a1 done") | |
| val a2 = IO.println("a2 start") >> IO.sleep(1.seconds) >> IO.println("a2 done") | |
| Dispatcher.parallel[IO].use { dispatcher => | |
| IO { | |
| dispatcher.unsafeRunAndForget(a1) | |
| dispatcher.unsafeRunAndForget(a2) | |
| } >> IO.sleep(5.seconds) | |
| }.unsafeRunSync() |
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
| for { | |
| d <- IO.deferred[Int] | |
| a <- d.complete(1) | |
| b <- d.complete(2) | |
| } yield (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 fs2.Stream | |
| val r1 = Stream.resource(Resource.make(IO.println("acquire 1"))(_ => IO.println("release 1"))) | |
| val r2 = Stream.bracket(IO.println("acquire 2"))(_ => IO.println("release 2")) | |
| val ss = | |
| r1.onFinalizeWeak(IO.println("final 1")) | |
| .flatMap(_ => r2.onFinalize(IO.println("final 2"))).evalMap(_ => IO.println("do work")) | |
| ++ fs2.Stream.eval(IO.println("----")) ++ fs2.Stream.sleep[IO](2.second) | |
| ss.compile.drain.unsafeRunSync() |
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 a1 = for { | |
| _ <- IO.println("a1") | |
| _ <- IO.sleep(5.seconds) | |
| } yield 1 | |
| val a2 = for { | |
| _ <- IO.println("a2") | |
| _ <- IO.sleep(5.seconds) | |
| } yield 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
| import better.files._ | |
| object GridlandMetro extends App { | |
| // https://www.hackerrank.com/challenges/gridland-metro/problem | |
| def gridlandMetro(n: Int, m: Int, k: Int, track: Array[Array[Long]]): Long = { | |
| val total = n * m.toLong | |
| val exist = track | |
| .groupBy(_(0)) | |
| .map { case (_, arr) => |
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
| // https://www.hackerrank.com/challenges/bfsshortreach/problem | |
| def height(neighbors: Map[Int, Set[Int]], level: Int, stack: Set[Int], rst: Map[Int, Int]): Map[Int, Int] = | |
| if (stack.isEmpty) rst | |
| else { | |
| val known = stack.filterNot(rst.contains).map((_ -> level)).toMap ++ rst | |
| val set = stack.flatMap(neighbors.get).flatten.filterNot(known.contains) | |
| height(neighbors, level + 1, set, known) | |
| } |
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
| // https://www.hackerrank.com/challenges/journey-to-the-moon/problem | |
| object journeyToMoon { | |
| final case class DisjointSet(ds: Map[UUID, Set[Int]] = Map.empty) { | |
| def find(v1: Int, v2: Int): Vector[UUID] = | |
| ds.flatMap { case (k, v) => if (v.contains(v1) || v.contains(v2)) Some(k) else None }.toVector | |
| def exists(v: Int): Boolean = ds.exists(_._2.contains(v)) |
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.syntax.all._ | |
| import cats.data._ | |
| import cats._ | |
| val f = Validated.invalid[Eval[String], Eval[Int]](Eval.always { println("eval fail"); "oops" }) | |
| val s = Validated.valid[Eval[String], Eval[Int]](Eval.always { println("eval succ"); 1 }) | |
| val combine: Validated[Eval[String], Eval[Int]] = f *> s | |
| val res = combine.bimap(_.value, _.value) match { |
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
| // https://www.hackerrank.com/challenges/insertion-sort/problem | |
| def insertionSort(arr: Array[Int]): Int = | |
| arr | |
| .foldLeft((TreeMap.empty[Int, Int], 0L)) { case ((tm, s), (v)) => | |
| val d = tm.rangeFrom(v + 1).values.sum | |
| val up = tm.updatedWith(v) { | |
| case Some(x) => Some(x + 1) | |
| case None => Some(1) | |
| } |
NewerOlder