Skip to content

Instantly share code, notes, and snippets.

@JoseGonzalez321
Created March 22, 2018 17:09
Show Gist options
  • Save JoseGonzalez321/5a19d76408a8f781cffd68aff90a6d18 to your computer and use it in GitHub Desktop.
Save JoseGonzalez321/5a19d76408a8f781cffd68aff90a6d18 to your computer and use it in GitHub Desktop.
Jose's failure to understand Maybe :)
using System;
using System.Collections.Generic;
using System.Linq;
namespace Playground.Utils
{
public struct Maybe<T>
{
readonly IEnumerable<T> _values;
public static Maybe<T> Some(T value)
{
if (value == null)
{
throw new InvalidOperationException();
}
return new Maybe<T>(new[] { value });
}
public static Maybe<T> None => new Maybe<T>(new T[0]);
private Maybe(IEnumerable<T> values)
{
this._values = values;
}
public bool HasValue => _values != null && _values.Any();
public T Value
{
get
{
if (!HasValue)
{
throw new InvalidOperationException("Maybe does not have a value");
}
return _values.Single();
}
}
public U Case<T, U>(Func<T, U> some, Func<U> none)
{
return this.HasValue
? some(Value)
: none();
}
}
}
@reidev275
Copy link

Case should be

public U Case<U>(Func<T, U> some, Func<U> none)
{
    return this.HasValue 
        ? some(Value)
        : none();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment