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 class ExtendedList extends ArrayList { | |
@Override | |
public int size() { | |
return 10; | |
} | |
} | |
System.out.println(new ExtendedList().isEmpty()); | |
The output of this depends on the implementation of the super class. | |
It may return false if the isEmpty() uses size(), or return true otherwise. |
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 class Transactor { | |
public <T> T inTx(T obj) { | |
return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), | |
obj.getClass().getInterfaces(), | |
new TransactionProxy<T>(obj)); | |
} | |
private static class TransactionProxy<T> implements InvocationHandler { | |
private final T object; | |
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 class Exceptions { | |
public static <T> T shouldThrow(T obj, Class<? extends Exception> exceptionClass) { | |
return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), | |
obj.getClass().getInterfaces(), | |
new ExceptionChecker<T>(obj, exceptionClass)); | |
} | |
public static class ExceptionChecker<T> implements InvocationHandler { | |
private final Object object; | |
private final Class<? extends Exception> exceptionClass; |
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
USE: math.parser | |
USE: math.ranges | |
: multiple-of ( x y -- ? ) | |
mod 0 = ; | |
: fizz ( x -- str ) | |
3 multiple-of [ "fizz" ] [ "" ] if ; | |
: buzz ( x -- str ) |
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
USE: math.parser | |
USE: math.ranges | |
GENERIC: fizzbuzzify ( obj -- str ) | |
! The fizzbuzzify method defined on the integer class | |
M: integer fizzbuzzify | |
number>string ; | |
! This is the predicate class multiple-of-3. The predicate is "n mod 3 = 0". |
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 NaturalNumber: | |
def is_zero(self): pass | |
def predecessor(self): pass | |
def successor(self): return Successor(self) | |
def __add__(self, other): | |
if other.is_zero(): | |
return self | |
else: | |
return self.successor() + other.predecessor() |
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 LazyList { | |
private Object head | |
private Closure tail | |
static cons(Object first, Closure rest) { | |
new LazyList(head: first, tail: rest) | |
} | |
static getNil() { | |
LazyList.cons(null, {-> null}) |
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
r := RBGenerateEqualHashRefactoring | |
className: #SomeClass | |
variables: #(anInstVar1 anInstVar2). | |
r execute |
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
score: rolls frame: count | |
count > 10 ifTrue: [^ 0]. | |
^ (self frameScore: rolls) + | |
(self score: (self rollsLeft: rolls) frame: count + 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
score: rolls frame: count | |
count > 10 ifTrue: [^ 0]. | |
^ (self frameScore: rolls) + | |
(self score: (self rollsLeft: rolls) frame: count + 1) | |
frameScore: rolls | |
^ ((self isStrike: rolls) or: [ self isSpare: rolls ]) | |
ifTrue: [ (rolls first: 3) sum ] | |
ifFalse: [ (rolls first: 2) sum ] | |
OlderNewer