Created
September 15, 2015 05:48
-
-
Save paulownia/938d046a4535cb1ad343 to your computer and use it in GitHub Desktop.
fizzbuzz java8
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
/* | |
* https://gist.github.com/kazuho/3300555 | |
*/ | |
import java.util.Iterator; | |
public class Main { | |
public static void main(String[] args) { | |
FizzBuzz fb = new FizzBuzz(30); | |
for (String s: fb) { | |
System.out.println(s); | |
} | |
} | |
} | |
interface Fizz { | |
String get(); | |
} | |
interface Buzz { | |
String get(); | |
} | |
class FizzBuzz implements Iterable<String> { | |
Fizz fizz = new FizzStart(); | |
Buzz buzz = new BuzzStart(); | |
int count = 0; | |
final int upto; | |
public FizzBuzz(int upto) { | |
this.upto = upto; | |
} | |
class FizzStart implements Fizz { | |
public String get() { | |
fizz = () -> { | |
fizz = () -> { | |
fizz = new FizzStart(); | |
return "fizz"; | |
}; | |
return ""; | |
}; | |
return ""; | |
} | |
} | |
class BuzzStart implements Buzz { | |
public String get() { | |
buzz = () -> { | |
buzz = () -> { | |
buzz = () ->{ | |
buzz = () -> { | |
buzz = new BuzzStart(); | |
return "buzz"; | |
}; | |
return ""; | |
}; | |
return ""; | |
}; | |
return ""; | |
}; | |
return ""; | |
} | |
} | |
public Iterator<String> iterator() { | |
return new Iterator<String>() { | |
public boolean hasNext() { | |
return count++ < upto; | |
} | |
public String next() { | |
String f = fizz.get(); | |
String b = buzz.get(); | |
return (f.equals("") && b.equals("")) ? Integer.toString(count) : f + b; | |
} | |
public void remove() { | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment