Scala 组合器解析器,>> 的作用是什么意思是?
我对“>>”有点困惑在斯卡拉。 Daniel 在 Scala 解析器组合器解析 xml? 中说它可以用于参数化解析器基于先前解析器的结果。有人能给我一些例子/提示吗?我已经读过 scaladoc 但仍然不明白。
谢谢
I am little bit confusing about ">>" in scala. Daniel said in Scala parser combinators parsing xml? that it could be used to parameterize the parser base on result from previous parser. Could someone give me some example/hint ? I already read scaladoc but still not understand it.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我所说,它用于参数化解析器,但让我们通过一个示例来清楚地说明这一点。
让我们从一个简单的解析器开始,它解析一个数字后跟一个单词:
在 RegexParsers,这将解析“3 个水果”之类的内容。
现在,假设您还想要这些“n 件事”的列表。例如,“3种水果:香蕉、苹果、橙子”。让我们尝试解析它,看看它是如何进行的。
首先,我如何解析“N”件事?碰巧,有一个
repN
方法:它将解析“banana apple Orange”,但不会解析“banana, apple, Orange”。我需要一个分隔符。有
repsep
提供了这一点,但这不会让我指定我想要的重复次数。因此,让我们自己提供分隔符:好的,就是这样。我们现在可以为三件事编写整个示例,如下所示:
这种方法可行,只是我在 3 中修复了“N”。我想让用户指定有多少个。这就是
>>
的所在,也称为into
(是的,它是Parser
的flatMap
),进入。首先,让我们更改thirdThings
:这比您预期的稍微复杂一些,因为我强制它返回
Parser[List[String]]
。但是如何将参数传递给事物呢?我的意思是,这行不通:但我们可以这样重写:
这几乎足够好了,除了我现在丢失了
n
和what
:它只返回“列出(香蕉、苹果、橙子)”,而不是应该有多少,以及它们是什么。我可以这样做:只是最后的评论。您可能想问自己“你的意思是
flatMap
?这不是一个 monad/用于理解的东西吗?”为什么,是的,是的! :-) 这是listOfThings
的另一种编写方式:我不做
n ~ What <- number ~ word <~ ":"
因为它使用Scala 中的 filter
或withFilter
,它不是由Parsers
实现的。但这里还有另一种编写方式,它不具有完全相同的语义,但会产生相同的结果:这甚至可能让人认为“单子无处不在”的说法可能会有一些东西。 :-)
As I said, it serves to parameterize a parser, but let's walk through an example to make it clear.
Let's start with a simple parser, that parses a number follow by a word:
Under RegexParsers, this will parse stuff like "3 fruits".
Now, let's say you also want a list of what these "n things" are. For example, "3 fruits: banana, apple, orange". Let's try to parse that to see how it goes.
First, how do I parse "N" things? As it happen, there's a
repN
method:That will parse "banana apple orange", but not "banana, apple, orange". I need a separator. There's
repsep
that provides that, but that won't let me specify how many repetitions I want. So, let's provide the separator ourselves:Ok, that words. We can write the whole example now, for three things, like this:
That kind of works, except that I'm fixing "N" in 3. I want to let the user specify how many. And that's where
>>
, also known asinto
(and, yes, it isflatMap
forParser
), comes into. First, let's changethreeThings
:This is slightly more complicated than you might have expected, because I'm forcing it to return
Parser[List[String]]
. But how do I pass a parameter to things? I mean, this won't work:But we can rewrite that like this:
That is almost good enough, except that I now lost
n
andwhat
: it only returns "List(banana, apple, orange)", not how many there ought to be, and what they are. I can do that like this:Just a final comment. You might have wondered asked yourself "what do you mean
flatMap
? Isn't that a monad/for-comprehension thingy?" Why, yes, and yes! :-) Here's another way of writinglistOfThings
:I'm not doing
n ~ what <- number ~ word <~ ":"
because that usesfilter
orwithFilter
in Scala, which is not implemented byParsers
. But here's even another way of writing it, that doesn't have the exact same semantics, but produce the same results:This might even give one to think that maybe the claim that "monads are everywhere" might have something to it. :-)
方法
>>
采用一个函数,该函数给出解析器的结果,并使用它来构造一个新的解析器。如前所述,这可用于根据前一个解析器的结果对解析器进行参数化。示例
以下解析器解析包含
n + 1
整数值的行。第一个值n
表示要遵循的值的数量。解析第一个整数,然后使用该解析的结果构造一个解析器,该解析器解析 n 个进一步的整数。解析器定义
以下行假设您可以使用
parseInt: Parser[Int]
解析整数。它首先解析一个整数值n
,然后使用>>
解析形成解析器结果的n
个附加整数。因此解析器不会返回初始的 n(尽管它是返回列表的大小)。有效输入
无效输入
The method
>>
takes a function that is given the result of the parser and uses it to contruct a new parser. As stated, this can be used to parameterize a parser on the result of a previous parser.Example
The following parser parses a line with
n + 1
integer values. The first valuen
states the number of values to follow. This first integer is parsed and then the result of this parse is used to construct a parser that parsesn
further integers.Parser definition
The following line assumes, that you can parse an integer with
parseInt: Parser[Int]
. It first parses an integer valuen
and then uses>>
to parsen
additional integers which form the result of the parser. So the initialn
is not returned by the parser (though it's the size of the returned list).Valid inputs
Invalid inputs