Well I did try to implement the map function. What I did was to check if the element belongs to the set and if yes, I apply the function to the set. I pass the result of the function to my already implemented singleton set which returns a Set. In the else block, I return false. Now, when I tried to test it using the following:

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!

Well, it's difficult to discuss the assignment without giving away the answer, and I don't understand what your code is trying to do, but I think you are making things way too complicated. You don't need your singleton set at all for this exercise, and the actual implementation of map()can be done in half a line of code that uses some of the functions we've already implemented. Of course, figuring out that half line of code was pretty hard (at least for me), so I suggest you take a look at David Hsu's suggestion on the thread I started for this question in the Assignments / Week 2 forum on the course website. David's formulation of the problem helped me figure out how to implement the requirement, so hopefully it will help you too.

David's suggestion hints that we use the exists method. But when using exists method, I would end up iterating.

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.

I mimiced my forall implementation and I was able to arrive at my answer for the map function. I will now have to reduce it to one line by making use of the exists method. Thanks for the pointer to your post. It helped me to finish it.

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.

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)).

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.

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.

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.

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24

posted

0

Thank you Joe. I think I got it now. Let me give it a try.

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24

posted

0

Here's the filter method:

...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.

Hi Wen. I hope you don't mind, but I've deleted your answer to the question. It's against the Coursera honor code to make any solutions for assignments available to anyone else. I know this is a very small example, but I still think we should keep to discussing assignments in general terms.

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24

posted

0

Hi Mathew. I apologize for posting the solution, I didn't know about that. Thanks for removing it.

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24

posted

0

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)

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?

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24

posted

0

Thanks Chris. I already submitted my exercise without the answer. Let me check that thread, for the sake of learning.