Scala 中函数类型的类型别名
Type Alias for Function Types in Scala
我是最新的 Scala 语言,我无法理解 type Set = Int => Boolean
是如何工作的。我知道这是转换为布尔值的整数。我有 def map(s: Set, f: Int => Int): Set = y => exists(s, x => y == f(x))
For exist def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, i => !p(i))
) 和
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (diff(s,p)(a)) false
else iter(a + 1)
}
iter(-bound)
}
这两个函数我理解,但是map
函数我有问题。我只是不明白如果我称之为 map(Set(2,3,5), i => i * 20)
它是如何显示答案的。我得到 {40,60,100}
。我试图理解它,我知道 x
是 s
中的每个元素, f(x)
是乘以 20 的 x 元素。 y
的范围从 -1000 到1000 (bound = 1000
)。我这样读这个函数:y
is range -1000..1000
in exists function is getting our Set(2,3,5)
and after it give somthing with Boolean type
.我是如何得到像 {40,60,100}
这样的答案的。那里的新系列制作情况如何?
完整代码 ->
type Set = Int => Boolean
def contains(s: Set, elem: Int): Boolean = s(elem)
def diff(s: Set, t: Set): Set = (i: Int) => s(i) && !t(i)
val bound = 1000
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (diff(s,p)(a)) false
else iter(a + 1)
}
iter(-bound)
}
def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, x => !p(x))
def map(s: Set, f: Int => Int): Set = y => exists(s, x => y == f(x))
def toString(s: Set): String = {
val xs = for (i <- -bound to bound if contains(s, i)) yield i
xs.mkString("{", ",", "}")
}
println(toString(map(Set(2, 3, 5), (i: Int) => i * 20)))
考虑到 map()
实施...
y => exists(s, x => y == f(x))
...和调用...
map(Set(2,3,5), (i: Int) => i * 20)
...进行一些替换,您将得到:
val mappedSet = y => exists(Set(2,3,5), x => y == x*20)
因为 Set
是 Int => Boolean
(接受 Int
和 returns true 或 false), mappedSet
是一个函数,要求:
给定一个数字 y
,是否存在一个数字在 + 范围内同时通过(属于)Set(2,3,5)
并且当乘以 20 时,等于 y
?
mappedSet(60)
returns true 因为在 +-bounds 内 do 存在一个数字,3
,它既是 Set(2,3,5)
的成员,又乘以 20,等于 60
.
mappedSet(80)
returns false 因为找不到通过两个测试的数字。
我是最新的 Scala 语言,我无法理解 type Set = Int => Boolean
是如何工作的。我知道这是转换为布尔值的整数。我有 def map(s: Set, f: Int => Int): Set = y => exists(s, x => y == f(x))
For exist def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, i => !p(i))
) 和
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (diff(s,p)(a)) false
else iter(a + 1)
}
iter(-bound)
}
这两个函数我理解,但是map
函数我有问题。我只是不明白如果我称之为 map(Set(2,3,5), i => i * 20)
它是如何显示答案的。我得到 {40,60,100}
。我试图理解它,我知道 x
是 s
中的每个元素, f(x)
是乘以 20 的 x 元素。 y
的范围从 -1000 到1000 (bound = 1000
)。我这样读这个函数:y
is range -1000..1000
in exists function is getting our Set(2,3,5)
and after it give somthing with Boolean type
.我是如何得到像 {40,60,100}
这样的答案的。那里的新系列制作情况如何?
完整代码 ->
type Set = Int => Boolean
def contains(s: Set, elem: Int): Boolean = s(elem)
def diff(s: Set, t: Set): Set = (i: Int) => s(i) && !t(i)
val bound = 1000
def forall(s: Set, p: Int => Boolean): Boolean = {
def iter(a: Int): Boolean = {
if (a > bound) true
else if (diff(s,p)(a)) false
else iter(a + 1)
}
iter(-bound)
}
def exists(s: Set, p: Int => Boolean): Boolean = !forall(s, x => !p(x))
def map(s: Set, f: Int => Int): Set = y => exists(s, x => y == f(x))
def toString(s: Set): String = {
val xs = for (i <- -bound to bound if contains(s, i)) yield i
xs.mkString("{", ",", "}")
}
println(toString(map(Set(2, 3, 5), (i: Int) => i * 20)))
考虑到 map()
实施...
y => exists(s, x => y == f(x))
...和调用...
map(Set(2,3,5), (i: Int) => i * 20)
...进行一些替换,您将得到:
val mappedSet = y => exists(Set(2,3,5), x => y == x*20)
因为 Set
是 Int => Boolean
(接受 Int
和 returns true 或 false), mappedSet
是一个函数,要求:
给定一个数字 y
,是否存在一个数字在 + 范围内同时通过(属于)Set(2,3,5)
并且当乘以 20 时,等于 y
?
mappedSet(60)
returns true 因为在 +-bounds 内 do 存在一个数字,3
,它既是 Set(2,3,5)
的成员,又乘以 20,等于 60
.
mappedSet(80)
returns false 因为找不到通过两个测试的数字。