Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Scala Coursera - Week 3

chris webster
Bartender
Posts: 2407
33
Just thought we could celebrate the new course week with a fresh thread for any discussion of this week's topics. See you later.

"Once more unto the breach, dear friends..."

Joe Harry
Ranch Hand
Posts: 10127
3
Sheesh!

Matthew Brown
Bartender
Posts: 4568
9
Does that mean you don't like this week's set of assignments?

chris webster
Bartender
Posts: 2407
33
Actually it seems suspiciously straightforward so far this week (famous last words?). Currently working on the assignment: Q1 and Q2 done, Q3 looks OK with all the hints they give us, not looked at Q4 yet. But I'm sure there'll be a few "learning steps" along the way!

Matthew Brown
Bartender
Posts: 4568
9
Yeah, they weren't too bad - I did them last night. I was vaguely aware that my approach for the filter() method probably wasn't what they had in mind (given the way the questions were worded), but I think I came up with a better solution while walking to work this morning.

Joe Harry
Ranch Hand
Posts: 10127
3
Matthew Brown wrote:Does that mean you don't like this week's set of assignments?

Had a look at them, then had a look at binary trees in general to understand their behavior. Would start tonight with the assignments hopefully.

Joe Harry
Ranch Hand
Posts: 10127
3
Started with the filter0 method. Using the predicate size(set5.filter(tw => tw.retweets >= 1)) === 4, my implementation actually just returns 3 elements (a, b, c) instead of 4. I'm using the head and tail calls on the current object. Why would the d not show up in the accumulator? Need a bit of debugging!

Matthew Brown
Bartender
Posts: 4568
9
That's the last member of the set, so I'd guess it's your teminating step that isn't quite right.

My first version used head and tail (and worked fine), but because in the next question they mention those as a hint, I suspect we weren't intended to use them for the filter() method. I've now got a revised version that doesn't use it. I think the new version would be more efficient, but I haven't worked that out properly (or tested it).

Joe Harry
Ranch Hand
Posts: 10127
3
I got my filter0 implemented. Was missing an additional check! Union is also done. For Q3 using the remove and findMin0 was helpful. I should find some time tonight to work on Q4! This weeks tasks at first were a bit intimidating for me. Once I understood the binary tree, things started falling down in place.

A course on Data Structures and Algorithms would have come in handy for this course.

chris webster
Bartender
Posts: 2407
33
Joe Harry wrote: I should find some time tonight to work on Q4....

Recommend you follow their tip to check out the Scala List.exists() function.
Joe Harry wrote:A course on Data Structures and Algorithms would have come in handy for this course.

Yeah, and maybe a spare brain!

chris webster
Bartender
Posts: 2407
33
Matthew Brown wrote:I think the new version would be more efficient, but I haven't worked that out properly (or tested it).

I'm slightly nervous about the apparent simplicity of some of these recursive functions. I understand the Scala compiler can optimise tail recursion (into conventional loops) underneath, but I still find it hard to believe this kind of code won't blow up if you force it into too many levels of recursion.

Joe Harry
Ranch Hand
Posts: 10127
3
chris webster wrote:
Joe Harry wrote: I should find some time tonight to work on Q4....

Recommend you follow their tip to check out the Scala List.exists() function.
Joe Harry wrote:A course on Data Structures and Algorithms would have come in handy for this course.

Yeah, and maybe a spare brain!

Did you use the filter0 method for iterating the allTweets?

chris webster
Bartender
Posts: 2407
33
Joe Harry wrote:Did you use the filter0 method for iterating the allTweets?

Yes - as the assignment instructions imply, you can do it in a single line using a combination of filter() and List.exists().

This stuff is really cool - I just wish I didn't find it so hard to work out how to do it!

Joe Harry
Ranch Hand
Posts: 10127
3
I just did submit my assignments. Two test failures as below:

Just wanted to know what values you guys get for the google and apple tweets after applying the exists method from the List?

Was it the same with you all?

Matthew Brown
Bartender
Posts: 4568
9
Joe Harry wrote:

Yes, that's what I got.

Joe Harry
Ranch Hand
Posts: 10127
3
Given the following declarations in the unit tests:

Doing a Union

But when I change val e as below:

Any reason as to why the change in the retweets really matter in the Union? Can anyone verify this in your implementation of Union?

Matthew Brown
Bartender
Posts: 4568
9
I don't think it's a change in the number of tweets. It's a change in the tweet body. You're trying to add a duplicate (probably by accident) - but if you look at the provided implementation of incl() you'll see it won't add it.

Joe Harry
Ranch Hand
Posts: 10127
3
Thanks for the post. I understood that.

When I did a Union on my apple and google tweets, I ended up having 179 elements instead of 150 + 38. What could be the reason? I have a couple of other test cases that tests my Union method and it seems perfectly fine. What is your test result when doing an union on googleTweets and appleTweets?

Matthew Brown
Bartender
Posts: 4568
9
I'll have to check that later - don't have access to my code right now - but the obvious explanation is that there might be some tweets about Google and Apple. If so the union won't contain those Tweets twice.

Joe Harry
Ranch Hand
Posts: 10127
3
Matthew Brown wrote:I'll have to check that later - don't have access to my code right now - but the obvious explanation is that there might be some tweets about Google and Apple. If so the union won't contain those Tweets twice.

Isn't that the expected behavior of union? But do let me know the total elements resulting out of the union. If they differ, then I have some problems with my union implementation.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15574
46
I've submitted my week 3 assignments yesterday, no errors.

Joe Harry wrote:Just wanted to know what values you guys get for the google and apple tweets after applying the exists method from the List?

Was it the same with you all?

Yes, that looks correct.

Joe Harry wrote:
Matthew Brown wrote:I'll have to check that later - don't have access to my code right now - but the obvious explanation is that there might be some tweets about Google and Apple. If so the union won't contain those Tweets twice.

Isn't that the expected behavior of union? But do let me know the total elements resulting out of the union. If they differ, then I have some problems with my union implementation.

Yes, that's the expected behaviour of union, and there are tweets that are both in the googleTweets and appleTweets sets, so it shouldn't be a surprise that in the union of those two, there are less than 38 + 150 tweets. I also got 179 in the union.

Joe Harry
Ranch Hand
Posts: 10127
3
Thanks for confirming. So the potential candidate that resulted in my test failure is my ascendingByRetweet. I checked in my union that the tweet with retweet 321 is there and it vanishes after I did my ascendingByReTweet. Would analyze that tonight.

Matthew Brown
Bartender
Posts: 4568
9
I think that's the last item in the ascending list (from memory), so again you probably want to be looking at your terminating step.

Joe Harry
Ranch Hand
Posts: 10127
3
I figured it out. It was a silly copy paste error. In the tail, I was using the this.head but rather is should be the accumulators head during termination. I now see the 321 on my Trending as the last element sorted ascending.

Joe Harry
Ranch Hand
Posts: 10127
3
Week3 was a bit easier compared to Week2. May be it would have been even more challenging if they had asked us to implement the incl, remove methods or read the TweetData using the TweetReader. But anyways, I'm glad that I've now learnt about higher order functions and I can appreciate their beauty.

Wen Tong Lin
Greenhorn
Posts: 24
Hi everyone. I was wondering if you guys have to implement union() first, because you have to use that in filter0? if not did you guys just use incl()?

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35095
380
Wen Tong Lin wrote:Hi everyone. I was wondering if you guys have to implement union() first, because you have to use that in filter0? if not did you guys just use incl()?

No. Go through it in the order the exercise is given (filter before union.) It works out.

Joe Harry
Ranch Hand
Posts: 10127
3
Wen Tong Lin wrote:Hi everyone. I was wondering if you guys have to implement union() first, because you have to use that in filter0? if not did you guys just use incl()?

I used incl, head and tail methods to implement my filter0 method. To start with, call filter0 from the filter method in the base class.

Something like this:

Matthew Brown
Bartender
Posts: 4568
9
I reckon there are three ways of implementing filter(). One uses union(), but it's clear that's not expected because of the order we're implementing things in. My first attempt used tail and head, and that was accepted, but I don't think that was the intended approach because head/tail are given as a hint for the next question. There's a third way that just uses incl().

Joe Harry
Ranch Hand
Posts: 10127
3
Matthew Brown wrote:I reckon there are three ways of implementing filter(). One uses union(), but it's clear that's not expected because of the order we're implementing things in. My first attempt used tail and head, and that was accepted, but I don't think that was the intended approach because head/tail are given as a hint for the next question. There's a third way that just uses incl().

I had a fourth way that used both head / tail and incl.

Matthew Brown
Bartender
Posts: 4568
9
Joe Harry wrote:I had a fourth way that used both head / tail and incl.

That's what I meant by the second way. I didn't mean head/tail and nothing else.

Matthew Brown
Bartender
Posts: 4568
9
Week 4 continues on http://www.coderanch.com/t/594702/Scala/Scala-Coursera-Week...