PROPOSAL: fold keyword (original) (raw)
Gabriel Belingueres belingueres at gmail.com
Tue Mar 10 08:51:11 PDT 2009
- Previous message: PROPOSAL: fold keyword
- Next message: PROPOSAL: fold keyword
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Tim thanks for the input. I apologize for taking so long to answer.
I'm not specially familiar with this library, but seems like it is the concurrent equivalent to what can be done with the Apache Commons Collections. Though the method call is very readable (given the appropriate naming of the predicates and mapping), this need the extra objects and anonymous classes I wanted to avoid creating. Not that there is anything wrong with that, it is just I wanted to find a more straightforward way to do it (if there is any).
Regards, Gabriel
2009/3/5 Tim Peierls <tim at peierls.net>:
Also check out ParallelArray, which is built on top of the ForkJoin framework proposed for Java 7: http://gee.cs.oswego.edu/dl/jsr166/dist/extra166ydocs/extra166y/ParallelArray.html The simple example becomes trivial: long sum = parallelLongArray.sum();
The more complex example that Josh mentioned would look like this: ParallelArray emps = ... // Salaries of employees not on vacation: return emps .withFilter(notPredicate(isOnVacation)) .withMapping(salaryField) .all(); where notPredicate is a static import from CommonOps, and isOnVacation and salaryField could be defined as: static final Predicate isOnVacation = new Predicate { public boolean op(Employee e) { return e.onVacation(); } }; static final Op<Employee, BigDecimal> salaryField = new Op<Employee,_ _BigDecimal> { public BigDecimal op(Employee e) { return e.getSalary(); } }; These examples are likely to outperform the sequential versions on a multiprocessor for moderately large inputs. --tim On Thu, Mar 5, 2009 at 4:28 PM, Joshua Bloch <jjb at google.com> wrote:
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 salaries = new ArrayList(); 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 ]