mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2024-11-12 21:09:01 +00:00
rewrite example/fizzbuzz
This commit is contained in:
parent
2905965f24
commit
bf0f36d5a9
1 changed files with 22 additions and 35 deletions
|
@ -12,30 +12,20 @@
|
|||
// Boost Software License - Version 1.0
|
||||
// <http://www.boost.org/LICENSE_1_0.txt>
|
||||
//
|
||||
#include <sprout/algorithm/transform.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/string.hpp>
|
||||
#include <sprout/numeric/iota.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
struct fizzbuzz{
|
||||
typedef sprout::string<12> result_type;
|
||||
|
||||
constexpr result_type
|
||||
operator()(int n) const {
|
||||
return n % 15 == 0 ? sprout::to_string("FizzBuzz")
|
||||
: n % 3 == 0 ? sprout::to_string("Fizz")
|
||||
: n % 5 == 0 ? sprout::to_string("Buzz")
|
||||
return n % 15 == 0 ? "FizzBuzz"
|
||||
: n % 3 == 0 ? "Fizz"
|
||||
: n % 5 == 0 ? "Buzz"
|
||||
: sprout::to_string(n);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
main(){
|
||||
typedef fizzbuzz::result_type string;
|
||||
|
||||
//
|
||||
// Test
|
||||
//
|
||||
|
@ -45,6 +35,17 @@ main(){
|
|||
static_assert(fizzbuzz()( 5) == "Buzz", "");
|
||||
static_assert(fizzbuzz()(15) == "FizzBuzz", "");
|
||||
|
||||
|
||||
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/algorithm/transform.hpp>
|
||||
#include <sprout/numeric/iota.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int
|
||||
main(){
|
||||
typedef fizzbuzz::result_type string;
|
||||
|
||||
//
|
||||
// Sequence [1..15]
|
||||
//
|
||||
|
@ -63,21 +64,9 @@ main(){
|
|||
// Check result
|
||||
//
|
||||
constexpr auto fizzbuzz_result = sprout::make_array<string>(
|
||||
sprout::to_string("1"),
|
||||
sprout::to_string("2"),
|
||||
sprout::to_string("Fizz"),
|
||||
sprout::to_string("4"),
|
||||
sprout::to_string("Buzz"),
|
||||
sprout::to_string("Fizz"),
|
||||
sprout::to_string("7"),
|
||||
sprout::to_string("8"),
|
||||
sprout::to_string("Fizz"),
|
||||
sprout::to_string("Buzz"),
|
||||
sprout::to_string("11"),
|
||||
sprout::to_string("Fizz"),
|
||||
sprout::to_string("13"),
|
||||
sprout::to_string("14"),
|
||||
sprout::to_string("FizzBuzz")
|
||||
"1", "2", "Fizz", "4", "Buzz",
|
||||
"Fizz", "7", "8", "Fizz", "Buzz",
|
||||
"11", "Fizz", "13", "14", "FizzBuzz"
|
||||
);
|
||||
// Equal result sequence
|
||||
static_assert(result == fizzbuzz_result, "");
|
||||
|
@ -89,6 +78,4 @@ main(){
|
|||
std::cout << str << ", ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue