forked from tim-group/FunctionalJavaDojo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mike/Marc/Chris: FootballMunging is done using tupled return type, on…
…e down, one to go
- Loading branch information
Marc Siegel
committed
May 16, 2012
1 parent
d6fe6db
commit d713ced
Showing
2 changed files
with
76 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,39 @@ | ||
package org.timgroup.dojo; | ||
|
||
import fj.data.Stream; | ||
|
||
public class FootballMunging { | ||
|
||
private final Stream<String> lines; | ||
|
||
public FootballMunging(Stream<String> lines) { | ||
this.lines = lines; | ||
} | ||
|
||
public String teamWithSmallestDifferenceBetweenForAndAgainst() { | ||
return ""; | ||
} | ||
} | ||
package org.timgroup.dojo; | ||
|
||
import fj.F; | ||
import fj.P2; | ||
import fj.data.Stream; | ||
|
||
import static fj.Ord.intOrd; | ||
import static fj.Ord.stringOrd; | ||
import static fj.Ord.p2Ord; | ||
import static fj.P.p; | ||
|
||
public class FootballMunging { | ||
private final Stream<String> lines; | ||
|
||
public FootballMunging(Stream<String> lines) { | ||
this.lines = lines; | ||
} | ||
|
||
public String teamWithSmallestDifferenceBetweenForAndAgainst() { | ||
return filterLines(lines).map(new F<String, P2<Integer, String>>() { | ||
@Override public P2<Integer, String> f(String s) { | ||
return parseLine(s); | ||
} | ||
}).sort(p2Ord(intOrd, stringOrd)).head()._2(); | ||
} | ||
|
||
public static Stream<String> filterLines(Stream<String> footballLines) { | ||
return footballLines.filter(new F<String, Boolean>() { | ||
@Override public Boolean f(String s) { | ||
return s.contains(" - "); | ||
} | ||
}); | ||
} | ||
|
||
public static P2<Integer, String> parseLine(String line) { | ||
final String[] fields = line.split("\\s+"); | ||
return p(Math.abs(Integer.valueOf(fields[7]) - Integer.valueOf(fields[9])), fields[2]); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,37 @@ | ||
package org.timgroup.dojo; | ||
|
||
import fj.data.Stream; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.timgroup.dojo.Files.readLinesFrom; | ||
|
||
public class FootballMungingTest { | ||
|
||
@Test | ||
public void teamWithSmallestDifferenceBetweenForAndAgainstIsAstonVilla() { | ||
Stream<String> footballLines = readLinesFrom("football.dat"); | ||
assertThat(new FootballMunging(footballLines).teamWithSmallestDifferenceBetweenForAndAgainst(), is("Aston_Villa")); | ||
} | ||
} | ||
package org.timgroup.dojo; | ||
|
||
import fj.P2; | ||
import fj.data.Stream; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.timgroup.dojo.Files.readLinesFrom; | ||
|
||
public class FootballMungingTest { | ||
private Stream<String> footballLines; | ||
|
||
@Before | ||
public void setup() { | ||
footballLines = readLinesFrom("football.dat"); | ||
} | ||
|
||
@Test | ||
public void teamWithSmallestDifferenceBetweenForAndAgainstIsAstonVilla() { | ||
assertThat(new FootballMunging(footballLines).teamWithSmallestDifferenceBetweenForAndAgainst(), is("Aston_Villa")); | ||
} | ||
|
||
@Test | ||
public void there_are_20_lines_of_football_stats() { | ||
assertThat(FootballMunging.filterLines(footballLines).length(), is(20)); | ||
} | ||
|
||
@Test | ||
public void parses_relevant_info_out_of_a_line() { | ||
final String firstLine = " 1. Arsenal 38 26 9 3 79 - 36 87"; | ||
final P2<Integer, String> arsenal = FootballMunging.parseLine(firstLine); | ||
assertThat(arsenal._1(), is(43)); | ||
assertThat(arsenal._2(), is("Arsenal")); | ||
} | ||
} |