Skip to content

Instantly share code, notes, and snippets.

View gabro's full-sized avatar

Gabriele Petronella gabro

View GitHub Profile
package buildo
import anorm._
import shapeless._
import shapeless.ops.hlist._
import shapeless.record._
import shapeless.syntax.std.traversable._
import shapeless.syntax.singleton._
@gabro
gabro / gist:b192a7258ec5072bd1e0
Created September 12, 2014 10:17
Beautiful validation with scalaz. Fail fast in the for-comprehension and fail slow with the application builder
def editResult(testId: LabOnlineId[Test], worklistId: LabOnlineId[Worklist], result: TestResult): Future[ActionResult] =
for {
test <- ensureExists(testId)(testData.findOne(_))
sample <- ensureExists(test._sampleId)(sampleData.findOneWithWorklist(_, worklistId))
} yield {
(ensureNotClosed(sample, worklistId) |@|
ensureNotClosed(test, worklistId))((_,_)) match {
case Failure(errors) => ActionResult.UserError(errors.toList: _*)
case Success(_) => /* run a stored procedure */ ActionResult.Ok
}
@gabro
gabro / A
Created September 13, 2014 14:56
Var length generator
def gen(n: Int, limit: Int, k: Int = 1): Iterator[List[Int]] = {
val range = (k until limit).iterator
n match {
case _ if n > 0 => for {
x <- range
y <- gen(n - 2, limit, x)
} yield x :: y
case _ => range.map(List(_))
}
}
@gabro
gabro / Main.scala
Last active August 29, 2015 14:11
Automatic get parameters generator from case class for spray routing
package example
import scala.language.existentials
import shapeless._; import record._; import ops.record._; import ops.hlist._
import spray.routing.SimpleRoutingApp
trait AutoGetParametersModule { self: SimpleRoutingApp =>
import spray.routing.{ Directive, Directive1, Directive0, HListDeserializer }
@gabro
gabro / Matrix
Created December 19, 2014 01:55
dependent type Matrix with implicit conversion from Vect m (Vect n a)
module Matrix
data Matrix : Nat -> Nat -> Type -> Type where
Matr : Vect m (Vect n a) -> Matrix n m a
(+) : Matrix n m a -> Vect n a -> Matrix n (m + 1) a
(+) (Matr rows) row = Matr (rows ++ [row])
implicit vectVectToMatr : Vect m (Vect n a) -> Matrix n m a
vectVectToMatr = Matr
@gabro
gabro / iff.js
Last active August 29, 2015 14:13 — forked from utaal/iff.js
let if = macro {
rule {
($x:expr) {
$truebody
...
$truelast:expr
} else {
$falsebody
...
$falselast:expr
@gabro
gabro / transformers-ftw.scala
Last active August 29, 2015 14:14
OptionT exercise
import scalaz._; import Scalaz._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
def getInteger(x: Int): OptionT[Future, Int] = 42.point[Future].liftM[OptionT]
def computeList(n: Int): OptionT[Future, List[Int]] = (1 to n).toList.traverseU(getInteger)
//if you need a Future[OptionT[List[Int]]] just use run
computeList(3).run // Some(List(1, 2, 3))
@gabro
gabro / vanilla-scala.scala
Last active August 29, 2015 14:14
Vanilla Scala exercise
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
def getInteger(x: Int) = Future.successful(
if (x == 11) None else Some(x))
def sequence[T](l: Seq[Option[T]]): Option[List[T]] = l.foldRight(Some(Nil) : Option[List[T]]) {
(or, ox) => for (r <- or; x <- ox) yield (r :: x)
}
@gabro
gabro / Messages.scala
Last active August 29, 2015 14:21
rapture-i18n + ICU
object Messages {
type IString = I18nString[En with It]
val cart: IString = en"Shopping cart" | it"Carrello"
val login: IString = en"Login" | it"Accedi"
val betReplayedBy: IString =
en"""
Ticket replayed by {n, plural,
API.cancelConsultation(consultation._id)
|> showHUDOnStart(status: "canceling consultation")
|> showHUDOnSuccessAndError
|> start()