Ejemplo n.º 1
0
 public void Coloring()
 {
     Verify.SequenceEquivalent(
         Enumerable.Range(0, 5),
         new[] { Color.Blue, Color.Red, Color.Blue, Color.Red, Color.Blue },
         Switching(
             TransducerSwitch.Create(x => x % 2 == 0, Mapping <int, Color>(_ => Color.Blue)),
             TransducerSwitch.Default(Mapping <int, Color>(_ => Color.Red))));
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            GuessingGame game;

            if (!TryGetGame(args, out game))
            {
                PrintUsage();
                return;
            }

            new[] {
                "Welcome to the Guessing Game!\n",
                $"We've picked a value between {game.Min} and {game.Max}. Your job is to guess it.\n",
                "We'll let you know if you're getting warmer or colder. Let's start!\n",
                "Guess: "
            }.Reduce(Console.Out, Relaxing <string, object>().Apply(TextIO.WriteReducer()));

            var gameInstance = Compose(
                Catching(
                    Compose(
                        Parsing <int>(),
                        game.Play(),
                        Mapping <GuessingGameResult, Result>(gameResult => new GuessResult(gameResult))),
                    Mapping <ExceptionalInput <string, FormatException>, Result>(exc =>
                                                                                 new ErrorResult($"Could not parse: {exc.Exception.Message}"))
                    ),
                Terminating <Result>(result => result.IsEnd),
                Switching(
                    TransducerSwitch.Create(
                        result => result.IsEnd,
                        Mapping <Result, string>(result => $"Congratulations! You did it!\nPress any key to continue...")),
                    TransducerSwitch.Default(
                        Mapping <Result, string>(result => result.Display())
                        .Compose(Formatting <string>("{0}\nGuess: ")))
                    ));

            Console.In.Transduce(Console.Out, gameInstance);

            Console.ReadKey();
        }