LINQ is one of the best features of C#, and provides elegant way to write declarative and functional style code which is easy to read and understand.

With ES6 features like generator functions and techniques like currying, it is very easy to explore various possibilities to adopt LINQ style programming with JavaScript. Do note that with inbuilt methods like map , reduce , filter from Array object, one can write LINQ style code even with ES5 JavaScript. Multiple libraries like lodash, underscore provides comprehensive ways to work with collections, and arrays and they are written considering performance as a key criteria.

My objective behind this post is to show how these features can be used to write more concise and readable code.

Check dotless, an experimental library for LINQ style JavaScript programming. to try ‘npm i dotless’

Currying

Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. — Wikipedia

Lets see this in action

As you can see here, Currying becomes a handy technique to compose functions which are more readable and expressive.

Generator functions

Generator functions provides a easy way to write custom iterators. With yield keyword the program flow returns back to the caller, so do note that with generator function the execution is not continuous. For details refer Mozilla Docs.

Lets see this in action, The following code is taken from this medium article from Eric Elliott, refer it for more details on generator functions and other ES6 features.

As you can see from the output, the program flow is switching back between the while loop from Fibonacci function and the for loop which is consuming the Generator returned by Fibonacci function.

Lets combine currying and generator function to write some utility functions.

Lets use these utility functions to write some code

The variables like whichAreOdd , and takeFour may be over simplification in some cases, but they can help to refactor a complex code to express its intent clearly. Currying and generator makes the code readable as well as make it easy to work with collections/iterators.

Now one can conclude that generator functions as alternative to a function returning an array/collection, but they are fundamentally different. Generator function (kind of state machine) returns iterator (lazy execution) and not the actual array, and the program flow switch between Generator function and consumer.

A Generator function can return infinite sequence, while a consumer can only decide to take only n items from that sequence.

Lets combine the Fibonacci function with above utility functions.

Refer this codepen to play with above code. I am using this style to solve programming puzzles from Advent of Code, refer the repository here. For details check LINQ.ts file which defines most of the extensions required to write expressive code.

Thanks for reading, and hope you found it useful.