fx-sharp  0.1
A collection of functional extensions for C#.
 All Classes Namespaces Files Functions Variables Enumerator
StackExtensions.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 using FxSharp.Utils;
4 using JetBrains.Annotations;
5 
6 namespace FxSharp.Extensions
7 {
8  public static class StackExtensions
9  {
10  /// <summary>
11  /// Remove and return the object at the top of the stack.
12  /// </summary>
13  /// <typeparam name="TSource">The type of the stack.</typeparam>
14  /// <param name="source">The stack.</param>
15  /// <returns>Maybe.Nothing(T) if stack is empty.</returns>
16  public static Maybe<TSource> PopOrNothing<TSource>([NotNull] this Stack<TSource> source)
17  {
18  Ensure.NotNull(source, "source");
19 
20  return source.Any() ? Maybe.Just(source.Pop()) : Maybe.Nothing<TSource>();
21  }
22  }
23 }