The Artima Developer Community
Sponsored Link

Scala Buzz
A simple Java FP list map

0 replies on 1 page.

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 0 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.
A simple Java FP list map Posted: Jun 10, 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: A simple Java FP list map
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
Following on the comments I received on my previous entry about doing a bit of Java functional programming style, I made a very simple class to map a list. I'm following the style of what I have seen in the apache collections utilities. See previous post.

First note that I am doing it without generics, so for some this would be old style Java programming.

Here's the utility class.
ListUtils.java

package fp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListUtils {
static public List map(List elements, Applier applier) {
List result = new ArrayList();
for (Iterator it = elements.iterator(); it.hasNext();) {
Object element = it.next();
result.add(applier.apply(element));
}
return result;
}
}


And the interface used by the utility class.
Applier.java

package fp;

public interface Applier {
public Object apply(Object element);
}


Here's a client code example.

WithPoints.java

package domino;

public interface WithPoints {
public Integer getPoints();
}


Domino.java

package domino;

public class Domino implements WithPoints {
private Integer points;

public Domino(int points) {
this.points = new Integer(points);
}

public Integer getPoints() {
return points;
}
}


And finally!

package domino;

import fp.Applier;
import fp.ListUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {

public static void main(String[] args) {
List dominoes = new ArrayList();
dominoes.add(new Domino(1));
dominoes.add(new Domino(2));
dominoes.add(new Domino(3));

List points = ListUtils.map(dominoes, new Applier() {
public Object apply(Object domino) {
return ((WithPoints) domino).getPoints();
}
});

System.out.println(points); // [1, 2, 3]
}
}


So this time around, I did get a new list instead of transforming a list but I still think the simple iterator style is clearer and it avoids using a "utility class" for doing something really simple.

Iterator style

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


And of course, Scala still is neater for me 8)

val points = dominoes map (x => x.points)

Read: A simple Java FP list map

Topic: Scala Light... A Java successor? Previous Topic   Next Topic Topic: Parsing Performance of Scala for NetBeans

Sponsored Links



Google
  Web Artima.com   

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