Last active
November 21, 2020 01:40
-
-
Save michaeloyer/9f87316eecafe5884b3702009e405753 to your computer and use it in GitHub Desktop.
Hello World Program using Result/Pattern Matching in Typescript, F# and C#
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
interface IResult { } | |
class Accepted : IResult | |
{ | |
public string text { get; set; } | |
} | |
class Rejected : IResult { } | |
string GetResultText(IResult result) | |
{ | |
return result switch | |
{ | |
Accepted r => r.text, | |
_ => "Reject" | |
}; | |
} | |
void PrintResult(IResult result) => | |
System.Console.WriteLine($"Hello, {GetResultText(result)}!"); | |
PrintResult(new Accepted {text = "World"}); | |
PrintResult(new Rejected()); |
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
type Result = | |
| Accepted of {| text: string |} | |
| Rejected | |
let GetResultText = function | |
| Accepted result -> result.text | |
| Rejected -> "Reject" | |
let PrintResult = GetResultText >> printfn "Hello, %s!" | |
Accepted {| text = "World" |} |> PrintResult | |
Rejected |> PrintResult |
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
type Result = Accepted | Rejected | |
interface Accepted { | |
status: "Accepted" | |
text: string | |
} | |
interface Rejected { | |
status: "Rejected" | |
} | |
function GetResultText(result: Result) : string { | |
switch (result.status) { | |
case "Accepted": return result.text | |
case "Rejected": return "Reject" | |
} | |
} | |
function PrintResult(result: Result) { | |
console.log('Hello, ' + GetResultText(result) + '!') | |
} | |
PrintResult({ status: "Accepted", text: "World" }) | |
PrintResult({ status: "Rejected" }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment