Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
philipschwarz / gist:1059890
Created July 2, 2011 09:23
Before applying "Replace Temp With Query" to totalAmount
public String statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration<Rental> allRentals = rentals.elements();
String result = "Rental record for " + getName() + "\n";
while (allRentals.hasMoreElements())
{
Rental each = (Rental) allRentals.nextElement();
frequentRenterPoints += each.getFrequentRenterPoints();
@philipschwarz
philipschwarz / gist:1059894
Created July 2, 2011 09:31
After applying "Replace Temp with Query" to totalAmount
public String statement()
{
int frequentRenterPoints = 0;
Enumeration<Rental> allRentals = rentals.elements();
String result = "Rental record for " + getName() + "\n";
while (allRentals.hasMoreElements())
{
Rental each = (Rental) allRentals.nextElement();
frequentRenterPoints += each.getFrequentRenterPoints();
@philipschwarz
philipschwarz / gist:1059899
Created July 2, 2011 09:35
After applying "Replace Temp with Query" to frequentRenterPoints
public String statement()
{
Enumeration<Rental> allRentals = rentals.elements();
String result = "Rental record for " + getName() + "\n";
while (allRentals.hasMoreElements())
{
Rental each = (Rental) allRentals.nextElement();
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
@philipschwarz
philipschwarz / Frame.groovy
Created July 12, 2011 21:45
code kata - bowling - groovy - 2011.07.12
package bowling
class Frame
{
public Frame(){}
public Frame(Frame nextFrame)
{
this.nextFrame = nextFrame
}
@philipschwarz
philipschwarz / gist:1100862
Created July 23, 2011 02:09
Functional answer to Naresh Jain's challenge using Jedi
int calculateAveragePreviousPercentageComplete() { return reduceAndAverageActivitiesBy(PREVIOUS_PERCENTAGE_COMPLETE); }
int calculateAverageCurrentPercentageComplete() { return reduceAndAverageActivitiesBy(CURRENT_PERCENTAGE_COMPLETE); }
int calculateAverageProgressPercentage() { return reduceAndAverageActivitiesBy(PROGRESS_PERCENTAGE); }
int reduceAndAverageActivitiesBy( Functor2<Integer, StudentActivityByAlbum, Integer> functor){
return fold(0, activities, functor) / activities.size(); }
static final Functor2<Integer, StudentActivityByAlbum, Integer> PREVIOUS_PERCENTAGE_COMPLETE =
new Functor2<Integer, StudentActivityByAlbum, Integer>(){
@Override public Integer execute(Integer accumulator, StudentActivityByAlbum value) {
@philipschwarz
philipschwarz / gist:1101512
Created July 23, 2011 14:57
2nd solution to Naresh Jain's challenge
int calculateAveragePreviousPercentageComplete() { return sumAndAverageTheValueOfField(PREVIOUS_PERCENTAGE_COMPLETE); }
int calculateAverageCurrentPercentageComplete() { return sumAndAverageTheValueOfField(CURRENT_PERCENTAGE_COMPLETE); }
int calculateAverageProgressPercentage() { return sumAndAverageTheValueOfField(PROGRESS_PERCENTAGE); }
int sumAndAverageTheValueOfField( Functor<StudentActivityByAlbum, Integer> fieldGetter){
return addAll(collect( activities, fieldGetter)) / activities.size(); }
<T> int addAll(Iterable<Integer> items){ return reduce( items, PLUS); }
static final Functor<StudentActivityByAlbum, Integer> PREVIOUS_PERCENTAGE_COMPLETE = new Functor<StudentActivityByAlbum, Integer>(){
@philipschwarz
philipschwarz / Frame.groovy
Created July 30, 2011 23:31
Modified Frame and FrameSequence so that "future" rolls are referenced without linking through "future" frames
package bowling
class Frame
{
public add(roll) { rolls << roll }
public addBonus(roll) { rolls << roll }
public score(){ rolls.sum() }
private rolls = []
@philipschwarz
philipschwarz / Frame.groovy
Created July 30, 2011 23:36
A Frame that models the domain more closely.
package bowling
class Frame
{
public add(roll) { rolls << roll }
public addBonus(roll) { bonusRolls << roll }
public score()
{
if(isStrike() || isSpare())
@philipschwarz
philipschwarz / gist:1241175
Created September 25, 2011 21:27
Separating use from construction - Before
public class BusinessObject {
public void actionMethod() {
// Other things
Service myServiceObject = Service.getInstance();
myServiceObject.doService();
// Other things
}
}
class Service {
@philipschwarz
philipschwarz / gist:1241180
Created September 25, 2011 21:31
Separating use from construction - After
public class BusinessObject {
public void actionMethod() {
// Other things
//No Change!
Servicec myServiceObject = Service.getInstance();
myServiceObject.doService();
// Other things
}
}