[英]What does the '<-' do in scala?
I'm new to the language and trying to figure out how to read some of the code in it. 我是该语言的新手,并试图弄清楚如何阅读其中的一些代码。 Here is the example code that I'm trying to figure out: 这是我想要弄清楚的示例代码:
lazy val genHeap: Gen[H] = for{
n <- arbitrary[A]
h <- frequency((1,value(empty)),(9,genHeap))
} yield insert(n,h)
I don't quite understand what is going on: 我不太明白发生了什么:
Gen
? 返回类型是Gen
? <-
act as an =
operator? <-
是否作为=
运算符? Hello fellow Coursera student! 你好,Coursera同学! The Principles of Reactive Programming Course is not exactly the easiest place to start to learn Scala! 反应式编程课程的原理并不是开始学习Scala的最简单的地方! It is an advanced Scala course. 这是一门先进的Scala课程。
The type return is a Gen? 类型返回是Gen?
Yes, that's what the :
means. 是的,这就是:
意思。 (The Gen
itself is an object, a random generator to be precise, which can produce a sequence of values, each having the same type as its type parameter - in this case, H
.) ( Gen
本身是一个对象,一个精确的随机生成器,它可以生成一系列值,每个值与其类型参数具有相同的类型 - 在本例中为H
)
Does the <- act as an '=' operator? < - 是否作为'='运算符?
Not exactly. 不完全是。
and the yield statement.. as I understand, it is building a heap with each iteration by inserting a new element? 和yield语句..据我所知,它是通过插入一个新元素与每次迭代构建一个堆?
Actually it's a recursion, not an iteration... but essentially, yes. 实际上它是递归,而不是迭代......但实质上,是的。
A for..yield expression is a fancy way to write a series of map
, flatMap
and withFilter
invocations. for..yield表达式是编写一系列map
, flatMap
和withFilter
调用的一种奇特方式。 Let's desugar it down into ordinary Scala code: 让我们把它变成普通的Scala代码:
lazy val genHeap: Gen[H] = arbitrary[A].flatMap(n => frequency((1,value(empty)),(9,genHeap)).map(h => insert(n,h)))
So a H
generator ( genHeap
) is one that starts by generating an arbitrary A
, then generating an arbitrary H
(an empty H
with probability 0.1, or the result of invoking genHeap
itself again with probability 0.9), and then inserting the A
into the H
to get a new H
. 因此, H
生成器( genHeap
)是通过生成任意A
,然后生成任意H
(概率为0.1的空H
,或者以0.9的概率再次调用genHeap
本身的结果),然后将A
插入到H
得到一个新的H
These A
s and H
s are both abstract types, by the way. 顺便说一句,这些A
和H
都是抽象类型。
Yes, I'd say this is pretty advanced stuff . 是的,我会说这是非常先进的东西 。 If you don't even know what :
means, you're definitely starting in the wrong place. 如果你甚至不知道:
什么意思,你肯定是在错误的地方开始的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.