# Scala - Coursera Course Discussion Thread

- 0

assert(contains(map(set1To4, (x: Int) => x + x)), 6) // This should result in true as 3 applied to x+x would be 6, but it returns false.

Later I tried to print the f(x) and s(x) and to my surprise, the s(x) prints 6

I do not get why it prints 6 and evaluates to false.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

No more Blub for me, thank you, Vicar.

- 0

Wouldn't I be needing a variable that I declare on my map function? A map function returns a Set. A Set here is something that takes an Int and returns a Boolean. So defining that on the map function body would look like:

Where I'm exactly stuck is when I call this function using my test case:

The value of x is 2. But s1 is 1. But the map function should evaluate to 2. I think I'm missing some basic understanding of anonymous functions.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

Joe Harry wrote:Where I'm exactly stuck is when I call this function using my test case:

The value of x is 2. But s1 is 1. But the map function should evaluate to 2. I think I'm missing some basic understanding of anonymous functions.

Surely the map should evaluate to a set that contains 2, not 2 itself?

My tip for getting a concise expression would be to step away from the code, and try and express what the result of the map function means. Complete the sentence "map(s1, f) contains x if...". Bonus points if you use the word "exists" somewhere.

- 0

Matthew Brown wrote:

My tip for getting a concise expression would be to step away from the code, and try and express what the result of the map function means. Complete the sentence "map(s1, f) contains x if...". Bonus points if you use the word "exists" somewhere.

if x exists in s and maps to the result of applying the function on x (f(x)).

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

Matthew Brown wrote:Joe Harry wrote:if x exists in s and maps to the result of applying the function on x (f(x)).

No, x doesn't have to exist in s. Using your doubling example, 6 doesn't exist in {1, 2, 3}, but does exist in {2, 4, 6}.

then, it would be as follows:

Set s has an element which when applied the function f(?) would get x where ? is an element in the Set s

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

Matthew Brown wrote:Pretty much, yes. And "s has an element <insert condition here>" is exactly what the exists method is for.

f(?) == x where ? is the element in s. I did infact complete the map function but I did not use the exists method. I'm having some mis-understanding with using the anonymous functions. I will have to think about calling the exists method instead of iterating once again in my map.

- 0

Its good to see this thread. I was wondering if anybody can give me a hint on how to define the filter function? The description says "Returns the subset of `s` for which `p` holds.". I'm confuse why p is not define as a Set, but instead it's a function with the same signature and return type.

My understanding was:

s = {1,2,3,4,5}

p = {2,3,4}

then it returns {2,3,4} ?

- 0

Wen Tong Lin wrote:Hello everyone,

Its good to see this thread. I was wondering if anybody can give me a hint on how to define the filter function? The description says "Returns the subset of `s` for which `p` holds.". I'm confuse why p is not define as a Set, but instead it's a function with the same signature and return type.

My understanding was:

s = {1,2,3,4,5}

p = {2,3,4}

then it returns {2,3,4} ?

Though technically a Predicate P is also a Set (that takes an Int and gives a Boolean), here it would be better to think about it as a Predicate or a Condition rather than a Set.

Your filter implementation should be able to satisfy whatever you give as the Predicate:

For example:

So if you call the filter method on a Set of numbers say 1 to 10, it should filter out everything that does not pass the Predicate. One hint for this would be to think about it as the inverse of your intersect method.

Did a rm -R / to find out that I lost my entire Linux installation!

- 0

...deleted...

test case:

test("filter will use predicate to every set") {

new TestSets {

val w = singletonSet(1)

val x = singletonSet(2)

val y = singletonSet(3)

val z = singletonSet(-4)

val a = union(w,x)

val b = union(a,y)

val c = union(b,z)

//c = {1,2,3,-4}

val f = filter(c, x => (x < 0))

// f = {1,2,3}

assert(contains(f,1), "f contains 1")

assert(contains(f,2), "f contains 2")

assert(contains(f,3), "f contains 3")

assert(!contains(f,-4), "f does not contain -4")

}

I am still digesting how this functional style of programming works, considering, we are using a function to hold the data. I am used to storing data in an array. In here the data is stored as closure.

- 0

- 0

Wen Tong Lin wrote:Hi guys,

I'm stuck with implementing map method. My solution looks very imperative. Any hints? This is what I did.

loop from -bound to +bound assign value to i

if s(i) is true return a function that does f(i)

A lot of us had problems with this - me included. I suggest you look at this thread on the course forum, especially

**David Hsu's**advice.

You can implement the map() function in a single short line of code, re-using some of the functions you created earlier in the assignment. For example, your "forall()" function may already check the valid range of integers, and "exists()" is supposed to re-use forall() as well, so maybe you don't need to re-implement the iterative stuff here?

No more Blub for me, thank you, Vicar.

- 0

I agree. Here's the link: http://aspose.com/file-tools |