Skip to content

Instantly share code, notes, and snippets.

View JoolsF's full-sized avatar

Julian Fenner JoolsF

  • London
View GitHub Profile
@JoolsF
JoolsF / unapply_example1.scala
Created July 5, 2018 11:11
Unapply example 1
sealed trait Thing {
val id: Int
}
case class ThingOne(id: Int) extends Thing
case class ThingTwo(id: Int, code: String) extends Thing
object Thing {
@JoolsF
JoolsF / string_pad1.scala
Created June 26, 2018 08:29
String pad example 1
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
@JoolsF
JoolsF / cats_functor_example1.scala
Created May 28, 2018 18:26
Cats functor example 1
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({
@JoolsF
JoolsF / simple_cache1.scala
Created May 22, 2018 10:39
Simple cache example 1
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) {
@JoolsF
JoolsF / lift_example2.scala
Created May 3, 2018 09:24
Lift example 2
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)
}
@JoolsF
JoolsF / monoids1.scala
Last active April 28, 2018 16:43
monoid example 1
/**
* 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
@JoolsF
JoolsF / type_class_example2.scala
Last active April 11, 2018 21:35
Type class example 2
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
@JoolsF
JoolsF / lift_example1.scala
Last active May 3, 2018 09:24
Lift function example 1
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
@JoolsF
JoolsF / future_retry1.scala
Created December 13, 2017 12:20
Simple future retry example 1
def retry[T](op: => Future[T], retries: Int): Future[T] =
op.recoverWith { case _ if retries > 0 => (retry(op, retries - 1)) }
@JoolsF
JoolsF / future_validation_retry.scala
Last active December 13, 2017 12:19
Scalaz FutureValidation retry
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 {