Skip to content

Instantly share code, notes, and snippets.

@Timshel
Timshel / Constraints.scala
Last active December 20, 2015 01:58 — forked from playxamplez-admin/CODE
Define a #play2 #form #constraint for #required #optional mapping
import play.api.data.validation._
trait Constraints {
/**
* Defines a ‘required’ constraint for `Optional` values, i.e. one in which None are invalid.
*
* '''name'''[constraint.required]
* '''error'''[error.required]
*/
def nonNone[A]: Constraint[Option[A]] = Constraint[Option[A]]("constraint.required") { o =>
@Timshel
Timshel / CustomMessagesPlugin.scala
Last active December 20, 2015 01:58 — forked from playxamplez-admin/CODE
#play2.1 custom #MessagesPlugin to add messages from different sources
package play.api.i18n
import play.api._
import play.api.i18n._
import scala.collection.JavaConverters._
import scalax.file._
import scalax.io.JavaConverters._
@Timshel
Timshel / Atomic.scala
Last active December 20, 2015 01:29 — forked from playxamplez-admin/CODE
#scala #atomic sugar from https://gist.github.com/piotrga/1986175
import annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
class Atomic[T](val atomic : AtomicReference[T]) {
@tailrec
final def update(f: T => T) : T = {
val oldValue = atomic.get()
val newValue = f(oldValue)
if (atomic.compareAndSet(oldValue, newValue)) newValue else update(f)
@Timshel
Timshel / Application.scala
Last active December 20, 2015 01:29 — forked from playxamplez-admin/CODE
#multiple #select example using play2 master
package controllers
import play.api._
import play.api.data._
import play.api.data.Forms._
import play.api.mvc._
object Application extends Controller {
val form = Form(

If your csv doesn't contain escaped newlines then it is pretty easy to do a progressive parsing without putting the whole file into memory. The iteratee library comes with a method search inside play.api.libs.iteratee.Parsing :

def search (needle: Array[Byte]): Enumeratee[Array[Byte], MatchInfo[Array[Byte]]]

which will partition your stream into Matched[Array[Byte]] and Unmatched[Array[Byte]]

Then you can combine a first iteratee that takes a header and another that will fold into the umatched results. This should look like the following code:

// break at each match and concat unmatches and drop the last received element (the match)
This is the fork of Xamplez core Configuration Gist
@Timshel
Timshel / Atomic.scala
Created July 12, 2013 11:50 — forked from piotrga/gist:1986175
Atomic Sugar
import annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
class Atomic[T](val atomic : AtomicReference[T]) {
@tailrec
final def update(f: T => T) : T = {
val oldValue = atomic.get()
val newValue = f(oldValue)
if (atomic.compareAndSet(oldValue, newValue)) newValue else update(f)
@Timshel
Timshel / solution.scala
Last active December 14, 2015 09:18 — forked from mandubian/solution.scala
How to create a form #scala #form #modified
Form(
tuple(
"numero" -> optional(nonEmptyText(6,6)),
"nom" -> cleanText(1,64),
"prenom" -> cleanText(1,64),
"coefficient" -> number
)
)