Skip to content

Instantly share code, notes, and snippets.

@kolov
kolov / bind-req-resp.clj
Last active August 29, 2015 14:03
Ring middleware to bind request and response to variables easy to explore in the repl
(declare dbg-req)
(declare dbg-resp)
(defn bind-req-resp [handler re-pattern]
(fn [req]
(if (re-matches re-pattern (:uri req))
(do
(alter-var-root (var dbg-req) (constantly req))
(let [resp (handler req)]
(alter-var-root (var dbg-resp) (constantly resp))
@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;
@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 / 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
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"))
}
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 getAllDatesCats(products: List[String]) = {
val futureAllDates = Future.sequence(products.map(p ⇒ getDates(p)))
futureAllDates.map(l ⇒ l.combineAll.toOption)
}
trait Semigroup[A] {
def combine(x: A, y: A): A
}
trait Monoid[A] extends Semigroup[A] {
def zero: A
}
// Laws:
//Associativity
def volume( box: Box) = for {
width <- box.width
length <- box.length
height <- box.height
} yield width * height * length
def volume(box: Box) = (box.width, box.length, box.height).mapN( _ * _ * _)