有没有“也许mzero回归”的名字?

时间:2016-05-17 01:52:55

标签: haskell

public class UpdateContact : IReturn<UpdateContactResponse> { public Guid Id { get; set; } public string Reference { get; set; } public string Notes { get; set; } public List<Accounts> Accounts { get; set; } } public class UpdateContactResponse : ResponseBase { public Guid ContactId { get; set; } } public class UpdateContactValidator : AbstractValidator<UpdateContact> { public UpdateContactValidator(IValidator<AccountDetail> accountDetailValidator) { RuleSet(ApplyTo.Post | ApplyTo.Put, () => { var session = base.Request.GetSession() as CustomAuthSession; RuleFor(c => c.Reference).Must(x => !string.IsNullOrEmpty(x) && session.Region.GetCountry() == RegionCodes.AU); }); RuleFor(R => R.Accounts).SetCollectionValidator(accountDetailValidator); } } 是否已建立名称?

它的类型为:

maybe mzero return

并将MonadPlus m => Maybe a -> m a 转换为失败,将Nothing转换为Just a

1 个答案:

答案 0 :(得分:8)

以上所有都是一样的。

一个明显的变体是

maybeAlt :: Alternative f => Maybe a -> f a
maybeAlt = maybe empty pure

这是以下特例,类似于asum

import Data.Monoid
import Control.Applicative

foldAlt :: (Foldable f, Alternative m) => f a -> m a
foldAlt = getAlt . foldMap (Alt . pure)

你在任何地方都找不到的原因是pure a <|> x === pure a。所以它对此有好处,而不是其他。它可以改进到

foldAltMap f = getAlt . foldMap (Alt . f)

foldrAltMap f = foldr (\x r -> f x <|> r) empty

但只是把它写出来可能更清楚。