Monthly Archives: May 2010

Scala Problem Number Two

Following with my take on the Scala 99 Problems that I started here, lets talk a little bit about the second one. Here is the problem definition, from the original page:

P02 (*) Find the last but one element of a list.
Example:

scala> penultimate(List(1, 1, 2, 3, 5, 8))
res0: Int = 5

This one is very similar to the last problem and has similar solutions as well. The first solution I tried was, again, an ugly one:

def penultimate[A](list: List[A]): A = {
  list(list.size-2)
}

Terrible, but works. The second solution is nicer, with pattern matching, as before. But this time I learned a little bit from Phil Gold’s solution for the first problem, so prettier it is:

def penultimateWithPatternMatching[A](list: List[A]): A = list match {
  case x :: y :: Nil => x
  case _ :: tail => penultimateWithPatternMatching(tail)
  case _ => throw new NoSuchElementException
}

You can find Phil Gold’s solution to this problem here. His simpler solution is a bit better than mine, so my mind expands a little bit and I learn =). Also, he plays with a generic finder – interesting to read, and more mind expanding happens.


Ninety-Nine Scala Problems

Listening to a few different podcasts (The Java Posse among them), I found out about Ninety-Nine Scala Problems. As the description on the site says, it is an adaptation of the original Ninety-Nine Prolog Problems. I find the idea very interesting and decided to give it a go.

In summary, this is a bunch of (sort-of) small problems to be solved. You can use them to improve your knowledge in a programming language – Scala in this case. I won’t pretend I know where this trend started, but whoever had this idea for the first time, kudos to you!

During the next (bunch of, a lot of, certainly not few) weeks I’ll go through the problems trying to solve them, and post my impressions here. Those impressions will most likely contain my solutions, so please bear that in mind if you intend to try to solve the problems yourself as well. Also, the page mentioned above with the problems has possible solutions available and I’ll mention them, with links to the originals.

US_99

US_99

To allow you to understand what that feels like, lets talk about the first problem, which is really easy.

The definition of the first problem says:

P01 (*) Find the last element of a list.
     Example:
     scala> last(List(1, 1, 2, 3, 5, 8))
     res0: Int = 8

I came up with basically two solutions. One that is pretty obvious and ugly, using the size of the list:

def last(list: List[Any]): Any = {
  list(list.size-1)
}

The other solution used Pattern Matching to decompose the list, until we have only the last element:

def lastUsingCase(list: List[Any]): Any = list match {
  case x :: Nil => x
  case x :: list => lastUsingCase(list)
  case _ => Unit
}

Phil Gold”s solutions include one Pattern Matching version, which is a little more elegant than mine, and a version using a built in feature that gives direct access to the last element of the list (the last function). I didn’t know about this function, so I already learned something new from this whole 99 Problems thing =) You can find Phil Gold’s solutions for this problem here.


TDD Talk at UMC

Yesterday I gave a speech, with two friends, about Test Driven Development at UMC, here in Mogi das Cruzes – SP – Brazil. The talk was nice, and this post is just to thank Claudinho and Fabio, the two friends mentioned previously, and Rodrigo Rocha and the University, for providing the space.

Thank you all =)


Motodev Summit Brasil 2010

One more quick post about an event. This time, the Motodev Summit 2010, that happened this last Wednesday, May 5th, 2010. I don’t have too much to say about it; in summary, it was a lot more marketing from Motorola than I was expecting. I was expecting more deep, technical stuff.

One of the talks...

One of the talks...

But there is one point I would like to make about Motorola, which is the reason to write this post. Motorola is releasing the Shops4Apps store. It is basically one more option for developers to sell mobile applications. It took me a while to understand why they are doing it, since there is already an Android Market, and almost all of their phones are Android phones today.

So here is what I think they’re doing: they are trying to beat Nokia, directly. Nokia has their own market place, called Ovi Store. It is out there, running and being used for a while now. Motorola wants to have their store as well. Simple as that. Also, I can think of one more reason they might be doing this: competitive advantage against other Android devices. Something like “our Android is better than the other’s”.

Boring... lets check e-mail...

Boring... lets check e-mail...

Now, as far as I know, we have quite a few market places / stores to sell mobile applications already: the Apple App Store (iPhone, iPad etc), Android Market, Ovi Store and now Shop4Apps. And this is to talk about the big ones; there are smaller initiatives out there.

What do you think? Is Motorola crazy to try to compete in this market all by itself instead of, for example, joining the Android Market or something like that? Or is this the right move? And what about the other markets? Do you think they are viable options to make some money? I might be wrong, but here in Brazil, at least for now, the Apple App Store seems to be the only place you can make some money today.

Please leave a comment with your thoughts!


Follow

Get every new post delivered to your Inbox.