Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
loicdescotte / Forcomptran.md
Last active May 27, 2023 06:27
Scala for comprehension translation helper

Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

@sadache
sadache / 1-post.md
Created September 5, 2012 22:14
Applicatives are too restrictive, breaking Applicatives and introducing Functional Builders

This post aims to document a practical design implementation we came up with when designing some APIs in Scala. Of course the concept is not Scala specific and applies to other functional languages.

If you don't want to go into the full introduction, this post talks about how Applicatives are too restrictive and breaking them into two independent components can be interesting for Contravariant and Invariant Functors. Jump to implementation attached.

We are taking JSON serialization/deserialization as a motivating example.

JsValue is the name of the type that represents the JSON AST. What we need is to be able to read a JsValue and produce an A:

trait Reads[A]{
@guillaumebort
guillaumebort / Secured.scala
Created April 7, 2012 12:05
HTTP Basic Authorization for Play 2.0
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) { request =>
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter { encoded =>
new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request))
}.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""")
@oodavid
oodavid / README.md
Last active March 11, 2025 21:41 — forked from aronwoost/README.md
Deploy your site with git

Deploy your site with git

This gist assumes:

  • you have a local git repo
  • with an online remote repository (github / bitbucket etc)
  • and a cloud server (Rackspace cloud / Amazon EC2 etc)
    • your (PHP) scripts are served from /var/www/html/
    • your webpages are executed by apache
  • apache's home directory is /var/www/
dcs@ayanami:~/tmp$ scala BenchCode 100000 5 100
Warming up Adrian, functional/indexOf
Warming up Adrian, functional/span
Warming up Adrian, imperative/ArrayBuffer
Warming up Adrian, imperative/ListBuffer
Warming up user unknown, recursive
Warming up user unknown, fold
Warming up paradigmatic, recursive
Warming up paradigmatic, fold
Warming up soc
@dcsobral
dcsobral / BenchCode.scala
Created August 17, 2011 21:00
Benchmark code for algorithms in this Stack Overflow question regarding Java vs Scala performance: http://stackoverflow.com/q/7084212/53013
// Question at http://stackoverflow.com/q/7084212/53013
// Results at https://gist.github.com/1152764
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
import scala.util.Random
object BenchCode extends scala.testing.Benchmark with Algorithms {
var xs: List[Int] = Nil
var acc = 0
var code: List[Int] => List[Int] = (identity _)