“一些”和‘许多’来自“替代”的功能类型类别
Alternative
类型类中的函数 some
和 many
有什么用处? 文档提供我无法理解的递归定义。
What are the functions some
and many
in the Alternative
type class useful for? Docs provide a recursive definition which I was unable to comprehend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
some
和many
可以定义为:也许有助于了解如何使用单子
do
语法编写some
:所以
some f
运行f
一次,然后“很多”次,并整理结果。many f
运行f
“一些”次,或者“或者”只返回空列表。这个想法是,它们都尽可能频繁地运行f
直到它“失败”,并将结果收集在列表中。区别在于,如果f
失败,some f
会立即失败,而在这种情况下,many f
仍然会成功并“返回”空列表。但这一切的确切含义取决于<|>
的定义方式。它只对解析有用吗?让我们看看它对基类中的实例做了什么:
Maybe
、[]
和STM
。首先
也许
。Nothing
表示失败,因此some Nothing
也会失败并计算为Nothing
,而many Nothing
成功并计算为 <代码>只是[]。some (Just ())
和many (Just ())
都不会返回,因为Just ()
永远不会失败!从某种意义上说,它们的计算结果为Just (repeat ())
。对于列表,
[]
表示失败,因此some []
计算结果为[]
(无答案),而many []
code> 的计算结果为[[]]
(只有一个答案,它是空列表)。同样,一些 [()]
和许多 [()]
不会返回。展开实例,some [()]
表示fmap (():) (many [()])
和many [()]
表示some [()] ++ [[]]
,因此您可以说many [()]
与tails (repeat ()) 相同
。对于
STM
,失败意味着必须重试事务。因此,some retry
将重试自身,而many retry
将仅返回空列表。some f
和many f
将重复运行f
直到重试。我不确定这是否有用,但我猜不是。因此,对于
Maybe
来说,[]
和STM
many
和some
不会似乎很有用。仅当应用程序具有某种状态,使得在一遍又一遍地运行相同的事情时越来越可能出现失败时,它才有用。对于解析器来说,这是随着每次成功匹配而缩小的输入。some
andmany
can be defined as:Perhaps it helps to see how
some
would be written with monadicdo
syntax:So
some f
runsf
once, then "many" times, and conses the results.many f
runsf
"some" times, or "alternatively" just returns the empty list. The idea is that they both runf
as often as possible until it "fails", collecting the results in a list. The difference is thatsome f
immediately fails iff
fails, whilemany f
will still succeed and "return" the empty list in such a case. But what all this exactly means depends on how<|>
is defined.Is it only useful for parsing? Let's see what it does for the instances in base:
Maybe
,[]
andSTM
.First
Maybe
.Nothing
means failure, sosome Nothing
fails as well and evaluates toNothing
whilemany Nothing
succeeds and evaluates toJust []
. Bothsome (Just ())
andmany (Just ())
never return, becauseJust ()
never fails! In a sense they evaluate toJust (repeat ())
.For lists,
[]
means failure, sosome []
evaluates to[]
(no answers) whilemany []
evaluates to[[]]
(there's one answer and it is the empty list). Againsome [()]
andmany [()]
don't return. Expanding the instances,some [()]
meansfmap (():) (many [()])
andmany [()]
meanssome [()] ++ [[]]
, so you could say thatmany [()]
is the same astails (repeat ())
.For
STM
, failure means that the transaction has to be retried. Sosome retry
will retry itself, whilemany retry
will simply return the empty list.some f
andmany f
will runf
repeatedly until it retries. I'm not sure if this is useful thing, but I'm guessing it isn't.So, for
Maybe
,[]
andSTM
many
andsome
don't seem to be that useful. It is only useful if the applicative has some kind of state that makes failure increasingly likely when running the same thing over and over. For parsers this is the input which is shrinking with every successful match.例如,解析(请参阅“示例应用解析”部分)。
E.g. for parsing (see the "Applicative parsing by example" section).