Sunday, June 23, 2019

Typing Iterables and Iterators with TypeScript

In this post, we are going to do a quick exploration of how TypeScript allows for compile time guarantees when working with the iteration protocol in JavaScript.

It is part of a series about Symbols, Iterables, Iterators and Generators in JavaScript, and a direct follow up of a previous post: Iteration Protocol And Configuring The TypeScript Compiler which was about the necessary compiler configurations needed to be able to work with iterables and iterators from TypeScript.

This post will then take things from there, and show how to add types to Iterables and Iterators in other to get compile time checking working.

Saturday, June 22, 2019

Iteration Protocol And Configuring The TypeScript Compiler

This post is part of the series of post on Symbols, Iterables, Iterators and Generators. It is part of various blog posts, where I penned down some of the things I learnt about TypeScript and JavaScript, while working on ip-num: A TypeScript library for working with ASN, IPv4, and IPv6 numbers.

This post would be about how to configure the TypeScript compiler in other to be able to work with the iteration protocol.

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. I have heard it described in some quarters as "JavaScript that scales".

The core feature of TypeScript that allows it to be described as such is the static guarantees and compile time safety it introduces, which allows invariants and structure about code base to be validated and checked for correctness at compile time.

In previous posts in this series, I have looked at Symbols In JavaScript and Iterables and Iterators in JavaScript. One of the things that stands out in these posts, is the requirement to have objects that have certain structure or adhere to stipulated protocols in other to be considered as Iterables or Iterators. Formally, these sets of requirement are known as the Iteration protocol

For example, for something to be considered an Iterable, it needs to adhere to the stipulated requirement that such a thing must have a Symbol.iterator method that returns an Iterator.

For something to be considered an Iterator, it needs to adhere to the requirement of having at least a next method.

And for the value returned from calling the next method on an iterator to be considered valid, it needs to be of a certain shape; that is the value must have a done property and a value property.

So it is obvious that for things to work out fine when working with the iteration protocal, a lot of requirements and structure needs to be satisfied. This is where TypeScript can help.

TypeScript allows us to code while making sure the checks and guarantees required by the iteration protocal are present and can be confirmed at compile time.

To do this, TypeScript provides a couple of interfaces that capture the structures and protocols of iterables and iterators. These interface can then be implemented at development time ensuring the requirements are adhered to.

But in other to be able to make use of these interfaces and compile time guarantees, we need to be able to configure the Typescript compiler appropriately. This post will show how to go about such configuration and it will contain the following sections:

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:



Sunday, June 02, 2019

How to Quickly Calculate Exponents With Base 2

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

This post is about how to quickly calculate exponents with base 2.

Unfortunately I am yet to find a magical tip to use for this task that does not involve having to memorise the result of some calculations, so the core of being able to quickly perform exponents of base 2, involves some memorisation.

Below are the calculations I have come to memorise:

21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
216 = 65536

The only tip to know is that with each increment of the exponent, the value of the calculation doubles. For example 26 is 64, increasing the exponent by 1, ie 27 leads to 128 which is a doubling of 64.

With this, I can then easily deduce the value of 217 as the double of 65536 which is 131072.



Previous post: How To Tell If Two IP Ranges Are Adjacent

Saturday, June 01, 2019

How To Tell If Two IP Ranges Are Adjacent

This post is about how to tell if two IP ranges are adjacent. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How to tell if two IP ranges are adjacent


Task:
You have two CIDR notations representing two IP ranges, and you want to tell if they are adjacent, or not.

Steps to answer:
  • The prefix value must be the same. CIDR notations with different prefix can never be adjacent
  • Used the steps outlined in How to quickly tell the first and last IP in a CIDR range, to figure out the first and last ip numbers in the two ranges
  • Check if adding 1 to the last IP address of one will lead to the starting IP address of the other. If so, then the two ranges are adjacent

Example:
Determine if 10.0.4.0/24 and 10.0.5.0/24 are adjacent




Previous post: How To Calculate How Many Smaller IP Ranges Can Be Gotten From a Bigger IP Range
Next post: How to quickly calculate exponents of base 2