Exemple #1
0
 public static Option <TSource> Where <TSource>(this Option <TSource> source, Func <TSource, bool> predicate)
 {
     if (predicate == null)
     {
         throw new ArgumentNullException("predicate");
     }
     return(SelectMany(source, x => predicate(x) ? Option.GetSome(x) : Option.GetNone <TSource>(), (x, y) => y));
 }
Exemple #2
0
 public static Option <object> AsOption(this IOption value)
 {
     if (value == null || value.IsNone)
     {
         return(Option.None);
     }
     return(Option.GetSome(value.Get()));
 }
Exemple #3
0
 public static Option <T> AsOptionOfType <T>(this object value)
 {
     if (value is T) // not null if true
     {
         return(Option.GetSome((T)value));
     }
     return(Option.None);
 }
Exemple #4
0
        public static Option <T> AsOption <T>(this T value)
        {
            if (ReferenceEquals(null, value))
            {
                return(Option.GetNone <T>());
            }

            return(Option.GetSome(value));
        }
Exemple #5
0
        private static Option <T[]> TakeExactly <T>(IEnumerable <T> source, int count, Option.ErrorHelper returnNoneOrThrow)
        {
            var xs = source.Take(count + 1).ToArray();

            if (xs.Length != count)
            {
                return(returnNoneOrThrow("Expected " + count + " items, but got " + (xs.Length > count ? " more." : xs.Length + " items.")));
            }
            return(Option.GetSome(xs));
        }
 public static Option <T> GetCachedValueOption <T>(this Lazy <T> source)
 {
     if (source.IsValueCreated)
     {
         return(Option.GetSome(source.Value));
     }
     else
     {
         return(Option.None);
     }
 }
Exemple #7
0
 public static Option <T> AsOption <T>(this IOption <T> value)
 {
     if (value is Option <T> )
     {
         return((Option <T>)value);
     }
     if (value == null || value.IsNone)
     {
         return(Option <T> .None);
     }
     return(Option.GetSome(value.Get()));
 }
Exemple #8
0
 public Option <TResult> Transform <TResult>(Func <T, TResult> func)
 {
     if (func == null)
     {
         throw new ArgumentNullException("func");
     }
     if (_isSome)
     {
         return(Option.GetSome(func(_value)));
     }
     else
     {
         return(Option.GetNone <TResult>());
     }
 }
Exemple #9
0
        public static Option <T[]> Sequence <T>(this IEnumerable <Option <T> > source)
        {
            var buf = new List <T>();

            foreach (var x in source)
            {
                if (x.IsNone)
                {
                    return(Option.None);
                }
                else
                {
                    buf.Add(x.Get());
                }
            }
            return(Option.GetSome(buf.ToArray()));
        }
 //[System.Diagnostics.Conditional("DEBUG")]
 public static void TraceDiff(string msg = null, object id = null)
 {
     id = id ?? "";
     try
     {
         var watch = watches.OptionGetValue(id);
         if (watch.IsSome)
         {
             watch.Get().Stop();
         }
     }
     catch
     {
         watches.Remove(id);
     }
     Trace(msg, 2, traceDiffId: Option.GetSome(id));
 }
        /// <summary>
        /// Gets the relative path by removing a prefix, i.e. this function won't add dots like in "../../some/path".
        /// E.g. if "/tmp" is the prefix then "/tmp/some/path" becomes "some/path", and "/tmp" becomes just ".".
        /// </summary>
        /// <param name="path"></param>
        /// <param name="referencePrefixPath"></param>
        /// <returns></returns>
        public static Option <string> GetPrefixRelativePathOption(string path, string referencePrefixPath)
        {
            if (string.IsNullOrEmpty(referencePrefixPath))
            {
                return(Option.None);
            }
            char[] seps = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
            string normReferencePath = referencePrefixPath.TrimEnd(seps).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToLowerInvariant();
            string normPath          = path.TrimEnd(seps).Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar).ToLowerInvariant();

            if (normPath == normReferencePath)
            {
                return(Option.GetSome("."));
            }
            if (normPath.StartsWith(normReferencePath))
            {
                return(Option.GetSome(path.Substring(normReferencePath.Length + 1)));
            }
            return(Option.None);
        }
        public static Option <T> ParseEnumOption <T>(string s) where T : struct, IConvertible
        {
            Type type = typeof(T);

            if (!type.IsEnum)
            {
                return(Option.None);
            }
            T e;

            if (!Enum.TryParse(s, out e))
            {
                return(Option.None);
            }
            if (!Enum.IsDefined(type, e))
            {
                return(Option.None);
            }
            return(Option.GetSome(e));
        }
Exemple #13
0
 public static bool IsFalse(this Option <bool> value)
 {
     return(value == Option.GetSome(false));
 }
Exemple #14
0
 public static bool IsTrue(this Option <bool> value)
 {
     return(value == Option.GetSome(true));
 }
Exemple #15
0
 public ErrorMessageException(string message, string title, Exception exception)
     : base(message, exception)
 {
     Title = Option.GetSome(title);
 }
 public DisposeOnGCWrapper(T value)
 {
     _value = Option.GetSome(value);
 }