Skip to content

Instantly share code, notes, and snippets.

View mariussoutier's full-sized avatar

Marius Soutier mariussoutier

View GitHub Profile
@mariussoutier
mariussoutier / Mail.scala
Created August 23, 2012 12:13
Sending mails fluently in Scala
package object mail {
implicit def stringToSeq(single: String): Seq[String] = Seq(single)
implicit def liftToOption[T](t: T): Option[T] = Some(t)
sealed abstract class MailType
case object Plain extends MailType
case object Rich extends MailType
case object MultiPart extends MailType
package controllers
import play.api._
import play.api.mvc._
import play.api.libs.oauth._
import play.api.libs.ws._
import play.api.libs.iteratee._
object Twitter extends Controller {
@mariussoutier
mariussoutier / Time.scala
Created August 8, 2012 09:19
Executes a given block of code and prints the elapsed time
def time[T](block: => T): T = {
val start = System.currentTimeMillis
val res = block
val totalTime = System.currentTimeMillis - start
println("Elapsed time: %1d ms".format(totalTime))
res
}
@mariussoutier
mariussoutier / togglewatch.js
Last active August 13, 2018 23:55
Toggle an AngularJS $watch expression. The default $watch can only be toggled once.
var toggleWatch = function(watchExpr, fn) {
var watchFn;
return function() {
if (watchFn) {
watchFn();
watchFn = undefined;
console.log("Disabled " + watchExpr);
} else {
watchFn = $scope.$watch(watchExpr, fn);
console.log("Enabled " + watchExpr);
@mariussoutier
mariussoutier / JsonFormats.scala
Last active December 6, 2016 07:19
ReactiveMongo Play Plugin Extensions
package json
import reactivemongo.bson._
import reactivemongo.bson.handlers.DefaultBSONHandlers._
import play.api.libs.json._
import play.api.libs.json.Json._
import play.api.libs.json.util._
import play.api.libs.json.Writes._
import play.api.libs.functional.syntax._
@mariussoutier
mariussoutier / Callbacks.coffee
Created June 12, 2013 07:46
Different ways of calling functions with call-backs (i.e. multiple parameters that are functions) in CoffeeScript.
# Separate functions
success = (data, status, header) ->
promise.resolve data.user
error = (data, status, header) ->
promise.reject data.error
user = getUserById id, success, error
# Comma in front of each function
user = getUserById id
@mariussoutier
mariussoutier / 1Problem.scala
Last active December 16, 2015 05:49
Bridge Pattern in Scala
// Adding new functionality via inheritance leads to combinatorial explanation
abstract class Serializer {
def write(string: String): Unit
}
class CsvFileWriter extends FileWriter {
def writeFile(file: java.io.File) = //...
}
# Map to language via RegEx
/$language<[a-z]{2}>/videos/:id controllers.Video.showVideo(id: Long, language: String)
# Hard-code supported languages
/de/videos/:id controllers.Video.showVideo(id: Long, language = "de")
/en/videos/:id controllers.Video.showVideo(id: Long, language = "en")
@mariussoutier
mariussoutier / Helper.scala
Created August 21, 2012 11:46
More template sequence helpers
package views.html.helper
import play.api.templates.Html
// Executes the first block if non-empty, the second otherwise
// Passes the entire seq (as in defining)
object ifEmptyOrElse {
def apply[T <: Seq[_]](t: T)(nonEmptyBlock: (T) => Html)(emptyBlock: => Html) = {
if (t.nonEmpty) nonEmptyBlock(t) else emptyBlock
}
@mariussoutier
mariussoutier / Helper.scala
Created August 7, 2012 14:28
Helper for using sequences in Play Scala templates
package views.html.helper
import play.api.templates.Html
object definingNonEmpty {
def apply[T <: Seq[_]](t: T)(block: (T) => Html) = {
if (t.nonEmpty) block(t) else Html("")
}
}