Showing posts with label Es6. Show all posts
Showing posts with label Es6. Show all posts

Friday, June 28, 2019

Generators and Iterators in JavaScript

This post concludes the series Symbols, Iterables, Iterators and Generators in JavaScript. It would be a whirlwind tour of generator functions in JavaScript and how they can be put to use when defining Iterators.

It is a direct follow up of Typing Iterables and Iterators with TypeScript and it would take the Iterator defined in the post, and improve it by making use of generator functions.

Basically, it will show how we can go from a verbose implementation that does not make use of generator functions:

class MakeRange {
    private _first: number;
    private _last: number
    constructor(first, last) {
     this._first = first;
     this._last = last;
   }
 
    [Symbol.iterator]() {
        return {
            next: () => {
                  if (this._first < this._last) {
                      return {value: this._first++, done: false}
                  } else {
                      return {value: undefined, done: true}
                 }
           }
       }
   }
 }

Into a more terse and clearer implementation that makes use of generator functions:

class MakeRange {
    private _first: number
    private _last: number

    constructor(first, last) {
     this._first = first;
     this._last = last;
    }

    *[Symbol.iterator]() {
        while (this._first < this._last) {
            yield this._first++
       }
   }
}

This post contains the following sections:

Meaning, we are going to first look at what generator functions are, then we see the various syntaxes for creating them, and finally we bring them to use in simplifying how we define iterables and iterators.

Let's get started.


Tuesday, June 18, 2019

Iterables and Iterators in JavaScript

This post is the second in a series of posts on Symbols, Iterables, Iterators and Generators. The previous post in the series was about Symbols in JavaScript. This post on the other hand, will be focusing on explaining what Iterables and Iterators are in JavaScript.

It contains the following sections:

Saturday, June 15, 2019

Symbols In JavaScript

This post is part of the series on Symbols, Iterables, Iterators and Generators. It focuses on Symbols in JavaScript.

My first encounter with Symbol in JavaScript was when I wanted to turn some classes in the ip-num library to iterables in other for them to be used in the "for of" syntax JavaScript provides.

The code involving Symbol that I ended up writing looked somewhat like this:

 [Symbol.iterator](): IterableIterator<IPv4> {
      return this;
  }

You can see one of such places where this exist in the ip-num codebase here

Back then, this piece of code totally threw me off, as I have never encountered something like this previously in JavaScript, and I had to take some time out to dig into Symbol in order to understand what was going on.

This post is a jot down of some of the key things I got to learn about Symbol. It also marks the beginning of a series of posts on Iterables and Iterators in JavaScript.

This post on Symbol will contain the following:


Friday, June 14, 2019

Symbols, Iterables, Iterators and Generators

This post introduces a series of posts that would be about Symbols, Iterables, Iterators and Generators in JavaScript and TypeScript.

It would capture some of the things I learnt about these topics while working on ip-num: a TypeScript library for working with ASN, IPv4, and IPv6 numbers.

The posts in this series include:




Saturday, December 08, 2018

Understanding JavaScript Modules As A TypeScript User

This post, is the first in a series of post that I will be writing about Modules in TypeScript. It is a continuation of the process of jotting down the various things I learnt while building ip-num. The previous post like this one, was Declaration Files in TypeScript: An Introduction. In which I shared how I came to wrap my head around declaration files in TypeScript.

Since the topic of Modules is quite an expansive one, instead of jamming everything I need to write into one blogpost, I will be spreading it out, across various posts, that would be published in the coming weeks/months.

This post is to get the ball rolling on that, and It would focus mainly on introducing the concepts of modules as we have them within the JavaScript ecosystem.

Since TypeScript is but a superset of JavaScript and JavaScript is still its only transpiler target, one really cannot get away from having to understand how things work in the JavaScript land in other to be able to deeply understand how various aspects of TypeScript fits together.

Modules are just one example of this.