kata
不久前,我使用Java 8流和lambda解决了FizzBuzz kata问题。 尽管最终结果是可行的,但中间步骤却没有。 我当然可以做得更好。
与往常一样,让我们从失败的测试开始:
package remonsinnema.blog.fizzbuzz;
+
+ import static org.junit.Assert.assertEquals;
+
+ import org.junit.Test;
+
+
+ public class WhenFunctionallyFuzzingAndBuzzing {
+
+ private final FizzBuzzer fizzBuzzer = new FizzBuzzer();
+
+ @Test
+ public void shouldReplaceMultiplesOfThreeWithFizzAndMultiplesOfFiveWithBuzz() {
+ assertEquals(“1”, “1”, fizzBuzzer.apply(1));
+ }
+
+ }
package remonsinnema.blog.fizzbuzz;
+
+ import java.util.function.Function;
+
+
+ public class FizzBuzzer implements Function<Integer, String> {
+
+ @Override
+ public String apply(Integer n) {
+ return null;
+ }
+
+ }
请注意,我马上就使用Java的Function
开始了函数课程。
我伪造实现以使测试通过:
public class FizzBuzzer implements Function<Integer, String> {@Overridepublic String apply(Integer n) {
– return null;
+ return “1”;}}
并重构测试以消除重复:
public class WhenFunctionallyFuzzingAndBuzzing {@Testpublic void shouldReplaceMultiplesOfThreeWithFizzAndMultiplesOfFiveWithBuzz() {
– assertEquals(“1”, “1”, fizzBuzzer.apply(1));
+ assertFizzBuzz(“1”, 1);
+ }
+
+ private void assertFizzBuzz(String expected, int value) {
+ assertEquals(Integer.toString(value), expected, fizzBuzzer.apply(value));}}
然后,我添加另一个测试以概括实现:
public class WhenFunctionallyFuzzingAndBuzzing {@Testpublic void shouldReplaceMultiplesOfThreeWithFizzAndMultiplesOfFiveWithBuzz() {assertFizzBuzz(“1”, 1);
+ assertFizzBuzz(“2”, 2);}private void assertFizzBuzz(String expected, int value) {public class FizzBuzzer implements Function<Integer, String> {@Overridepublic String apply(Integer n) {
– return “1”;
+ return Integer.toString(n);}}
好的,到目前为止非常标准的东西。 接下来,我需要将3替换为“ Fizz”:
public class WhenFunctionallyFuzzingAndBuzzing {public void shouldReplaceMultiplesOfThreeWithFizzAndMultiplesOfFiveWithBuzz() {assertFizzBuzz(“1”, 1);assertFizzBuzz(“2”, 2);
+ assertFizzBuzz(“Fizz”, 3);}nbsp;private void assertFizzBuzz(String expected, int value) {public class FizzBuzzer implements Function<Integer, String> {@Overridepublic String apply(Integer n) {
– return Integer.toString(n);
+ return numberReplacerFor(n).apply(n);
+ }
+
+ private Function<Integer, String> numberReplacerFor(Integer n) {
+ return n == 3
+ ? i -> “Fizz”
+ : i -> Integer.toString(i);}}
在这里,我认识到我需要根据输入应用两个函数之一。 该代码有效,但需要进行一些清理。 首先,作为垫脚石,我将lambda提取到各个字段中:
import java.util.function.Function;public class FizzBuzzer implements Function<Integer, String> {
+ private final Function<Integer, String> replaceNumberWithStringRepresentation
+ = n -> Integer.toString(n);
+ private final Function<Integer, String> replaceNumberWithFizz
+ = n -> “Fizz”;
+@Overridepublic String apply(Integer n) {return numberReplacerFor(n).apply(n);private Function<Integer, String> numberReplacerFor(Integer n) {return n == 3
– ? i -> “Fizz”
– : i -> Integer.toString(i);
+ ? replaceNumberWithFizz
+ : replaceNumberWithStringRepresentation;}}
接下来,我强调通过提取一个类将“ 3”和“ Fizz”结合在一起:
public class FizzBuzzer implements Function<Integer, String> {private final Function<Integer, String> replaceNumberWithStringRepresentation= n -> Integer.toString(n);
– private final Function<Integer, String> replaceNumberWithFizz
– = n -> “Fizz”;
+ private final Fizzer replaceNumberWithFizz = new Fizzer();@Overridepublic String apply(Integer n) {}private Function<Integer, String> numberReplacerFor(Integer n) {
– return n == 3
+ return replaceNumberWithFizz.test(n)? replaceNumberWithFizz: replaceNumberWithStringRepresentation;}
+ package remonsinnema.blog.fizzbuzz;
+
+ import java.util.function.Function;
+ import java.util.function.Predicate;
+
+
+ public class Fizzer implements Function<Integer, String>, Predicate<Integer> {
+
+ @Override
+ public boolean test(Integer n) {
+ return n == 3;
+ }
+
+ @Override
+ public String apply(Integer n) {
+ return “Fizz”;
+ }
+
+ }
在这里,我使用标准的Java Predicate
功能接口。
要添加“ Buzz”,我需要将代码从单个if
(隐藏为三元运算符)推广到循环:
public class WhenFunctionallyFuzzingAndBuzzing {assertFizzBuzz(“1”, 1);assertFizzBuzz(“2”, 2);assertFizzBuzz(“Fizz”, 3);
+ assertFizzBuzz(“4”, 4);
+ assertFizzBuzz(“Buzz”, 5);}private void assertFizzBuzz(String expected, int value) {package remonsinnema.blog.fizzbuzz;
+ import java.util.Arrays;
+ import java.util.Collection;import java.util.function.Function;private final Function<Integer, String> replaceNumberWithStringRepresentation= n -> Integer.toString(n);
– private final Fizzer replaceNumberWithFizz = new Fizzer();
+ private final Collection<ReplaceNumberWithFixedText> replacers = Arrays.asList(
+ new ReplaceNumberWithFixedText(3, “Fizz”),
+ new ReplaceNumberWithFixedText(5, “Buzz”)
+ );@Overridepublic String apply(Integer n) {}private Function<Integer, String> numberReplacerFor(Integer n) {
– return replaceNumberWithFizz.test(n)
– ? replaceNumberWithFizz
– : replaceNumberWithStringRepresentation;
+ for (ReplaceNumberWithFixedText replacer : replacers) {
+ if (replacer.test(n)) {
+ return replacer;
+ }
+ }
+ return replaceNumberWithStringRepresentation;}}
package remonsinnema.blog.fizzbuzz;
–
– import java.util.function.Function;
– import java.util.function.Predicate;
–
–
– public class Fizzer implements Function<Integer, String>, Predicate<Integer> {
–
– @Override
– public boolean test(Integer n) {
– return n == 3;
– }
–
– @Override
– public String apply(Integer n) {
– return “Fizz”;
– }
–
– }
package remonsinnema.blog.fizzbuzz;
+
+ import java.util.function.Function;
+ import java.util.function.Predicate;
+
+
+ public class ReplaceNumberWithFixedText implements Function<Integer, String>,
+ Predicate<Integer> {
+
+ private final int target;
+ private final String replacement;
+
+ public ReplaceNumberWithFixedText(int target, String replacement) {
+ this.target = target;
+ this.replacement = replacement;
+ }
+
+ @Override
+ public boolean test(Integer n) {
+ return n == target;
+ }
+
+ @Override
+ public String apply(Integer n) {
+ return replacement;
+ }
+
+ }
糟糕,旧习惯……那应该是一个循环,而不是循环:
import java.util.function.Function;public class FizzBuzzer implements Function<Integer, String> {
– private final Function<Integer, String> replaceNumberWithStringRepresentation
+ private final Function<Integer, String> defaultReplacer= n -> Integer.toString(n);private final Collection<ReplaceNumberWithFixedText> replacers = Arrays.asList(new ReplaceNumberWithFixedText(3, “Fizz”),}private Function<Integer, String> numberReplacerFor(Integer n) {
– for (ReplaceNumberWithFixedText replacer : replacers) {
– if (replacer.test(n)) {
– return replacer;
– }
– }
– return replaceNumberWithStringRepresentation;
+ return replacers.stream()
+ .filter(replacer -> replacer.test(n))
+ .map(replacer -> (Function<Integer, String>) replacer)
+ .findFirst()
+ .orElse(defaultReplacer);}}
好多了。 下一个测试是用于倍数的:
public class WhenFunctionallyFuzzingAndBuzzing {assertFizzBuzz(“Fizz”, 3);assertFizzBuzz(“4”, 4);assertFizzBuzz(“Buzz”, 5);
+ assertFizzBuzz(“Fizz”, 6);}private void assertFizzBuzz(String expected, int value) {public class FizzBuzzer implements Function<Integer, String> {private final Function<Integer, String> defaultReplacer= n -> Integer.toString(n);
– private final Collection<ReplaceNumberWithFixedText> replacers = Arrays.asList(
– new ReplaceNumberWithFixedText(3, “Fizz”),
– new ReplaceNumberWithFixedText(5, “Buzz”)
+ private final Collection<ReplaceMultipleWithFixedText> replacers = Arrays.asList(
+ new ReplaceMultipleWithFixedText(3, “Fizz”),
+ new ReplaceMultipleWithFixedText(5, “Buzz”));@Override
+ package remonsinnema.blog.fizzbuzz;
+
+ import java.util.function.Function;
+ import java.util.function.Predicate;
+
+
+ public class ReplaceNumberWithFixedText implements Function<Integer, String>,
+ Predicate<Integer> {
+
+ private final int target;
+ private final String replacement;
+
+ public ReplaceNumberWithFixedText(int target, String replacement) {
+ this.target = target;
+ this.replacement = replacement;
+ }
+
+ @Override
+ public boolean test(Integer n) {
+ return n % target == 0;
+ }
+
+ @Override
+ public String apply(Integer n) {
+ return replacement;
+ }
+
+ }
– package remonsinnema.blog.fizzbuzz;
–
– import java.util.function.Function;
– import java.util.function.Predicate;
–
–
– public class ReplaceNumberWithFixedText implements Function<Integer, String>, Predicate<Integer> {
–
– private final int target;
– private final String replacement;
–
– public ReplaceNumberWithFixedText(int target, String replacement) {
– this.target = target;
– this.replacement = replacement;
– }
–
– @Override
– public boolean test(Integer n) {
– return n == target;
– }
–
– @Override
– public String apply(Integer n) {
– return replacement;
– }
–
– }
最后一个测试是结合Fizz和Buzz:
public class WhenFunctionallyFuzzingAndBuzzing {assertFizzBuzz(“4”, 4);assertFizzBuzz(“Buzz”, 5);assertFizzBuzz(“Fizz”, 6);
+ assertFizzBuzz(“7”, 7);
+ assertFizzBuzz(“8”, 8);
+ assertFizzBuzz(“Fizz”, 9);
+ assertFizzBuzz(“Buzz”, 10);
+ assertFizzBuzz(“11”, 11);
+ assertFizzBuzz(“Fizz”, 12);
+ assertFizzBuzz(“13”, 13);
+ assertFizzBuzz(“14”, 14);
+ assertFizzBuzz(“FizzBuzz”, 15);}private void assertFizzBuzz(String expected, int value) {package remonsinnema.blog.fizzbuzz;import java.util.Arrays;import java.util.Collection;import java.util.function.Function;
+ import java.util.stream.Collectors;
+ import java.util.stream.Stream;public class FizzBuzzer implements Function<Integer, String> {@Overridepublic String apply(Integer n) {
– return numberReplacerFor(n).apply(n);
+ return numberReplacersFor(n)
+ .map(function -> function.apply(n))
+ .collect(Collectors.joining());}
– private Function<Integer, String> numberReplacerFor(Integer n) {
– return replacers.stream()
+ private Stream<Function<Integer, String>> numberReplacersFor(Integer n) {
+ return Stream.of(replacers.stream().filter(replacer -> replacer.test(n)).map(replacer -> (Function<Integer, String>) replacer).findFirst()
– .orElse(defaultReplacer);
+ .orElse(defaultReplacer));}}
我概括了单一Function
到一个Stream
的Function
S,而我应用的map-reduce模式。 我本可以使用.reduce("", (a, b) -> a + b)
类的东西来拼写Reduce的部分,但是我认为Collectors.joining()
更具表现力。
这还没有通过测试,因为我返回了单个函数的流。 该修复有些棘手,因为我需要知道是否找到了任何适用的替换器函数,并且您必须在不终止stream的情况下才能做到这一点 。 所以我需要使用StreamSupport
创建一个新的流:
package remonsinnema.blog.fizzbuzz;import java.util.Arrays;import java.util.Collection;
+ import java.util.Iterator;
+ import java.util.Spliterators;import java.util.function.Function;import java.util.stream.Collectors;import java.util.stream.Stream;
+ import java.util.stream.StreamSupport;public class FizzBuzzer implements Function<Integer, String> {}private Stream<Function<Integer, String>> numberReplacersFor(Integer n) {
– return Stream.of(replacers.stream()
+ Iterator<Function<Integer, String>> result = replacers.stream().filter(replacer -> replacer.test(n)).map(replacer -> (Function<Integer, String>) replacer)
– .findFirst()
– .orElse(defaultReplacer));
+ .iterator();
+ return result.hasNext()
+ ? StreamSupport.stream(Spliterators.spliteratorUnknownSize(result, 0), false)
+ : Stream.of(defaultReplacer);}}
就是这样。 完整代码在GitHub上 。
我从这个小练习中学到了两个教训:
- Java附带了一堆功能接口,例如
Function
和Predicate
,可以很容易地与流结合以解决各种问题。 -
if → while
转换的标准变成if → stream
在功能世界中if → stream
。
翻译自: https://www.javacodegeeks.com/2016/08/functional-fizzbuzz-kata-java.html
kata