Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- comic agile
- essay
- web
- AI
- agile
- Linux
- Artificial Intelligence
- Programming
- ubuntu
- Python
- MySQL
- program
- Software Engineering
- hadoop
- QT
- django
- Italy
- Kuala Lumpur
- RFID
- history
- Book review
- hbase
- France
- Java
- erlang
- Book
- Malaysia
- management
- programming_book
- leadership
Archives
- Today
- Total
Example functional programming fold left and fold right in java 본문
Programming/Java
Example functional programming fold left and fold right in java
halatha 2011. 4. 30. 04:28
import java.util.ArrayList;
import java.util.List;
// http://abel-perez.com/example-functional-programming-fold-left-and
public class TestFunctionalSum {
public static int foldLeft(List<Integer> elements,
int seed,
Function<Integer, Integer> function) {
int accumulated = seed;
for ( final Integer element : elements ) {
accumulated = function.apply(element, accumulated);
}
return accumulated;
}
public static void main(final String[] args) {
final List<Integer> elements = new ArrayList<Integer>();
elements.add(2);
elements.add(6);
elements.add(10);
elements.add(5);
elements.add(2);
final int result =
foldLeft(elements, 0, new Function<Integer, Integer>() {
public Integer apply(Integer x, Integer y) {
return x + y;
}
});
System.out.println(result);
}
}
interface Function<A, B> {
public B apply(A element, B accumulated);
}
Comments