Skip to content

Add Collection#reduce #1649

Closed
Closed
@DartBot

Description

@DartBot

This issue was originally filed by @seaneagan


see http://en.wikipedia.org/wiki/Fold_(higher-order_function)

As with the other higher-order Collection methods, would be good to be consistent with JavaScript method names, and JavaScript has Array#reduce.

Similar to Collection#map (issue #945), this really wants to be a generic method (issue #254). The contract of this method could be something like:

R reduce<R>(R callback(R prev, E curr), [R initial = _somePrivateValue]) {
  Iterator iter = iterator();
  if(initial === _somePrivateValue) { // initial value not passed
    if(!iter.hasNext()) return null; // no items either
    initial = iter.next(); // defaults to first item
  }
  R prev = initial;
  while(iter.hasNext()) prev = callback(prev, iter.next());
  return prev;
}

Metadata

Metadata

Assignees

Labels

area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions