This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String value = getValueFromDb("key") // possibly null | |
if (value != null) { | |
// continue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Event(id: Long, name: String, when: Long, source: EventSource) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Event { | |
private final long id; | |
private final String name; | |
private final long when; | |
private final EventSource source; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val odds = collection.filter(n => n % 2 == 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Integer> odds = new ArrayList<Integer>(); | |
for (int n : collectionOfNumbers) { | |
if (n % 2 == 1) { | |
odds.add(n); | |
} | |
} |
NewerOlder