iterators & generators in ecmascript 6.0

16
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Upload: eyal-vardi

Post on 14-Jul-2015

227 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 2: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

foreach (object obj in list) {DoSomething(obj);

}

Enumerator e = list.GetEnumerator();while (e.MoveNext()) {

object obj = e.Current;DoSomething(obj);

}

public interface IEnumerable{IEnumerator GetEnumerator();

}

public interface IEnumerator{object Current {get;}bool MoveNext();void Reset();

}

Page 3: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

class Range{constructor(low, high){ this.low = low; this.high = high; }

[Symbol.iterator](){return new RangeIterator(this);

}}

class RangeIterator{constructor(range){ this.range = Range; this.current = this.range.low; }

next(){if( this.current > this.range.high ){

return { value : undefined, done : true };} else {

return { value : this.current++, done : false };}

}}

for( let i of new Range(1, 3) ) { console.log(i); }

// Like IEnumerable in C#

// Like IEnumerator in C#

// Like foreach in C#

Page 4: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

var colors = new Set(['rojo', 'amarillo', 'azul']);

for (let word of colors){

alert(word); // "rojo", "amarillo", "azul" (the data)

}

Page 5: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

let arr = [ "blue", "green" ];

arr.notAnIndex = 123;

Array.prototype.protoProp = 456;

for (let k in arr) {

console.log(k);

}

Print:

> 0

> 1

> notAnIndex

> protoProp

Page 6: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Page 7: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

public class Test {public IEnumerator GetEnumerator() {

yield return "Hello"; yield return "World";

}}

Page 8: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

public class Test {public IEnumerator GetEnumerator() {

return new __Enumerator(this);}

} private class __Enumerator : IEnumerator{object current;int state;

public bool MoveNext() {switch (state) {

case 0:current = "Hello";state = 1;return true;

case 1:current = "World";state = 2;return true;

default:return false;

}}public object Current { get { return current; } }

}

Page 9: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function *simpleGenerator(){

yield "Hello";

yield "World";

for (var i = 0; i < 2; i++)

yield i;

}

var g = simpleGenerator();

print(g.next()); // "Hello"

print(g.next()); // "World"

print(g.next()); // 0

print(g.next()); // 1

Page 10: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

class Range{constructor(low, high){

this.low = low;this.high = high;

}*[Symbol.iterator](){

for (let i = this.low; i <= this.high; i++) {yield i;

}}

}

for (var i in new Range(3, 5) ) {print(i); // 3, 4, 5 in sequence

}

// Like IEnumerable in C#

Page 11: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function* g1() { yield 2; yield 3; }

function* g2() {yield 1;yield* g1();yield 4;

}

var iterator = g2();

console.log( iterator.next().value ); // 1console.log( iterator.next().value ); // 2console.log( iterator.next().value ); // 3console.log( iterator.next().value ); // 4

Page 12: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function* longRunningTask() {

let result1 = yield* step1();

yield;

let result2 = yield* step2();

yield;

yield* step3(result1, result2);

}

function* step1() { ... return result1; }function* step2() { ... return result2; }function* step3(p1, p2) { ... }

Page 13: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

function* fibonacci() {let [prev, curr] = [0, 1];for (;;) {

[prev, curr] = [curr, prev + curr];yield curr;

}}

for (n of fibonacci()) {// truncate the sequence at 1000if (n > 1000)

break;print(n);

}

Page 14: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

link

let items = [1, 2, 3, 4, 5, 6]

.asEnumerable()

.where(x => x % 2)

.select(x => x + 1);

for (let item in items) {

console.log(item);

}

//output will be 2, 4, 6

Page 15: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

http://www.2ality.com/

Understanding ECMAScript 6

http://ecmascript6.org/

A Few New Things Coming To JavaScript

HARMONY OF DREAMS COME TRUE

Harmony specification_drafts

Page 16: Iterators & Generators in ECMAScript 6.0

© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

eyalvardi.wordpress.com