Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop.
  3.  * User: zeonni
  4.  * Date: 29.09.2016
  5.  * Time: 16:07
  6.  *
  7.  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8.  */
  9. using System;
  10.  
  11. namespace Maybe
  12. {
  13.     class Program
  14.     {
  15.         public static void Main(string[] args)
  16.         {
  17.             welcome(new Maybe<String>("Onni"));
  18.             welcome(new Maybe<String>(null));
  19.            
  20.             Console.ReadKey();
  21.         }
  22.        
  23.         public static void welcome(Maybe<String> user) {
  24.             Console.WriteLine(
  25.                 user.Take<String>(
  26.                     (name) => String.Format("Welcome, {0}!", name),
  27.                     () => "User not auth"
  28.                 )
  29.             );
  30.         }
  31.     }
  32.    
  33.     class Maybe<T> {
  34.        
  35.         public delegate R Just<R>(T value);
  36.         public delegate R Nothing<R>();
  37.        
  38.         private T value;
  39.        
  40.         public Maybe(T value) {
  41.             this.value = value;
  42.         }
  43.        
  44.         public R Take<R>(Just<R> just, Nothing<R> nothing) {
  45.             return value == null ? nothing() : just(value);
  46.         }
  47.                
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement