Skip to content

Instantly share code, notes, and snippets.

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import cats.effect.{IO, Timer}
import cats._
import cats.implicits._
object X extends App {
def recoverTimeout[A](fa: IO[A], after: FiniteDuration, fallback: A)
(implicit timer: Timer[IO]): IO[A] = {
def volume(box: Box) = (box.width, box.length, box.height).mapN( _ * _ * _)
def volume( box: Box) = for {
width <- box.width
length <- box.length
height <- box.height
} yield width * height * length
trait Semigroup[A] {
def combine(x: A, y: A): A
}
trait Monoid[A] extends Semigroup[A] {
def zero: A
}
// Laws:
//Associativity
def getAllDatesCats(products: List[String]) = {
val futureAllDates = Future.sequence(products.map(p ⇒ getDates(p)))
futureAllDates.map(l ⇒ l.combineAll.toOption)
}
def getAllDatesScala(products: List[String]) = {
val futureAllDates = Future.sequence(products.map(p ⇒ getDates(p)))
futureAllDates.map(l ⇒
if (l.exists(_.isLeft))
None
else
Some(l.foldLeft(List[LocalDate]())((acc, e) =>
e match {
case Left(_) => acc
case Right(l) => acc ++ l
def getDates(product: String): Future[Either[String, List[LocalDate]]] = product match {
case "fast" => Future.successful(Right(List(LocalDate.parse("2018-03-09"), LocalDate.parse("2018-03-10"))))
case "slow" => Future.successful(Right(List(LocalDate.parse("2018-03-07"), LocalDate.parse("2018-03-08"))))
case "express" => Future.successful(Left("Service not available"))
case "flash" => Future.failed(new Throwable("haha"))
}
@kolov
kolov / build.gradle
Last active February 17, 2017 10:25
Tag and push an image created by gradle-docker to an alternative repository
// Will tag an push if the property 'dockerRepository' is defined
task buildDocker(type: Docker) {
push = false
applicationName = 'curriculi-service-users'
dockerfile = file('src/main/docker/Dockerfile')
doFirst {
copy {
from jar
@kolov
kolov / mobile-browser.clj
Last active January 2, 2016 15:10
mobile browser detection in Clojure
; Mobile browser detection based on the JSP code at http://detectmobilebrowsers.com/
(defn mobile-browser? [req]
(if-let [agent (get-in req [:headers "user-agent"])]
(let [agent (.toLowerCase agent)]
(or
(re-matches (Pattern/compile (str "(?i).*((android|bb\\\\d+|meego)"
".+mobile|avantgo|bada\\\\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)"
"|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)"
"\\\\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\\\\.(browser|link)|vodafone|wap|windows ce|xda|xiino).*"))
@kolov
kolov / Time & Locale
Last active August 29, 2015 14:21
Print date in different locales
package _;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;