Skip to content

Instantly share code, notes, and snippets.

View zsolt-donca's full-sized avatar
🥸

Zsolt Donca zsolt-donca

🥸
View GitHub Profile
@zsolt-donca
zsolt-donca / TreeStream.java
Last active August 8, 2016 12:58
java.util.Stream for a binary tree; checking if it's sorted, min, max; first two values
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;
@zsolt-donca
zsolt-donca / IOMonadExercise.scala
Created November 11, 2016 08:07
Why does this Scala code type check even though it shouldn't?
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]
@zsolt-donca
zsolt-donca / BinaryWatch.scala
Created February 16, 2017 11:32
A scala solution to the binary watch problem: https://leetcode.com/problems/binary-watch/
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)
@zsolt-donca
zsolt-donca / StringDecode.java
Created February 23, 2017 10:45
Java solution to the string decoding problem: https://leetcode.com/problems/decode-string/
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;
}
@zsolt-donca
zsolt-donca / StringDecode.sc
Created February 23, 2017 10:49
Scala solution (as a worksheet) to the string decoding problem: https://leetcode.com/problems/decode-string
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
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
@zsolt-donca
zsolt-donca / Main2.scala
Created June 7, 2017 19:44
Playing around with Cats' State monad
package testing
import cats._
import cats.data._
import cats.implicits._
object Main2 extends App {
case class Stats(warning: Int, error: Int)
@zsolt-donca
zsolt-donca / example-with-traverse-and-validated-nel.scala
Last active May 23, 2018 20:18
Part 1: Followup of the FP meetup "Abstractions of higher kind" - using ValidatedNel
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))
@zsolt-donca
zsolt-donca / example-with-traverse-and-writer.scala
Last active May 23, 2018 20:19
Part 2: Followup of the FP meetup "Abstractions of higher kind" - using Writer
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)
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]
}