Scala set function

后端 未结 6 1884
忘掉有多难
忘掉有多难 2020-12-13 08:04

In Stanford Scala course I\'ve come across the following assignment:

Exercise 1 – Sets as Functions:

In this exercise we will represent sets

6条回答
  •  独厮守ぢ
    2020-12-13 08:27

    Here is another version of it using contains function:

    def union(s: Set, t: Set): Set = x => contains(s,x) || contains(t,x)
    
    def intersect(s: Set, t: Set): Set = x => contains(s,x) && contains(t,x)
    
    def diff(s: Set, t: Set): Set = x => contains(s,x) && !contains(t,x)
    
    def filter(s: Set, p: Int => Boolean): Set = x =>  contains(s, x) && p(x)
    

提交回复
热议问题