The Artima Developer Community
Sponsored Link

Scala Buzz
The Scala Influence

2 replies on 1 page. Most recent reply: Jun 10, 2008 6:21 AM by Jeff Heon

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Jeff Heon

Posts: 40
Nickname: jfheon
Registered: Feb, 2005

Jeff Heon is software developer who has worked mainly with OO languages.
The Scala Influence Posted: Jun 9, 2008 7:40 PM
Reply to this message Reply

This post originated from an RSS feed registered with Scala Buzz by Jeff Heon.
Original Post: The Scala Influence
Feed Title: The Careful Programmer
Feed URL: http://thecarefulprogrammer.blogspot.com/feeds/posts/default
Feed Description: A (starting) collection of my musings about programming. I'll brush more often on Scala related topics since I am currently learning it.
Latest Scala Buzz Posts
Latest Scala Buzz Posts by Jeff Heon
Latest Posts From The Careful Programmer

Advertisement
A while ago, Frank Sommers asked the following:
How Has Functional Programming Influenced Your Coding Style?

If wish I could have replied something then, but I was just starting to learn Scala, and I couldn't.

Now, I'm still just at the beginning of grokking FP, but I can write about how it almost influenced a bit of Java code I had to write the other day at work.

I had a list of instances of a certain class, and I wanted to get a list made from the result of invoking a certain method on each instance.

We'll pretend I had a Domino class, and I wanted to invoke the method getPoints() on each instances

Here's how I would have done it in Scala:

class Domino(p: int) {
val points=p
}

val dominoes = List(new Domino(1), new Domino(2), new Domino(3))

val points = dominoes map (_.points)

Let's say it's a bit cryptic in Scala and make it a bit more explicit:
val points = dominoes map (x => x.points)

I'll skip the Java class definition, but it implements the interface WithPoints, which declares the "Integer getPoints()" method .

Now, just look at what I had to code in Java to get the point list:

List points;
for (Iterator iter = Dominoes.iterator(); iter.hasNext();) {
points.add((WithPoints) iter.next().getPoints());
}

Simple really, but I couldn't get over how much neater (to me) it was in Scala.

Now comes the functional programming influence part. It so happens that I had a transformation library at my disposal in my Java project: org.apache.commons.collections

It's not what I really wanted, but I could use some kind of functional programming style with it. And here's the end result:

List dominoes;
CollectionUtils.transform(dominoes, new Transformer() {
public Object transform(Object domino) {
return ((WithPoints) domino).getPoints();
}
});


So there, it's not so bad, except that:
1) It transforms my list instead of returning a new list, which is what I wanted.
2) The imperative version is, in my opinion, clearer.

And so this is how functional programming has (almost, but not quite) influenced my coding style so far.

Read: The Scala Influence


Ricky Clarkson

Posts: 63
Nickname: rclarkson
Registered: Jul, 2006

Re: The Scala Influence Posted: Jun 10, 2008 12:52 AM
Reply to this message Reply
I don't know why you would choose to use Transformer, as it has not been generified. It's a few lines of code to implement what it provides for Java 5. Here's some untested code:


interface F<T, R> { R invoke(T t); }
class Utils {
public static <T, R> Iterable<R> map(final Iterable<T> iterable, final F<T, R> f) {
return new Iterable<R>() {
public Iterator<R> iterator() {
final Iterator<T> wrapped = iterable.iterator();
return new Iterator<R>() {
public boolean hasNext() { return wrapped.hasNext(); }
public R next() { return f.invoke(wrapped.next()); }
public void remove() { throw null; } } }; } }

Jeff Heon

Posts: 40
Nickname: jfheon
Registered: Feb, 2005

Re: The Scala Influence Posted: Jun 10, 2008 6:21 AM
Reply to this message Reply
Hi Ricky,

We have to use java 1.4.2 at work.

I do know Transformer is not the ideal choice, it is just the only choice that I found in what was immediately available.

To be fair, I could just code something akin to Transform and call it Map 8)

Flat View: This topic has 2 replies on 1 page
Topic: Parsing Performance of Scala for NetBeans Previous Topic   Next Topic Topic: Edit distance in Scala

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use