Skip to content

Instantly share code, notes, and snippets.

@Beyamor
Created November 24, 2012 19:15
Show Gist options
  • Save Beyamor/4141059 to your computer and use it in GitHub Desktop.
Save Beyamor/4141059 to your computer and use it in GitHub Desktop.
Java Map
import java.util.*;
public class Main {
public static class Map {
public static interface Fn<IN, OUT> {
public OUT call(final IN in);
}
public static <IN, OUT> List<OUT> map(
List<IN> xs,
Fn<IN, OUT> fn) {
final List<OUT> ys = new LinkedList<OUT>();
for (final IN x : xs) {
ys.add(fn.call(x));
}
return ys;
}
}
public static void main(String[] args) {
List<Integer> xs = new LinkedList<Integer>();
for (int i = 0; i < 10; ++i) xs.add(i);
List<Character> ys = Map.map(xs, new Map.Fn<Integer, Character>() {
public Character call(final Integer x) {
return new Character((char)('A' + x.intValue()));
}
});
for (final Character y : ys) { System.out.println(y); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment