Created
November 24, 2012 19:15
-
-
Save Beyamor/4141059 to your computer and use it in GitHub Desktop.
Java Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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