Last active
September 15, 2015 03:50
-
-
Save paulownia/c61c72328a3df454c65c to your computer and use it in GitHub Desktop.
fizzbuzz
This file contains hidden or 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 = new Fizz() { | |
public String get() { | |
fizz = new Fizz() { | |
public String get() { | |
fizz = new FizzStart(); | |
return "fizz"; | |
} | |
}; | |
return ""; | |
} | |
}; | |
return ""; | |
} | |
} | |
class BuzzStart implements Buzz { | |
public String get() { | |
buzz = new Buzz() { | |
public String get() { | |
buzz = new Buzz() { | |
public String get() { | |
buzz = new Buzz() { | |
public String get() { | |
buzz = new Buzz() { | |
public String get() { | |
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