Skip to content

Instantly share code, notes, and snippets.

@max-l
Created March 23, 2010 00:17
Show Gist options
  • Save max-l/340714 to your computer and use it in GitHub Desktop.
Save max-l/340714 to your computer and use it in GitHub Desktop.
import WhileZ._
whileZ(somethting) {
println("!!!!")
doSomething()
}
object WhileZ {
def whileZ(b: =>Boolean)(u: Unit) =
val condition = b _
val block = u _
while(condition()) (block())
}
}
def assignAuthors(docFile: File) = {
val doc = loadDocument(docFile)
for(author <- doc.authors) {
assignAuthor(author, doc)
Logger.logDebug(lookupInDatabaseById(author.id).toString + "->" + docFile)
}
}
trait Logger {
var logLevel = LogLevel.Verbose
def logDebug(message: =>String) =
if(logLevel == LogLevel.Debug)
doLog(message)
def doLog(message: String): Unit
}
object DSL {
val vitesse = 5.3.km / (15.minutes - 5.seconds)
assert(vitesse.isInstanceOf[Speed])
implicit def int2Time(i: Int) = new TimeUnit(i)
implicit def float2Distance(f: float) = new Distance(f)
class TimeUnit(milliseconds: Long) {
def seconds(i: Int) = new TimeUnit(i * 1000)
def minutes(i: Int) = new TimeUnit(i * 1000 * 60)
def -(t: TimeUnit) = new TimeUnit(this.milisseconds = t.milliseconds)
}
class Speed(d: Distance, t: TimeUnit)
class Distance(val millimeters: Float) {
def km(f: Float) = new Distance(f * 1000^2)
def / (t: TimeUnit) = new Speed(d, t)
}
}
object MatchingExample {
def toText(p: Person) = p match {
case t:Tourist(_, _, c:Car(_, _, l:License(num))) =>
case t:Tourist(_, _, c:Bicycle(_)) => ""
case e:Employee(_, _) =>
}
}
trait KeyedEntity[K] {
def id: K
}
trait Optimistic {
self: KeyedEntity[_] =>
protected val occVersionNumber = 0
}
class Book(val id: Long, title: String)
extends KeyedEntity[Long] with Optimistic
trait Map[K,V] {
def get(k: K): Option[V]
}
public class Verbosity {
enum Gender {
MALE,
FEMALE
}
class Person {
private String _firstName;
private String _lastName;
private Gender _gender;
private int _age;
public String getFirstName() {
return _firstName;
}
public void setFirstName(String firstName) {
this._firstName = firstName;
}
public String getLastName() {
return _lastName;
}
public void setLastName(String lastName) {
this._lastName = lastName;
}
public Gender getGender() {
return _gender;
}
public void setGender(Gender gender) {
this._gender = gender;
}
public int getAge() {
return _age;
}
public void setAge(int age) {
this._age = age;
}
}
public Map<Gender, Map<Integer, List<Person>>>
groupIntoGenderAndAge(List<Person> crowd) {
ArrayList<Person> boys = new ArrayList<Person>();
ArrayList<Person> girls = new ArrayList<Person>();
for(Person p : crowd) {
if(p.getGender().equals(Gender.FEMALE))
girls.add(p);
else
boys.add(p);
}
HashMap<Gender, Map<Integer, List<Person>>> res =
new HashMap<Gender, Map<Integer, List<Person>>>();
res.put(Gender.FEMALE, _groupByAge(girls));
res.put(Gender.MALE, _groupByAge(boys));
return res;
}
private Map<Integer, List<Person>>
_groupByAge(List<Person> crowd) {
HashMap<Integer, List<Person>> res =
new HashMap<Integer, List<Person>>();
for(Person p : crowd) {
List ageGroup = res.get(p.getAge());
if(ageGroup == null) {
ageGroup = new ArrayList<Person>();
res.put(p.getAge(), ageGroup);
}
ageGroup.add(p);
}
return res;
}
}
object Gender extends Enumeration {
type Gender = Value
val Male, Female = Value
}
import Gender._
class Person(val firstName: String, val lastName: String,
val gender: Gender, age: Int)
object Grouper {
def groupIntoGenderAndAge(crowd: List[Person]) =
crowd.groupBy(p => p.gender).mapValues(genderSet => genderSet.groupBy(p=>p.age)
}
class XmlSample {
// Ceci retourne un noeud DOM !
def allo(s: String) = <p>
<i>{s}</i>
</p>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment