forked from Sergio0694/FizzBuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
46 lines (37 loc) · 1.36 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* FizzBuzz Implementation in C# with Compiled Expression Trees
* The Sharp Ninja - October 20, 2019
*
* "Write a program that prints the numbers from 1 to 100. But for multiples of three print
* “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which
* are multiples of both three and five print “FizzBuzz”."
*/
#nullable enable
using System.Linq.Expressions;
using System.Linq;
using System;
using static System.Console;
using static System.Linq.Enumerable;
namespace CSharpExpressions
{
class Program
{
static Expression<Func<int, string?>> ByThree =
(i) => i % 3 == 0 ? "Fizz" : null;
static Expression<Func<int, string?>> ByFive =
(i) => i % 5 == 0 ? "Buzz" : null;
static Expression<Func<int, string?>> ByThreeAndFive =
(i) => i % 5 == 0 && i % 3 == 0 ? "FizzBuzz" : null;
static Expression<Func<int, string>> Other = (i) => $"{i}";
static void Main(string[] args)
{
var by3and5 = ByThreeAndFive.Compile();
var by5 = ByFive.Compile();
var by3 = ByThree.Compile();
var other = Other.Compile();
Range(1, 100).ToList().ForEach(i =>
WriteLine(by3and5(i) ?? by5(i) ?? by3(i) ?? other(i))
);
}
}
}