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
| sealed trait Thing { | |
| val id: Int | |
| } | |
| case class ThingOne(id: Int) extends Thing | |
| case class ThingTwo(id: Int, code: String) extends Thing | |
| object Thing { |
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
| object StringUtils { | |
| implicit class RichString(s: String) { | |
| /** | |
| * Take a string and int representing the desired string length. | |
| * Front pads the string with a char up to the desired length | |
| * | |
| * @param l length of string | |
| * @param c char to pad | |
| * @return |
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.Functor | |
| import cats.instances.list._ // for Functor | |
| import cats.instances.option._ // for Functor | |
| /** | |
| * Example 1 | |
| */ | |
| val doubleList: List[Double] = List(1.2, 2.55, 3.1) | |
| val listTimes: Double => Double = x => Math.round({ |
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 org.joda.time.LocalDateTime | |
| import scala.concurrent.{ExecutionContext, Future} | |
| /** | |
| * Very simple thread-safe cache implementation with basic support for cache invalidation | |
| */ | |
| class TimedCache[K, V](private val store: scala.collection.concurrent.Map[K, (V, LocalDateTime)], | |
| daysCacheValidFor: Int) { |
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
| def lift3[A, B, C, D](f: Function3[A, B, C, D]): Function3[Option[A], Option[B], Option[C], Option[D]] = { | |
| (oa: Option[A], ob: Option[B], oc: Option[C]) => | |
| for { | |
| a <- oa | |
| b <- ob | |
| c <- oc | |
| } yield f(a, b, c) | |
| } | |
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
| /** | |
| * A monoid is a type with a combine operation of type (A, A) => A | |
| * and an identity operation of type A | |
| * | |
| * Note that a semgigroup is just the combine part of a Monoid. | |
| * Here we have split the type up into traits as some types | |
| * for which we cannot define an identity element. | |
| */ | |
| trait Semigroup[A] { | |
| def combine(x: A, y: A): A |
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
| sealed trait Json | |
| final case class JsObject(get: Map[String, Json]) extends Json | |
| final case class JsString(get: String) extends Json | |
| final case class JsNumber(get: Double) extends Json | |
| case object JsNull extends Json |
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 scala.util.Try | |
| // Allows a function A => B to operate on options | |
| def lift[A, B](f: A => B): Option[A] => Option[B] = _ map f //(a: Option[A]) => a.map(f) | |
| val cbrtO: Option[Double] => Option[Double] = lift(math.cbrt) | |
| val r1 = Try(throw new RuntimeException).toOption | |
| val r2 = Try(1000d).toOption |
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
| def retry[T](op: => Future[T], retries: Int): Future[T] = | |
| op.recoverWith { case _ if retries > 0 => (retry(op, retries - 1)) } |
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 com.bfil.api.{FutureValidation, TypedFutureValidation} | |
| case class CustomError(msg: String) | |
| object CustomResult extends TypedFutureValidation[CustomError] | |
| def retry[T](attempts: Int, f: => FutureValidation[CustomError, T]): FutureValidation[CustomError, T] = { | |
| def attempt(n: Int): FutureValidation[CustomError, T] = n match { | |
| case n if n > 0 => f.map { |