public interface Matcher {
Optional match(int value);
static Matcher divideBy(int div, String text) {
return value -> Optional.of(value).filter(v -> v % div == 0).map(__ -> text);
}
default Matcher zip(Matcher matcher, BinaryOperator op) {
return value -> Stream.of(match(value), matcher.match(value)).flatMap(Optional::stream).reduce(op);
}
public static void main(String[] args) {
var matcher = divideBy(3, "Fizz").zip(divideBy(5, "Buzz"), String::concat);
IntStream.rangeClosed(0, 100)
.mapToObj(value -> matcher.match(value).orElse("" + value))
.skip(1).forEach(System.out::println);
}
}FizzBuzz in ten languages
21–30 of 103 posts
Re: FizzBuzz in ten languages
#22I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…
Something like this, with the caveat that I haven't done any proper coding in Haskell in years and I don't have an interpreter installed to verify correctness.
fb :: (Integer, Integer) -> String
fb (mod3, mod5)
| (0, 0) = "FizzBuzz"
| (0, _) = "Fizz"
| (_, 0) = "Buzz"
| otherwise = show n
main = putStrLn $ unlines $ map fb $ map (\x -> (x % 3, x % 5)) [1..100]Re: FizzBuzz in ten languages
#23Re: FizzBuzz in ten languages
#24Re: FizzBuzz in ten languages
#25I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…
Re: FizzBuzz in ten languages
#26Took this as an excuse to write it in pure lambda calculus https://gist.github.com/Tarmean/65da65b8da37bd66d48e96d2aa73... I always enjoy how lambda calculus suddenly becomes a readable language after the prelude: let (\n. let (\m. isZero (mod n m)) \divisibleBy. if (and (divisibleBy 3) (divisibleBy 5)) then FizzBuzz else (if (divisibleBy 3) then Fizz else (if (divisibleBy 5) then Buzz else (intToStr n)))) \fizzBuzzS…
The whole point of fizzbuzz is to filter for people that can just solve a tiny problem in a reasonably short time-- that's it. Most interviewers will even give some slack if the person has to look up the modulo operator or even do with out it. If someone can't do it at all there's some kind of serious issue with the way they're approaching the problem, or perhaps they're just not capable of doing it. But another comm…
Re: FizzBuzz in ten languages
#27I've been looking for an example that shows how pattern matching can make code much both compact and readable. I think this does nicely. Here's an example in Scala: (1 to 100).map(i => (i % 3, i % 5) match { case (0, 0) => "FizzBuzz" case (0, _) => "Fizz" case (_, 0) => "Buzz" case _ => s"$i" }).foreach(println) Compare that to the rest of the examples on the page. The only one that comes close in either readability…
The Haskell example is quite similar to the Scala/Rust example as well. I guess you could rewrite the Haskell version to match your Scala version pretty closely as well if you prefer doing the tuple construction and then matching on the tuple, like in your Scala version, instead of doing it directly inside the pattern match. Something like this, with the caveat that I haven't done any proper coding in Haskell in year…
Re: FizzBuzz in ten languages
#28 for i in {1..100}; do
if (( $i % 3 && $i % 5 )); then
echo FizzBuzz
elif (( $i % 3 )); then
echo Fizz
elif (( $i % 5 )); then
echo Buzz
else
echo $i
fi
done
Bash also has a decently powerful matching statement so you can do something similar to the rust example. for i in {1..100}; do
case "$(( $i % 3 ))$(( $i % 5 ))" in
00)
echo FizzBuzz
;;
0*)
echo Fizz
;;
*0)
echo Buzz
;;
*)
echo $i
;;
esac
doneRe: FizzBuzz in ten languages
#29Took this as an excuse to write it in pure lambda calculus https://gist.github.com/Tarmean/65da65b8da37bd66d48e96d2aa73... I always enjoy how lambda calculus suddenly becomes a readable language after the prelude: let (\n. let (\m. isZero (mod n m)) \divisibleBy. if (and (divisibleBy 3) (divisibleBy 5)) then FizzBuzz else (if (divisibleBy 3) then Fizz else (if (divisibleBy 5) then Buzz else (intToStr n)))) \fizzBuzzS…
Re: FizzBuzz in ten languages
#30The next question I like to ask in an interview, once they have fizzbuzz working is, "Now make it more efficient."