Skip to Content

Java 8 - FooBarQix - ListMaker

You might be familiar with the FooBarQix kata. Last year the CodeStory team used this kata for the first selection round of the challenge. You can read a few submissions here (French).

Here are the rules:

Here’s what I wrote with Java 8:

String[] codes = {"", "", "", "Foo", "", "Bar", "", "Qix", "", ""};

String fooBarQix = with(3, 5, 7)
  .only(divisor -> (n % divisor) == 0)
  .concat(n / 100, (n % 100) / 10, n % 10)
  .map(v -> codes[v])
  .join();

return fooBarQix.isEmpty() ? n.toString() : fooBarQix;

To be honest, this is not plain java 8 code. It uses the library ListMaker I started to write in 2008 to test-drive “functional programming” in Java with Guava. It is available on Maven Central. Feel free to use it and improve it.

It is very similar to Java 8’s Streams or Guava’s FluentIterable introduced a couple of versions back. But I find ListMaker more complete and less verbose. Tell me what you think.

comments powered by Disqus