Skip to content

Instantly share code, notes, and snippets.

Gabriele Petronella gabro

Block or report user

Report or block gabro

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View shapeless compilation time exploding
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 Sep 12, 2014
Beautiful validation with scalaz. Fail fast in the for-comprehension and fail slow with the application builder
View gist:b192a7258ec5072bd1e0
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 Sep 13, 2014
Var length generator
View A
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 Aug 29, 2015
Automatic get parameters generator from case class for spray routing
View Main.scala
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 Dec 19, 2014
dependent type Matrix with implicit conversion from Vect m (Vect n a)
View Matrix
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 Aug 29, 2015 — forked from utaal/iff.js
View iff.js
let if = macro {
rule {
($x:expr) {
$truebody
...
$truelast:expr
} else {
$falsebody
...
$falselast:expr
@gabro
gabro / vanilla-scala.scala
Last active Aug 29, 2015
Vanilla Scala exercise
View vanilla-scala.scala
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)
}
View transformers-ftw.scala
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 / Messages.scala
Last active Aug 29, 2015
rapture-i18n + ICU
View Messages.scala
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,
View RAC-SVProgressHUD.swift
API.cancelConsultation(consultation._id)
|> showHUDOnStart(status: "canceling consultation")
|> showHUDOnSuccessAndError
|> start()
You can’t perform that action at this time.