Skip to content

Instantly share code, notes, and snippets.

View zeroflag's full-sized avatar

Attila Magyar zeroflag

View GitHub Profile
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.
@zeroflag
zeroflag / gist:5903181
Created July 1, 2013 18:12
Java transaction with higher order message
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;
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;
USE: math.parser
USE: math.ranges
: multiple-of ( x y -- ? )
mod 0 = ;
: fizz ( x -- str )
3 multiple-of [ "fizz" ] [ "" ] if ;
: buzz ( x -- str )
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".
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()
@zeroflag
zeroflag / gist:7301376
Created November 4, 2013 11:40
lazy groovy
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})
@zeroflag
zeroflag / RBGenerateEqualHashRefactoring
Created December 30, 2013 23:23
Generate equals and hashcode with Pharo
r := RBGenerateEqualHashRefactoring
className: #SomeClass
variables: #(anInstVar1 anInstVar2).
r execute
@zeroflag
zeroflag / gist:8d5718ac3e208b852b84
Created June 8, 2014 16:30
Bowling Kata in 140 character
score: rolls frame: count
count > 10 ifTrue: [^ 0].
^ (self frameScore: rolls) +
(self score: (self rollsLeft: rolls) frame: count + 1)
@zeroflag
zeroflag / gist:7d20e45c6e46a7c6eb54
Last active August 29, 2015 14:02
Bowling Kata in 140 character
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 ]