Skip to content

Instantly share code, notes, and snippets.

@julienrf
julienrf / JFunction1.java
Created December 3, 2011 17:55
Using scala.Function1 from Java
package lambda;
public interface JFunction1<T1, R> {
R apply(T1 t);
}
@julienrf
julienrf / implementation.scala
Created December 3, 2011 15:08
Querying JSON
package jsonquery
object `package` {
import dispatch.json._
implicit def jsonToSearchable(json: JsValue) = new {
def \ (selector: String): Either[String, JsValue] = json match {
case JsObject(map) => {
(for {
@julienrf
julienrf / foo.tmpl
Created December 1, 2011 15:11
forest
{* Example of template processing an article *}
{article: Article}
li class="article {article.featured ? 'featured'}" /view.root
a href="/details"
| {article.name} /data.name
span.actions
button type=button /view.editBtn | Edit
button type=button /view.deleteBtn | Delete
@julienrf
julienrf / 1_Secured.scala
Last active September 28, 2015 06:08
Dependency injection in Scala with Play 2: it’s free
object MyApp extends Controller {
// Home page
val index = Action {
Ok(views.html.index())
}
// Login action: here I just bind a (non empty) username from the query and put it in the session
val login = Action { implicit request =>
Form(mapping("username" -> nonEmptyText)(identity)(Some(_))).bindFromRequest.fold(
noUser => redirectToIndex,
object Happy {
def sumSquare(ds: List[Int]): Int = ds map (d => d * d) sum
def digits(n: Int): List[Int] = if (n < 10) List(n) else (n % 10) :: digits(n / 10)
def happy(n: Int, visited: List[Int] = Nil): Boolean = sumSquare(digits(n)) match {
case 1 => true
case n => if (visited contains n) false else happy(n, n :: visited)
}
@julienrf
julienrf / jQueryButtons.js
Created October 16, 2011 16:21
Don’t use jQuery to write a web application
$next.attr('disabled', 'disabled');
*{ Liste éditable d’éléments }*
*{ @param _arg: Iterable[A] Liste des éléments constitutifs de la liste }*
*{ @param as: String Name of the iterator }*
*{ @param (optional) addAction: ActionDefinition URL permettant d’ajouter un élément }*
*{ @param (optional) pager: Pager }*
#{ui.list users, as: 'user', addAction: @Users.add(), pager: pager}
#{useritem user /}
#{/ui.list}
*{ Formulaire }*

What do we want to do?

Find a way to easily allow industrial web applications developers to write consistent user interfaces without having to repeat themselves, and to efficiently handle variability.

The user interface is displayed by the web browser from the HTML of the page. Developers write the HTML markup using a template engine. This template engine allows to include parametrized fragments, giving the ability to define some UI traits (capturing one UI concept) and to reuse them across the application.

@julienrf
julienrf / 1_Max.scala
Created April 29, 2011 10:28
Scala typeclass pattern
/** Say we have a `max` function, computing the maximum of two numbers */
def max(a: Int, b: Int): Int = {
if (a < b) b
else a
}
/** It can be used like that: */
max(3, 4)
@julienrf
julienrf / tyco_sample.scala
Created April 20, 2011 12:11
Is it possible to write more expressive things?
import tyco.compiler._
object MyWebsite extends tyco.Site {
for (page <- Content.allPages)
page.uri ==> Template.show(page)
for (file <- Path("stylesheets/*.scss"))
"/css/"+file.name ==> new TextFile(file) with Sass with CssMinifier
}