type Set = Int => Boolean
/**
* Returns whether all bounded integers within `s` satisfy `p`.
*/
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (contains(s, a) && !p(a)) false
else iter(a + 1)
}
iter(-bound)
}
/**
* Returns whether there exists a bounded integer within `s`
* that satisfies `p`.
*/
def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, (x => !p(x)))
/**
* Returns a set transformed by applying `f` to each element of `s`.
*/
def map(s: Set, f: Int => Int): Set = (x => exists(s, (y: Int) => f(y) ==
x))
所以这段代码。我不明白功能map
。在Scala中为内涵定义的集合实现映射
我看到它的输入是2个参数,这是设置和方法f。但是“身体”部分,我努力尝试,但仍然没有得到它。那是什么"y"
,以及为什么使用f(y)== x使它应用方法f来设置elemtns?
需要我的一些解释。 谢谢!
你应该包括你的'Set'定义,否则这个问题对没有参加Scala Coursera的人来说是没有意义的;) – 2014-09-24 06:19:00
解释我编辑问题的摘要:参见[intensional definition](https ://en.wikipedia.org/wiki/Intensional_definition)。 – 2014-09-24 06:27:34
没有读过Coursera Honor Code,那么? – 2014-09-24 07:01:14