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
map_fun = fn i -> i + 1 end | |
inputs = [ | |
{"Small (10 Thousand)", Enum.to_list(1..10_000)}, | |
{"Middle (100 Thousand)", Enum.to_list(1..100_000)}, | |
{"Big (1 Million)", Enum.to_list(1..1_000_000)}, | |
{"Giant (10 Million)", Enum.to_list(1..10_000_000)}, | |
{"Titanic (50 Million)", Enum.to_list(1..50_000_000)} | |
] |
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
random_list = fn size, spread -> | |
for _i <- 1..size, do: :rand.uniform(spread) | |
end | |
inputs = [ | |
{"10k", random_list.(10_000, 100)}, | |
{"1M", random_list.(1_000_000, 1_000)}, | |
{"10M", random_list.(10_000_000, 10_000)} | |
] |
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
context "with Zazz into the equation" do | |
zazz_rules = [ | |
FizzBuzz::Rule.new("Fizz", 3), | |
FizzBuzz::Rule.new("Buzz", 5), | |
FizzBuzz::Rule.new("Zazz", 7) | |
] | |
expected = { | |
1 => 1, | |
3 => "Fizz", |
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
module FizzBuzz | |
module_function | |
class Rule | |
def initialize(output, applicalbe_divisible_by) | |
@output = output | |
@applicalbe_divisible_by = applicalbe_divisible_by | |
end | |
def apply(number) |
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
module FizzBuzz | |
module_function | |
FIZZ_NUMBER = 3 | |
FIZZ_TEXT = "Fizz" | |
BUZZ_NUMBER = 5 | |
BUZZ_TEXT = "Buzz" | |
FIZZ_BUZZ_TEXT = FIZZ_TEXT + BUZZ_TEXT |
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
module FizzBuzz | |
module_function | |
def fizz_buzz(number) | |
fizz = divisible_by?(number, 3) | |
buzz = divisible_by?(number, 5) | |
if fizz && buzz | |
"FizzBuzz" | |
elsif fizz |
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
module FizzBuzz | |
# ... | |
def run | |
1.upto(100) do |number| | |
puts fizz_buzz(number) | |
end | |
end | |
end |
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
module FizzBuzz | |
module_function | |
def fizz_buzz(number) | |
if (number % 3 == 0) && (number % 5 == 0) | |
"FizzBuzz" | |
elsif number % 3 == 0 | |
"Fizz" | |
elsif number % 5 == 0 | |
"Buzz" |
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
1.upto(100) do |number| | |
if (number % 3 == 0) && (number % 5 == 0) | |
puts "FizzBuzz" | |
elsif number % 3 == 0 | |
puts "Fizz" | |
elsif number % 5 == 0 | |
puts "Buzz" | |
else | |
puts number | |
end |
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
list = Enum.to_list(1..10_000) | |
map_fun = fn i -> [i, i * i] end | |
Benchee.run( | |
%{ | |
"flat_map" => fn -> Enum.flat_map(list, map_fun) end, | |
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten() end | |
}, | |
profile_after: true | |
) |
NewerOlder