Skip to content

Instantly share code, notes, and snippets.

@tkrs
Last active June 6, 2017 12:20
Show Gist options
  • Save tkrs/eb5a227ba6356610e52d to your computer and use it in GitHub Desktop.
Save tkrs/eb5a227ba6356610e52d to your computer and use it in GitHub Desktop.
Challenge the FizzBuzz for some languages.
(defun fizzbuzz (i)
(if (< (mod i 15) 1) "FizzBuzz"
(if (< (mod i 3) 1) "Fizz"
(if (< (mod i 5) 1) "Buzz"
(write-to-string i)))))
(loop
for i from 1 to 100
do (print (fizzbuzz i)))
import std.stdio,std.conv;
void main() {
foreach(i;1..100)writeln(fizzbuzz(i));
}
string fizzbuzz(int i) {
string s=((i%3<1)?"Fizz":"")~((i%5<1)?"Buzz":"");
return s==""?to!string(i):s;
}
import Control.Monad
x = map show [1..]
y = cycle["","","Fizz"]
z = cycle["","","","","Buzz"]
f a [] [] = a
f _ a [] = a
f _ [] a = a
f _ a b = a ++ b
main :: IO ()
main = forM_ (take 100 $ zipWith3 f x y z) putStrLn
public class FizzBuzz{
public static void main(String...args){
java.util.stream.Stream.iterate(1, i -> i + 1)
.map(i -> {
String x=(i % 3 < 1 ? "Fizz" : "") + (i % 5 < 1 ? "Buzz" : "");
return x.equals("") ? i : x;
})
.limit(100)
.forEach(System.out::println);
}
}
for(var i=1;i<1e2;i++)console.log((i%3<1?'Fizz':'')+(i%5<1?'Buzz':'')||i)
<?php foreach(range(1,100)as$n){$x=($n%3<1?"Fizz":"").($n%5<1?"Buzz":"");print(($x?$x:$n)."\n");}
print die+map{(Fizz)[$_%3].(Buzz)[$_%5]||$_,$/}1..1e2
def take(n, l):
for i in range(n):
yield next(l)
def infinite(i = 1):
while 1:
yield i
i += 1
def forM(f, l):
for x in l:
f(x)
def prn(x):
a, b, c = x
print(a + b if a or b else c)
def fizzbuzz(c, w, i = 1):
while 1:
if c(i): yield w
else: yield ""
i += 1
fb = zip(
fizzbuzz(lambda i: i % 3 < 1, 'Fizz'),
fizzbuzz(lambda i: i % 5 < 1, 'Buzz'),
infinite()
)
forM(prn, take(100, fb))
(1..1e2).each{|x|a=(x%3<1?"Fizz":"")+(x%5<1?"Buzz":"");puts a==""?x:a}
struct FizzBuzz {
index: u32,
}
impl Iterator for FizzBuzz {
type Item = String;
fn next(&mut self) -> Option<String> {
let v = match (self.index%3, self.index%5) {
(0, 0) => "FizzBuzz".to_string(),
(0, _) => "Fizz".to_string(),
(_, 0) => "Buzz".to_string(),
_ => self.index.to_string()
};
self.index+=1;
Some(v)
}
}
fn fizzbuzz(i: u32) -> FizzBuzz {
FizzBuzz { index: i }
}
fn main() {
for msg in fizzbuzz(1).take(100) {
println!("{}", msg);
}
}
def f(i:Int):String={val x=(if(i%3<1)"Fizz"else"")+(if(i%5<1)"Buzz"else"");if(x=="")s"$i"else x}
(1 to 100).foreach(i=>println(f(i)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment