-
-
Save lanwin/852890 to your computer and use it in GitHub Desktop.
public static class ObjectExtensions | |
{ | |
public static IEnumerable<T> ToMaybe<T>(this T obj) | |
{ | |
return Equals(obj,null) ? Enumerable.Empty<T>() : new[] {obj}; | |
} | |
} |
Hmm as i said it is no 100% like foreach. On first it made no sens that there is an Run() without parameters. But that only run thru the enumerable without accessing the result.
They also provide a Do. But that allows you to add a side effect while enumerating the enumerable. But its dose not start the enumeration. Example:
GetList()
.Where(item=="foo")
.Do(item=>Console.WriteLine(item))
.Run();
lanwin is right. Since IEnumerable is lazy you need something which starts the process. Foreach() will do so but breaks the monad. Do() is something which stays in the monad and therefor keeps laziness.
A slight, but noteworthy difference. I don't know RX, so bare with me. I assumed that RX's Run() is the same as you have used it in your comment - which for me is a simple application like a commonly used ForEach-Extension does.
I wasn't aware of that "do" adds side effects without application. This fact actually is a clear indicator that i yet have a long learning path before me.
@lanwin nice. one more reason have a look at RX. The Run() seems to me as if it resembles to the "do" of Haskell. Despite, it would finally render the gazillions of custom ForEach() extensions useless ;)