This file contains 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.util.List; | |
import java.util.Optional; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class TreeTest { | |
static class Tree { | |
final int value; |
This file contains 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 IOMonadExercise extends App { | |
sealed trait IO[A] | |
case class Return[A](value: A) extends IO[A] | |
case class Suspend[A](f: () => A) extends IO[A] | |
case class FlatMap[A, B](io: IO[A], cont: A => IO[B]) extends IO[B] |
This file contains 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 BinaryWatch extends App { | |
def hasBitCount(count: Int)(binTime: Int): Boolean = Integer.bitCount(binTime) == count | |
case class Time(hour: Int, minute: Int) | |
def parseTime(time: Int): Option[Time] = { | |
val hour = time >> 6 | |
val minute = time & ((1 << 6) - 1) |
This file contains 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.util.function.Predicate; | |
import java.util.stream.Stream; | |
public class Solution { | |
private int indexOfPred(String s, Predicate<Character> p) { | |
for (int i = 0; i < s.length(); i++) { | |
if (p.test(s.charAt(i))) { | |
return i; | |
} |
This file contains 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
def splitWhile[A](it: Seq[A])(pred: A => Boolean): (Seq[A], Seq[A]) = { | |
(it.takeWhile(pred), it.dropWhile(pred)) | |
} | |
def splitParenContentsAndRest(s: Seq[Char]): (Seq[Char], Seq[Char]) = { | |
assert(s.head == '[') | |
val parenCounts = s.scanLeft(0) { | |
case (parenCount, '[') => parenCount + 1 |
This file contains 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 keypad: Seq[Seq[Int]] = Seq(Seq(1, 2, 3), Seq(4, 5, 6), Seq(7, 8, 9)) | |
case class Pos(row: Int, col: Int) | |
sealed trait Dir | |
case object Up extends Dir | |
case object Left extends Dir | |
case object Right extends Dir | |
case object Down extends Dir |
This file contains 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 testing | |
import cats._ | |
import cats.data._ | |
import cats.implicits._ | |
object Main2 extends App { | |
case class Stats(warning: Int, error: Int) |
This file contains 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._ | |
import cats.data._ | |
import cats.implicits._ | |
case class User(age: Int) | |
def complicatedFunction[F[_]: Traverse, G[_]: Applicative, A: Monoid](ids: F[Int], fetchUser: Int => G[User], consume: User => A): G[A] = { | |
ids.traverse(fetchUser) | |
.map(_.foldMap(consume)) |
This file contains 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._ | |
import cats.data._ | |
import cats.implicits._ | |
case class User(age: Int) | |
// notice F: FlatMap; fetchUser returning G[F[User]], and the use of flatTraverse | |
def complicatedFunction[F[_]: Traverse : FlatMap, G[_]: Applicative, A: Monoid](ids: F[Int], fetchUser: Int => G[F[User]], consume: User => A): G[A] = { | |
ids.flatTraverse(fetchUser) |
This file contains 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 testing | |
import org.scalatest.FunSuite | |
import shapeless.labelled.FieldType | |
import shapeless.{:+:, CNil, Coproduct, Generic, HNil, Inl, Inr, LabelledGeneric, Lazy, Witness} | |
trait EnumCodec[T] { | |
def encode: T => String | |
def decode: String => Option[T] | |
} |
OlderNewer