devlights
10/15/2013 - 8:48 AM

モナドについて

モナドについて

以下のサイトが非常に分かりやすい。

http://d.hatena.ne.jp/qtamaki/20130624/1372086242

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Monads;

namespace MonadsSample
{
  class Program
  {
    static void Main(string[] args)
    {
      new Data() { Name = "hello", Age = 40 }.With(_ => _.Name)
                                             .With(_ => _.ToUpper())
                                             .Do(_ => Console.WriteLine(_))
                                             .With(_ => _.ToLower())
                                             .Do(_ => Console.WriteLine(_))
                                             .With(_ => Method1(_))
                                             .With(_ => _.ToUpper())
                                             .Do(_ => Console.WriteLine(_));
    }

    static string Method1(string value)
    {
      return value.Reverse().Return(_ => new string(_.ToArray()), string.Empty);
    }
  }

  public class Data
  {
    public string Name { get; set; }
    public int Age { get; set; }
  }
}