Path: blob/trunk/third_party/closure/goog/labs/iterable/iterable.js
2868 views
// Copyright 2014 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utilities for working with ES6 iterables.16* Note that this file is written ES5-only.17*18* The goal is that this should be a replacement for goog.iter which uses19* a now non-standard approach to iterables.20*21* @see https://goo.gl/Rok5YQ22*/2324goog.module('goog.labs.iterable');252627/**28* Get the iterator for an iterable.29* @param {!Iterable<VALUE>} iterable30* @return {!Iterator<VALUE>}31* @template VALUE32*/33exports.getIterator = function(iterable) {34return iterable[goog.global.Symbol.iterator]();35};363738/**39* Call a function with every value of an iterable.40*41* Warning: this function will never halt if given an iterable that42* is never exhausted.43*44* @param {function(VALUE): void} f45* @param {!Iterable<VALUE>} iterable46* @template VALUE47*/48exports.forEach = function(f, iterable) {49var iterator = exports.getIterator(iterable);50while (true) {51var next = iterator.next();52if (next.done) {53return;54}55f(next.value);56}57};585960/**61* Maps the values of one iterable to create another iterable.62*63* When next() is called on the returned iterable, it will call the given64* function {@code f} with the next value of the given iterable65* {@code iterable} until the given iterable is exhausted.66*67* @param {function(this: THIS, VALUE): RESULT} f68* @param {!Iterable<VALUE>} iterable69* @return {!Iterable<RESULT>} The created iterable that gives the mapped70* values.71* @template THIS, VALUE, RESULT72*/73exports.map = function(f, iterable) {74return new FactoryIterable(function() {75var iterator = exports.getIterator(iterable);76return new MapIterator(f, iterator);77});78};79808182/**83* Helper class for {@code map}.84* @param {function(VALUE): RESULT} f85* @param {!Iterator<VALUE>} iterator86* @constructor87* @implements {Iterator<RESULT>}88* @template VALUE, RESULT89*/90var MapIterator = function(f, iterator) {91/** @private */92this.func_ = f;93/** @private */94this.iterator_ = iterator;95};969798/**99* @override100*/101MapIterator.prototype.next = function() {102var nextObj = this.iterator_.next();103104if (nextObj.done) {105return {done: true, value: undefined};106}107108var mappedValue = this.func_(nextObj.value);109return {done: false, value: mappedValue};110};111112113114/**115* Helper class to create an iterable with a given iterator factory.116* @param {function():!Iterator<VALUE>} iteratorFactory117* @constructor118* @implements {Iterable<VALUE>}119* @template VALUE120*/121var FactoryIterable = function(iteratorFactory) {122/**123* @private124*/125this.iteratorFactory_ = iteratorFactory;126};127128129// TODO(nnaze): For now, this section is not run if Symbol is not defined,130// since goog.global.Symbol.iterator will not be defined below.131// Determine best course of action if "Symbol" is not available.132if (goog.global.Symbol) {133/**134* @return {!Iterator<VALUE>}135*/136FactoryIterable.prototype[goog.global.Symbol.iterator] = function() {137return this.iteratorFactory_();138};139}140141142