联盟的两套在Scala
发布时间:2020-12-16 09:17:31 所属栏目:安全 来源:网络整理
导读:从与 here相关的问题,我发现在Scala中实现Union: def union(a: Set,b: Set): Set = i = a(i) || b(i) And Set是一个类型的函数: type Set = Int = Boolean 现在我明白,在Scala中,一个函数从Int映射到Boolean,我进一步了解了如何执行这个语句: a(i) || b(i
从与
here相关的问题,我发现在Scala中实现Union:
def union(a: Set,b: Set): Set = i => a(i) || b(i) And Set是一个类型的函数: type Set = Int => Boolean 现在我明白,在Scala中,一个函数从Int映射到Boolean,我进一步了解了如何执行这个语句: a(i) || b(i) 但我不明白的是我在这里是什么它从何而来?当它找到一个匹配之间的集合,它返回true,如果它确实,我在哪里过滤它? 解决方法
假设我们有一个叫做SoSet的对象
object SoSet { type Set = Int => Boolean val a : Set = ??? val b : Set = ??? def isItem(item : Int) = a(item) || b(item) } isItem的签名由Int =>布尔型是一套.到现在为止还挺好. 但是现在我们只想返回函数isItem(即Set). 所以我们可以定义联合函数(现在没有参数,稍后再加上). object SoSet { //.. def union : Set = isItem // returns the function isItem } 现在可以将isItem重构为anonymous function. object SoSet { //.. def union : Set = { (item : Int) => a(item) || b(item) } } 允许将对象SoSet中的a和b设置为def union的参数.重构项目到我. object SoSet { type Set = Int => Boolean def union(a : Set,b : Set) : Set = (i : Int) => a(i) || b(i) } UPDATE val s1 = Set(1,2,3) val s2 = Set(2,3,4) val s3 = union(s1,s2) // returns the function.. Int => Boolean = <function1> s3(2) // invokes the function & checks if 2 is present in the union (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读