CoffeeScript Comprehensions - CoffeeScript

What is CoffeeScript Comprehensions?

Inside the previous chapter, we've learned various loops supplied with the aid of CoffeeScript, while and its versions. In addition to the ones, CoffeeScript offers extra loop structures called comprehensions.

These comprehensions replace the for loop in different programming languages if we upload the optional guard clauses and the value of the current array index explicitly. Using comprehensions, we are able to iterate arrays as well as objects and the comprehensions that iterate arrays are expressions, and we can return them in a feature or assign to a variable.

S.No.

Statement & Description

1

for..in comprehensions

Thefor..incomprehension is the basic form of comprehension in CoffeeScript using this we can iterate the elements of a list or array.

2

for..of comprehensions

Just like Arrays CoffeeScriptScript provides a containers to store key-value pairs known as objects. We can iterate objects using thefor..ofcomprehensions provided by CoffeeScript.

3

list comprehensions

Thelistcomprehensions in CoffeeScript are used to map an array of objects to another array.

Index of comprehensions

The list/array of elements have an index which can be used in comprehensions. You can use it in comprehensions using a variable as shown below.

Example

The following example demonstrates the usage of index of the for…in comprehension in CoffeeScript. Save this code in a file with name for_in_index.coffee


Open the command prompt and compile the .coffee file as shown below.


On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

Postfix form of comprehensions

Just like postfix if and unless, CoffeeScript provides the postfix form of the Comprehensions which comes handy while writing the code. Using this, we can write the for..in comprehension in a single line as shown below.

Assigning to a variable

The comprehension we use to iterate arrays can be assigned to a variable and also returned by a function.

Example

Consider the example given below. Here you can observe that we have retrieved the elements of an array using for..in comprehension and assigned this to a variable named names. And we also have a function which returns a comprehension explicitly using the return keyword. Save this code in a file with name example.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

The by keyword

CoffeeScript provides ranges to define a list of elements. For example, the range [1..10] is equivalent to [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] where, every element is incremented by 1. We can also change this increment using the by keyword of comprehensions.

Example

The following example demonstrates the usage of the by keyword of the for..in comprehension provided by CoffeeScript. Save this code in a file with name by_keyword_example.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics