General FizzBuzz: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 1,817: | Line 1,817: | ||
'''For a more complete example see [[/jFizzBuzz|jFizzBuzz]]''' |
'''For a more complete example see [[/jFizzBuzz|jFizzBuzz]]''' |
||
''' Or using a lot of Lambdas ... ''' |
|||
<syntaxhighlight lang="java"> |
|||
import java.util.stream.*; |
|||
import java.util.function.*; |
|||
import java.util.*; |
|||
public class fizzbuzz_general { |
|||
public static void main(String[] args) { |
|||
BiFunction<Integer,String,String> formatter = (i,printThis) -> "".equals(printThis)?Integer.toString(i):printThis; |
|||
List<Function<Integer,String>> cycle_functions = new ArrayList<>(); |
|||
Function<Integer,String> moduloTesters = i -> cycle_functions.stream() |
|||
.map(fcn->fcn.apply(i)) |
|||
.collect(Collectors.joining()); |
|||
Function<Integer,String> fizzBuzz = i -> formatter.apply(i,moduloTesters.apply(i)); |
|||
Stream.of(args) |
|||
.filter(arg->arg.contains("=")) |
|||
.forEach(arg->{ |
|||
String[] parts = arg.split("="); |
|||
cycle_functions.add(j-> j%(Integer.parseInt(parts[0]))==0?parts[1]:""); |
|||
}); |
|||
IntStream.rangeClosed(0,Integer.parseInt(args[args.length-1])) |
|||
.mapToObj(Integer::valueOf) |
|||
.map(fizzBuzz::apply) |
|||
.forEach(System.out::println); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |