more idiomatic clojure (assignment 3 discussion)

90 views
Skip to first unread message

Norman Richards

unread,
Oct 8, 2012, 12:46:31 AM10/8/12
to austin-scala...@googlegroups.com
I don't know how idiomatic the scala is here, but I wrote the following:

def matchesKeyword(text:String)(keyword:String) = text.contains(keyword)
def tweetMatchesAnyKeyword(keywords: List[String])(tweet: Tweet) :
Boolean =
!keywords.filter(matchesKeyword(tweet.text)).isEmpty

val googleTweets: TweetSet =
TweetReader.allTweets.filter(tweetMatchesAnyKeyword(google))

I used currying because I wasn't getting the function literal syntax
right. I eventually looked into and did it that way:

def tweetMatchesAnyKeyword(keywords: List[String], tweet: Tweet) :
Boolean =
!keywords.filter(tweet.text.contains(_)).isEmpty

val googleTweets: TweetSet =
TweetReader.allTweets.filter(tweetMatchesAnyKeyword(google,_))

So, the first question is, which looks better to scala people?
Currying or function literals?


Second, question: for the tweetMatchesAnyKeyword function, I'm
filtering keywords that match string and testing if that list is
empty, indicating no matches. I really don't like that code. In my
head, I had this clojure code:

(some #(matches-keyword tweet %) keywords)

For those unfamiliar #() is a function literal. it does essentially
what matchesKeyword(tweet,_) would do.

I don't seem to see a some/any function for lists that does this. Am
I missing something obvious in the API? How would you normally write
this in scala?

Cody Koeninger

unread,
Oct 8, 2012, 1:23:36 AM10/8/12
to austin-scala...@googlegroups.com
Closest thing to clojure's some is probably find, but in this case I
think you only need exists.

I personally don't think currying looks very idiomatic in scala, but
maybe that's just me.
> --
> You received this message because you are subscribed to the Google Groups "Austin Scala Enthusiasts" group.
> To unsubscribe from this group, send email to austin-scala-enthu...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Norman Richards

unread,
Oct 8, 2012, 9:57:54 AM10/8/12
to austin-scala...@googlegroups.com
On Mon, Oct 8, 2012 at 12:23 AM, Cody Koeninger <co...@koeninger.org> wrote:
> Closest thing to clojure's some is probably find, but in this case I
> think you only need exists.

That's what I was looking for. So much better:

def tweetMatchesAnyKeyword(keywords: List[String], tweet: Tweet) : Boolean =
keywords.exists(tweet.text.contains(_))


> I personally don't think currying looks very idiomatic in scala, but
> maybe that's just me.

I haven't used a language with that kind of syntax, so it's hard to
say. It was certainly very easy to write that, but to compare again
to Clojure, I'd write out the function completely and use either
partial application to produce the curried function or the function
literal, like in the scala above. (I assume that's what it is called
at least) Of course, if there the first curried argument needed
processing of some sort that didn't need to happen on every
invocation, I'd write it out as a function generator.

I wonder what's up for this week.

Cody Koeninger

unread,
Oct 9, 2012, 1:06:38 AM10/9/12
to austin-scala...@googlegroups.com
You should also be able to write keywords.exists(tweet.text.contains) without the underscore.  If you're passing a simple function to a higher order function, you generally don't need to explicitly write out a function literal.  The name of the function will work on its own, e.g.

List(1,2,3,4).foreach(println)
instead of
List(1,2,3,4).foreach(println _)
or even
List(1,2,3,4).foreach((x: Int) => println(x))


As far as currying goes (or at least, multiple argument lists, since you could always curry stuff manually), part of the issue is that there are several other idiomatic uses for multiple argument lists:


So its not just me ;)

Michael Douglass

unread,
Oct 10, 2012, 10:46:29 PM10/10/12
to austin-scala...@googlegroups.com
I felt part of my mind tearing away while doing this assignment set.

Probably the biggest issue I had was not fully understanding head and tail -- if he had just called them car and cdr, I'd have understood and been able to do this exercise without thrashing brain cells.

I did catch myself getting tripped up on the syntax of the language since I hadn't touched it in a week, and have been busy with lots of other stuff in the interim.  :/

That said, I secured my 10 of 10.  :)

John Cohorn

unread,
Oct 11, 2012, 8:00:35 PM10/11/12
to austin-scala...@googlegroups.com
This one took me a good bit longer than the previous ones which seemed easy. It was filter where I got stuck. Didn't want to use head or tail which felt like cheating. and kept getting tripped up with tree traversal corner cases. Finally occurred to me to pass left.filter as the accu argument of right.filter and got it to a single if/else in 1-2 lines. Rather different than how I remember doing it some years ago in C while trying too hard to avoid recursion.

My go at getting the apple/google tweets was a little different than the above because I found Scala's foldLeft for lists. Used keywords.foldLeft with union and filter on contains(keyword). Sorry if a little vague, didn't want to write it verbatim after the nasty note they sent out about solutions being posted publicly.

Howdy by the way, and are there any meetups happening soon?

Cody Koeninger

unread,
Oct 12, 2012, 12:48:11 AM10/12/12
to austin-scala...@googlegroups.com
Haha, yeah, norman should probably take down that github he posted a while back, lest they kick him out of the class.

As far as meetups, I think absent other lunchtime suggestions, we're going to keep having lunch at austin java on fridays and talking with whoever shows up.

I'll post up a separate thread for suggestions about topics for a more organized meetup.

Norman Richards

unread,
Oct 12, 2012, 12:48:28 AM10/12/12
to austin-scala...@googlegroups.com
On Thu, Oct 11, 2012 at 7:00 PM, John Cohorn <john....@gmail.com> wrote:
> Howdy by the way, and are there any meetups happening soon?

I don't know, but if there's a lunch tomorrow, I'll try and show. I
haven't watched the week 4 videos, but I went did the assignment
(huffman encoding) just now to have something to talk about. No idea
if the code looks anything like what is expected, but it works.

Dan Luu

unread,
Oct 12, 2012, 12:54:29 AM10/12/12
to austin-scala...@googlegroups.com
I'll try to make it tomorrow, but it's kinda iffy.

For union/union0, I feel like my solution is really ugly; there surely must be a cleaner and/or more idiomatic solution than this

union calls union0 with 'this' as the accumulator
in union0,
  if(that.isEmpty) accu
  else if (this.contains(that.head)) union0(that.tail, accu)
  else ...

I'm not even sure why that bothers me. It just seems like there must be a simpler solution.

Cody Koeninger

unread,
Oct 12, 2012, 1:05:25 AM10/12/12
to austin-scala...@googlegroups.com
Dan, I'm pretty sure incl does the work for you of checking that the
item isn't already in the set.

John Cohorn

unread,
Oct 12, 2012, 2:17:44 AM10/12/12
to austin-scala...@googlegroups.com
Tomorrow might be tight and I'm also moderately north, but fingers crossed else it'll catch y'all next time. 

Norman Richards

unread,
Oct 12, 2012, 11:49:54 AM10/12/12
to austin-scala...@googlegroups.com
On Thu, Oct 11, 2012 at 11:48 PM, Cody Koeninger <co...@koeninger.org> wrote:
> As far as meetups, I think absent other lunchtime suggestions, we're going
> to keep having lunch at austin java on fridays and talking with whoever
> shows up.

With ACL starting up, I'm staying far away from the Zilker area this weekend.

Cody Koeninger

unread,
Oct 12, 2012, 1:02:40 PM10/12/12
to austin-scala...@googlegroups.com

Yeah, traffic is broken, I forgot about that. We're bailing for this week, sorry if anyone fought through traffic to get there.

Reply all
Reply to author
Forward
0 new messages