PROPOSAL: fold keyword (original) (raw)
Joshua Bloch jjb at google.com
Thu Mar 5 13:28:11 PST 2009
- Previous message: PROPOSAL: fold keyword
- Next message: PROPOSAL: fold keyword
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Gabriel,
This doesn't seem compelling.
Here's your simple example:
// sum all numbers in the list
Integer sum =
fold(Integer n : list; // iterating collection element n
Integer result = 0) { // Where to accumulate the result, and
the initial value
result += n;
};
}
And here's how it looks today (with no change to the language or libraries):
Integer sum = 0;
for (Integer n : list)
sum += n;
Here's one of your complex examples:
// returns a new collection with the salaries of the employees not on vacation List salaries = fold(Employee e : emps; List newList = new ArrayList()) { if (!e.onVacation()) { newList.add(e.getSalary()); } }
And here's how it looks today:
List<BigDecimal> salaries = new ArrayList<BigDecimal>();
for (Employee e : emps)
if (!e.onVacation())
salaries.add(e.getSalary());
To my eyes, the current version is clearer as well as shorter.
Josh
- Previous message: PROPOSAL: fold keyword
- Next message: PROPOSAL: fold keyword
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]