Created
December 26, 2019 15:14
-
-
Save ykon/f4f6a2cb5adb37b57f141ca534d746b8 to your computer and use it in GitHub Desktop.
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
# typed: ignore | |
# Ruby 2.7 (Pattern Matching) | |
require 'sorbet-runtime' | |
class FizzBuzz | |
extend T::Sig | |
sig {params(n: Integer).void} | |
def self.check_number(n) | |
if n <= 0 then raise ArgumentError end | |
end | |
sig {params(n: Integer).returns(T::Boolean)} | |
def self.is_fizz(n) | |
check_number(n) | |
n % 3 == 0 | |
end | |
sig {params(n: Integer).returns(T::Boolean)} | |
def self.is_buzz(n) | |
check_number(n) | |
n % 5 == 0 | |
end | |
sig {params(n: Integer).returns(T::Array[T.any(Symbol, Integer)])} | |
def self.number_as(n) | |
case [is_fizz(n), is_buzz(n)] | |
in [true, true] | |
[:FizzBuzz, n] | |
in [true, false] | |
[:Fizz, n] | |
in [false, true] | |
[:Buzz, n] | |
else | |
[:Number, n] | |
end | |
end | |
sig {params(fz: T::Array[T.any(Symbol, Integer)]).returns(String)} | |
def self.to_str(fz) | |
case fz | |
in [:Number, n] | |
n.to_s | |
in [s, _] | |
s.to_s | |
else | |
raise ArgumentError | |
end | |
end | |
sig {void} | |
def self.main() | |
puts (1..100) | |
.map(&(method(:number_as) >> method(:to_str))) | |
.zip((1..100).to_a) | |
.map{|fzs, n| "#{n} => #{fzs}"} | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
FizzBuzz.main() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment