/// <summary>replicateM</summary> public static Mayhap <IEnumerable <T> > Replicate <T>(this Mayhap <T> @this, int count) { // replicateM :: Applicative m => Int -> m a -> m [a] // replicateM cnt0 f = // loop cnt0 // where // loop cnt // | cnt <= 0 = pure [] // | otherwise = liftA2 (:) f (loop (cnt - 1)) // // replicateM n act performs the action n times, gathering the results. return(@this.Select(x => Enumerable.Repeat(x, count))); }
/// <summary>mfilter</summary> public static Mayhap <T> Where <T>(this Mayhap <T> @this, Func <T, bool> predicate) { // mfilter :: (MonadPlus m) => (a -> Bool) -> m a -> m a // mfilter p ma = do // a <- ma // if p a then return a else mzero // // Direct MonadPlus equivalent of filter (for lists). Utilities.Guard.NotNull(predicate, nameof(predicate)); // NB: x is never null. return(@this.Bind(x => predicate(x) ? Mayhap <T> .Some(x) : Mayhap <T> .None)); }