Created
February 2, 2014 15:24
-
-
Save podhmo/8769895 to your computer and use it in GitHub Desktop.
hmm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/* | |
* f0 :: a -> R<F,T0> | |
* f1 :: t0 -> a -> R<F,T1> | |
* F :: a -> R<F,T1> | |
* F = f0 * f1 みたいなことしたい. | |
*/ | |
namespace TT | |
{ | |
public class Result<TF,TS> | |
{ | |
public bool Status; | |
public TF Left { get; set; } | |
public TS Right { get; set; } | |
public Result (bool b) | |
{ | |
this.Status = b; | |
} | |
public static Result<TF,TS> Success (TS s) | |
{ | |
return new Result<TF,TS> (true){ Right = s }; | |
} | |
public static Result<TF,TS> Failure (TF f) | |
{ | |
return new Result<TF,TS> (false){ Left = f }; | |
} | |
public Result<TF,TS2> Merge<TS2> (Func<TS,Result<TF,TS2>>use) | |
{ | |
if (!this.Status) { | |
return Result<TF,TS2>.Failure (this.Left); | |
} | |
return use (this.Right); | |
} | |
public override string ToString () | |
{ | |
if (this.Status) { | |
return String.Format ("Success[{0}]", this.Right); | |
} else { | |
return String.Format ("Failure[{0}]", this.Left); | |
} | |
} | |
} | |
public class LoginStatus | |
{ | |
public string Token { get; set; } | |
} | |
public class UserInfo | |
{ | |
public string Name { get; set; } | |
} | |
public enum APIStatus | |
{ | |
ok, | |
ng, | |
timeout, | |
notfound | |
} | |
public class MessageUtil | |
{ | |
public static string MessageFromStatus (APIStatus s) | |
{ | |
switch (s) { | |
case APIStatus.ok: | |
throw new InvalidOperationException ("don't call"); | |
case APIStatus.ng: | |
return "arya- ng."; | |
case APIStatus.timeout: | |
return "oo timeout."; | |
case APIStatus.notfound: | |
return "oops not found."; | |
default: | |
throw new NotImplementedException (); | |
} | |
} | |
public static Result<string, T> MessageFromStatus<T> (APIStatus s, string prefix) | |
{ | |
return Result<string,T>.Failure (String.Format ("{0}: {1}", prefix, MessageUtil.MessageFromStatus (s))); | |
} | |
} | |
public class APIRepository | |
{ | |
public Result<String, LoginStatus>Login (string url, APIStatus s) | |
{ | |
if (s == APIStatus.ok) { | |
return Result<String,LoginStatus>.Success (new LoginStatus (){ Token = "dummy" }); | |
} else { | |
return MessageUtil.MessageFromStatus<LoginStatus> (s, "login"); | |
} | |
} | |
public Result<String, UserInfo>GetUserInfo (string url, APIStatus s, LoginStatus status) | |
{ | |
if (s == APIStatus.ok) { | |
return Result<String,UserInfo>.Success (new UserInfo (){ Name = "dummy name" }); | |
} else { | |
return MessageUtil.MessageFromStatus<UserInfo> (s, "status"); | |
} | |
} | |
} | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
Console.WriteLine ("Hello World!"); | |
var m = new MainClass (); | |
var repo = new APIRepository (); | |
Console.WriteLine ("ok,ok => {0}", m.GetThis0 (repo)); | |
Console.WriteLine ("ng,ok => {0}", m.GetThis1 (repo)); | |
Console.WriteLine ("ok,timeout => {0}", m.GetThis2 (repo)); | |
Console.WriteLine ("notfound,notfound => {0}", m.GetThis3 (repo)); | |
} | |
public Result<string, UserInfo>GetThis0 (APIRepository repo) | |
{ | |
return repo.Login ("*login*", APIStatus.ok).Merge<UserInfo> ( | |
(LoginStatus s) => repo.GetUserInfo ("*status*", APIStatus.ok, s) | |
); | |
} | |
public Result<string, UserInfo>GetThis1 (APIRepository repo) | |
{ | |
return repo.Login ("*login*", APIStatus.ng).Merge<UserInfo> ( | |
(LoginStatus s) => repo.GetUserInfo ("*status*", APIStatus.ok, s) | |
); | |
} | |
public Result<string, UserInfo>GetThis2 (APIRepository repo) | |
{ | |
return repo.Login ("*login*", APIStatus.ok).Merge<UserInfo> ( | |
(LoginStatus s) => repo.GetUserInfo ("*status*", APIStatus.timeout, s) | |
); | |
} | |
public Result<string, UserInfo>GetThis3 (APIRepository repo) | |
{ | |
return repo.Login ("*login*", APIStatus.notfound).Merge<UserInfo> ( | |
(LoginStatus s) => repo.GetUserInfo ("*status*", APIStatus.notfound, s) | |
); | |
} | |
} | |
} |
Author
podhmo
commented
Feb 2, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment