Affichage des articles dont le libellé est itérateur. Afficher tous les articles
Affichage des articles dont le libellé est itérateur. Afficher tous les articles

Itérateurs : SYMBOL


const m = 'myMethod';
class Foo {
    [m]() {}
}

Donc, on pourra écrire : 

class IterableClass {
    [Symbol.iterator]() {
        ···
    }
}

itérator

Ne marche pas si appellé deux fois

const dateRange = {
  from: new Date(2018, 0, 23),
  to: new Date(2018, 3, 28),
  [Symbol.iterator]() {
    this.current = this.from

    return this
  },
  next() {
    if (this.current <= this.to) {
      this.current.setDate(this.current.getDate() + 1)

      return {
        done: false,
        value: new Date(this.current)
      }
    }

    return {
      done: true
    }
  }
}

const dateList = Array.from(dateRange);

dateList.forEach(date => {
    console.log(date.toString())
})


Array.from + itérateur

let range = {
  from: "A",
  to: "W"
};

// 1. call to for..of initially calls this
range[Symbol.iterator] = function() {

  // 2. ...it returns the iterator:
  return {
    current: this.from,
    last: this.to,

    // 3. next() is called on each iteration by the for..of loop
    next() {
      // 4. it should return the value as an object {done:.., value :...}
      if (this.current <= this.last) {
let char = this.current;
this.current = String.fromCharCode(this.current.charCodeAt(0)+1);
        return { done: false, value: char };
      } else {
        return { done: true };
      }

    }
  };
};

for (let num of range) {
  console.log(num);
}

let t = Array.from(range,c=>c.toLowerCase());
console.log(t);

code


JS Bin on jsbin.com

itérator :

Voici deux écritures de Fibonacci :

const Fibonacci = {
    n1: 1,
    n2: 1,
    [Symbol.iterator]() {
      return this;
    },
    next() {
      let current = this.n2;
      this.n2 = this.n1;
      this.n1 = this.n1 + current;
      return {
        value: current,
        done: false
      };
    },
    return (v) {
      return {
        value: v,
        done: true
      };
    }
  };

let Fib = {
  [Symbol.iterator]() {
    let n1 = 1,
      n2 = 1;
    return {
      [Symbol.iterator]() {
        return this;
      },
      next() {
          let current = n2;
          n2 = n1;
          n1 = n1 + current;
          return {
            value: current,
            done: false
          };
        },
        return () {
          console.log(
            `stop`
          );
          return {
            value: v,
            done: true
          };
        }
    };
  }
};
for (var v of Fib) {
  console.log(v);
  if (v > 50) break;
}

itérateur

let range = {
  from: 65,
  to: 90,
  [Symbol.iterator]() {
    return this;
  },
  next() {
    if (this.from <= this.to) {
      return {
        done: false,
        value: String.fromCharCode(this.from++)
      };
    } else {
      return {
        done: true
      };
    }
  },
};



// A-W
for (let num of range) {
  console.log(num); 
}

Itérateurs

let elements = document.querySelectorAll("div"),
    callback = (el) => { console.log(el.name); };

elements[Symbol.iterator] = function () {
  let i = 0;
  const Max = 2;

  return { 
    next () {
      if ( i > Max ) 
        return { value : undefined, done: true };
       return { value : `\nLe ${elements[i].nodeName} N°${i} \t a pour texte ${elements[i++].textContent}`, done: false};
    }
  };
};

console.log([...elements]);



JS Bin on jsbin.com