Skip to content

Instantly share code, notes, and snippets.

View beranradek's full-sized avatar

Radek Beran beranradek

View GitHub Profile
package com.spinoco.dojo
import scalaz.concurrent.Task
import scalaz.stream.{ process1, Process }
import Process._
import scala.concurrent.duration._
import scalaz.stream.io
/*
Time to put it all together.
package com.spinoco.dojo
import scalaz.stream.Process
import Process._
import scalaz.concurrent.Task
import scala.util.Random
object Dojo extends Dojo
trait Dojo {
private static final FormMapping<Registration> registrationForm =
Forms.automatic(Registration.class, "registration")
.nested(Forms.automatic(Address.class, "contactAddress",
Forms.factoryMethod(Address.class, "getInstance")).build())
.build();
private static final FormMapping<RegDate> regDateMapping =
Forms.basic(RegDate.class, "regDate").fields("month", "year").build();
private static final FormMapping<Registration> registrationForm =
Forms.basic(Registration.class, "registration")
// whitelist of properties to bind
.fields("attendanceReasons", "cv", "interests", "email")
.nested(Forms.automatic(UploadedFileWrapper.class, "certificates",
null, MappingType.LIST).build())
.nested(Forms.basic(Address.class, "contactAddress",
Forms.factoryMethod(Address.class, "getInstance"))
private static final FormMapping<Person> personForm =
Forms.automatic(Person.class, "person").build();
FormData<Person> formData = new FormData<Person>(person, null);
FormMapping<Person> filledForm = personForm.fill(formData);
// push filledForm to template …
FormData<Person> formData = personForm.bind(
new HttpServletRequestParams(request));
if (formData.isValid()) {
/**
* Dumps (possibly large) database table to SQL file or more files
* according to MaxRecordsPerFile parameter.
* @author Radek Beran
*/
import java.io._
import java.sql._
// --- SCRIPT PARAMETERS ---
val DriverName = "com.mysql.jdbc.Driver"
val parSeq: ParSeq[Int] = List(3, 4, 5).par
(for {
m <- models.view // vytvoří non-strict view pamatující si lazy vykonávané operace
if m.carMaker == CarMaker.Porsche
price <- m.price // transformace na kolekci obsahující ceny
} yield price).sum
val parSeq: ParSeq[Int] = List(3, 4, 5).par
// Posčítání cen automobilů, jejichž výrobcem je Porsche:
case class CarModel(carMaker: CarMaker.Value, name: String, price: Option[Int])
val models: List[CarModel] = List(...)
models.view.filter(_.carMaker == CarMaker.Porsche).map(_.price.getOrElse(0)).sum