Divide an integer into evenly distributed groups with an ES6 generator

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the starting integer.

The following ES6 generator function will perform just that:

Read more of this post

Advertisements

Using ES6 generators for custom iterators

We looked at the basics of using ES6 generators in this post. Generators also come with symbol iterators which can be applied to objects. A symbol iterator helps us implement a custom iterator for our objects. We can declare how our object should behave in a for-of loop. Symbol iterators also involve some weird syntax but generators are weird anyway so a little more weirdness should be fine.

Let’s start off with a simple example:

let partyOrganisation = {
  host: 'John',
  dj: 'Jane',
  food: 'Peter',
  games: 'Mary'
}

Read more of this post

Using ES6 generators in for loops

Generator functions are a new feature in ES6. A generator is a function that can be entered multiple times and each time it will return something else. Generators are definitely strange at first. If you are familiar with the yield keyword in C# and how it is used then you’ll catch up with generators in ES6 quicker than others. I’m not aware of a similar feature in Java. Generators are strongly linked to iterators and arrays as we’ll see in a bit. I believe that Python uses yield as well.

The best thing is if we jump right into it.

Read more of this post

Asynchronous operations using Promises in ES6

The Promise object in ES6 makes asynchronous programming in JavaScript easier. Asynchronous method calls are most useful in case of long-running function calls such as AJAX calls to a backend method. We dispatch a function call and take care of its result when it is done without holding up the main execution thread.

A Promise object accepts two functions: resolve and reject which can be called within the Promise depending on its outcome.

Here’s a basic example of initialising a Promise object:

Read more of this post

Declaring variables in ES6

We don’t use the var keyword in ES6 anymore to declare variables:

var name = 'John'

Instead we have the “let” and “const” keywords for this purpose. With “let” we declare variables whose value can change over time:

Read more of this post

The rest and spread operator in ES6

ES6 comes with a new operator that consists of three dots: … It is strongly related to arrays. It has two major purposes:

Let’s see how the … operator works.

Read more of this post

Enhanced string concatenation/interpolation in ES6

ES6 comes with a new syntactic feature that makes concatenating strings easier. Here’s an example of creating an object with a function with traditional string concatenation in JavaScript:

Read more of this post

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: