Skip to content

Instantly share code, notes, and snippets.

import java.util.ArrayList;
import java.util.List;
class Employee {
private Double dailyRate;
public Employee(Double dailyRate) {
this.dailyRate = dailyRate;
}
case class Employee(val dailyRate: Double) {
def getDailyRate = dailyRate
}
trait Calculator {
def calculate(dailyRate: Double, noOfDays: Int): Double
}
trait Calendar {
def noOfDays: Int
class User(val firstName: String, val lastName: String)
trait UserDao {
def findByFirstName(firstName: String): User
}
object defaultUserDao extends UserDao {
override def findByFirstName(firstName: String) = new User(firstName, "Lewis")
}
var EventAttacher = Class.create({
events: "click,focus".split(","),
initialize: function(element, handler) {
this.element = $(element);
this.handler = handler;
this.events.each(this._buildObservationMethod.bind(this));
},
Array.prototype.toList = function() {
var list = new Element("ul")
$A(this).each(function(e) { list.appendChild(new Element("li").update(e)) } )
return list
}
document.body.appendChild([1,2,3].toList())
trait XmlRenderer {
val handlers = Map(
"span" -> ((n: Node) =>
<handled>
<div class="boink">
{n.label}
</div>
</handled>
)
)
/*
* An object for building an object of handler methods, which can
* can then be added via Element.addMethods. Example result:
*
* {
* mousedown: function(element, fn) {
* element = $(element);
* element.observe("mousedown", fn.bind(element));
* return element;
* },
/*
* Extension that makes it easy to convert an array to a "hash"
* (a bland JSON object), given an array of desired keys.
*/
Array.prototype.toObject = function(keys) {
var obj = {};
for(var i = 0; i < keys.length; i++) {
obj[keys[i]] = this[i];
}
return obj;
/*
* Recursive xml transformation util. Provides a simplistic tool for
* registering transforms as functions, which will be called recursively
* on each node of the tree.
*/
import scala.xml._
trait XmlRenderer {
val handlers: Map[(Node) => Boolean, (Node) => Node]
/*
* A curried emulation of Iterable#map.
*
* map(List(1,2,3)) { _ + 2} //List[Int] = List(3, 4, 5)
*/
def map[T](l: List[T])(f: (T) => T): List[T] = {
def doMap[T](l: List[T], f: (T) => T, acc: List[T]): List[T] = l match {
case Nil => acc
case x :: tail => doMap(tail, f, acc ::: List(f(x)))
}