Skip to content

Instantly share code, notes, and snippets.

View jakubjanecek's full-sized avatar

Jakub Janeček jakubjanecek

  • Prague, Czech Republic
View GitHub Profile
@jakubjanecek
jakubjanecek / ComplexNumber.scala
Created April 10, 2013 07:58
Implementation of complex numbers from Czech Scala Enthusiasts coding dojo held on April 9th 2013
class ComplexNumber private (val x: Int, val y: Int) {
def +(c: ComplexNumber) = ComplexNumber(x + c.x, y + c.y)
def *(c: ComplexNumber) = ComplexNumber((x * c.x) - (y * c.y), (y * c.x) + (x * c.y))
// higher-order method
def transform(f: ComplexNumber => ComplexNumber) = f(this)
override def toString = s"$x + ${y}i"
String value = getValueFromDb("key") // possibly null
if (value != null) {
// continue
}
ev match {
case Event(id, "event2", when, device) => println("'event2' from device " + device)
case Event(id, name, when, device) if when > System.currentTimeMillis() - 10000 => println("fresh event")
case _ => // ignore
}
if (ev.getName().equals("event2")) {
System.out.println("'event2' from device " + ev.getSource());
} else if (ev.getWhen() > System.currentTimeMillis() - 10000) {
System.out.println("fresh event");
} else {
// ignore
}
def genericSize(obj: AnyRef): Int = obj match {
case x: String => x.length
case x: Array[AnyRef] => x.length
case x: List[AnyRef] => x.size
case x: ObjectWithSize => x.count
case _ => -1
}
private static int genericSize(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
return s.length();
} else if (obj instanceof int[]) {
int[] a = (int[]) obj;
return a.length;
} else if (obj instanceof List) {
List l = (List) obj;
return l.size();
case class Event(id: Long, name: String, when: Long, source: EventSource)
public final class Event {
private final long id;
private final String name;
private final long when;
private final EventSource source;
val odds = collection.filter(n => n % 2 == 1)
List<Integer> odds = new ArrayList<Integer>();
for (int n : collectionOfNumbers) {
if (n % 2 == 1) {
odds.add(n);
}
}