Created
January 6, 2016 11:24
-
-
Save miklund/5770d9cef25aba81552b to your computer and use it in GitHub Desktop.
2011-04-08 Option type implementation in C#
This file contains hidden or 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
# Title: Option type implementation in C# | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2011/04/08/option-type-implementation-in-csharp.html |
This file contains hidden or 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
public Option<User> FindUserByName(string name) | |
{ | |
var query = from user in Users | |
where user.FirstName.Contains(name) || user.Surname.Contains(name) | |
select user; | |
var found = query.FirstOrDefault(); | |
if (found == null) | |
return new None<User>(); | |
return new Some<User>(found); | |
} |
This file contains hidden or 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
public Option<int> GetIndexOfSubstring(string s, string substring) | |
{ | |
var index = s.IndexOf(substring); | |
if (index == -1) | |
return new None<int>(); | |
return new Some<int>(index); | |
} |
This file contains hidden or 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
let getIndexOfSubstring (s : string) (substring : string) = | |
let index = s.IndexOf(substring) | |
match index with | |
| -1 -> None | |
| n -> Some n |
This file contains hidden or 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
// Used as return type from method | |
public abstract class Option<T> | |
{ | |
// Could contain the value if Some, but not if None | |
public abstract T Value { get; } | |
public abstract bool IsSome { get; } | |
public abstract bool IsNone { get; } | |
} |
This file contains hidden or 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
public static class OptionExtensions | |
{ | |
public static Option<T> SomeOrNone<T>(this T reference) | |
where T : class | |
{ | |
if (reference == null) return new None<T>(); | |
return new Some<T>(reference); | |
} | |
} |
This file contains hidden or 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
public sealed class Some : Option | |
{ | |
private T value; | |
public Some(T value) | |
{ | |
// Setting Some to null, nullifies the purpose of Some/None | |
if (value == null) | |
{ | |
throw new System.ArgumentNullException("value", "Some value was null, use None instead"); | |
} | |
this.value = value; | |
} | |
public override T Value { get { return value; } } | |
public override bool IsSome { get { return true; } } | |
public override bool IsNone { get { return false; } } | |
} | |
public sealed class None : Option | |
{ | |
public override T Value | |
{ | |
get { throw new System.NotSupportedException("There is no value"); } | |
} | |
public override bool IsSome { get { return false; } } | |
public override bool IsNone { get { return true; } } | |
} |
This file contains hidden or 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
// Get string before substring | |
private string SubstringBefore(string s, string substring) | |
{ | |
var operations = new StringOperations(); | |
var index = operations.GetIndexOfSubstring(s, substring); | |
if (index.IsSome) | |
return s.Substring(0, index.Value); | |
return s; | |
} |
This file contains hidden or 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
[Test] | |
public void ShouldReturnSomeIndexForExistingSubstring() | |
{ | |
/* Test implementation */ | |
} | |
[Test] | |
public void ShouldReturnNoneWhenSubstringDoesNotExist() | |
{ | |
/* Test implementation */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment