Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Why Scala? Doesn't Java Work?

Java works, so why should we use Scala.
by

Sean Parsons

on 2 June 2011

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Why Scala? Doesn't Java Work?

Why Scala?
Doesn't Java Work? Let's Make A Class public class UserStockJava
{
private int id;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

UserStockJava that = (UserStockJava) o;

if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;

return true;
}

@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "UserStockJava{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
} public class UserGuava {
private int id;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public int hashCode() {
return Objects.hashCode(id, name);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
UserGuava that = (UserGuava)obj;
return Objects.equal(this.id, that.id)
&& Objects.equal(this.name, that.name);
}

@Override
public String toString() {
return Objects.toStringHelper(this)
.add("id", id)
.add("name", name)
.toString();
}
} case class User(id: Int, name: String) scala> val user = User(2, "Sen")
user: User = User(2,Sen)

scala> val corrected = user.copy(name = "Sean")
corrected: User = User(2,Sean) Pattern Matching OMGWTFBBQ THERE'S SO MUCH CODE OMGWTFBBQ THERE'S STILL SO MUCH CODE Switch statements are nice, better than if...else hell, but:
Can't switch on Strings, or anything that isn't an int.
Easy to miss a "break;" statement and bring about the end of days. abstract class Food
case class ChocolateCake(chocolatey: Int) extends Food
case class Pie(filling: String) extends Food

def describe(food: Food) = {
food match {
case ChocolateCake(chocolatey) if (chocolatey > 80) => "Very chocolatey cake."
case cake: ChocolateCake => "Just a normal chocolate cake."
case Pie(filling) => "Pie with a %s filling".format(filling)
}
}

describe(new ChocolateCake(99)) scala> def findHeader(line: String): String = {
val Parser = """Content-Type:(.*)""".r
line match {
case Parser(contentType) => contentType.trim
case _ => null
}
}

scala> findHeader("Flan")
res13: java.lang.String = null

scala> findHeader("Content-Type: Flange")
res14: java.lang.String = Flange Moar Pie scala> val foods = List(ChocolateCake(99), Pie("beef"), Pie("chicken"))
scala> foods.filter(food => food.isInstanceOf[Pie])
.map(pie => "Pie with a %s filling".format(pie.asInstanceOf[Pie].filling))

res22: List[String] = List(Pie with a beef filling, Pie with a chicken filling) scala> List(ChocolateCake(99), Pie("beef"), Pie("chicken")).collect {
case Pie(filling) => "Pie with a %s filling".format(filling)
}
res23: List[String] = List(Pie with a beef filling, Pie with a chicken filling) List<Food> foodList = ImmutableList.of(new ChocolateCake(90), new Pie("Beef"), new Pie("Pizza"));
System.out.println(
ImmutableList.copyOf(
Iterables.transform(
Iterables.filter(foodList, Predicates.instanceOf(Pie.class)),
new Function<Food, String>() {
public String apply(Food food) {
return "Pie with a %s filling".format(((Pie)food).filling);
}
}
)
)
); Given a list of foods, we want:
Only the pies.
A textual description more useful for a human. The Billion Dollar Mistake Sir Tony Hoare invented the null reference and that's his estimate of how much money they've cost over the years.

Handling them is annoying, causing "if (foo == null) ..." everywhere.

Who here has been caught by a NullPointerException (or equivalent)? Let's use Option instead, which has two implementing classes:
final case class Some[+A](x: A) extends Option[A]
case object None extends Option[Nothing] We've all used something like Option before... scala> val user1: Option[User] = None
scala> val user2 = Some(User(2, "Sean", 1000))

scala> user1.map(user => user.score).getOrElse(0)
res14: Int = 0

scala> user2.map(user => user.score).getOrElse(0)
res15: Int = 1000 scala> val user1: Option[User] = None
scala> val user2 = Some(User(2, "Sean", 1000))

scala> user1.size
res16: Int = 0

scala> user2.size
res17: Int = 1 Lookup a user for a given ID.
If we find one, lookup the preferences for that user, if those are present return those.
In all other cases return null.

Versus.

For each user for that has a given ID.
For each preference for a user, return those preferences. User user = db.lookupUser(12)
if (user != null) {
Preferences preferences = db.lookupPreferences(user)
if (preferences != null) {
return preferences;
}
}
return null;

Versus

for (user <- db.lookupUser(12); preferences <- db.lookupPreferences(user))
yield preferences for (user <- db.lookupUser(12); preferences <- db.lookupPreferences(user))
yield preferences

Versus

for (user <- db.lookupUser("Sean"); preferences <- db.lookupPreferences(user))
yield preferences Nearly Done Scala gets out of your way and allows you to get on with what not how.

Doesn't require pure functional programming, but can do it.

Even normal imperative code can benefit from type inference, way more useful collections and traits. Go Mental Scala - http://scala-lang.org
SBT - https://github.com/harrah/xsbt
Specs2 - http://specs2.org
Scalaz - http://code.google.com/p/scalaz/
Akka - http://akka.io If you've got a laptop, feel free to go to http://www.simplyscala.com
Full transcript