Re:Overthinking Fizzbuzz with Monoids
by kbenson on Wednesday September 13, 2017 @05:47PM
(#15243029)
There's some interesting Perl 6 implementations here[1] (and in the discussion of that), which shows some interesting capabilities of multiple dispatch, and farther down using subtypes along with multiple dispatch.
I particularly like this terse one:
sub fizzbuzz($n, $_ = [$n % 3, $n % 5]) {
when [0, 0] { "fizzbuzz" }
when [0, *] { "fizz" }
when [*, 0] { "buzz" }
default { $n }
}
That's fairly tight, while remaining somewhat readable. There's a version that adds a line or two by moving $_ into the body of the sub as well in case that's more readable to some.
I particularly like this terse one:
That's fairly tight, while remaining somewhat readable. There's a version that adds a line or two by moving $_ into the body of the sub as well in case that's more readable to some.1: https://gist.github.com/masak/b9371625ad85cfe0faba