Photo by rawpixel.com on Unsplash I will cover LINQ methods for initializing a sequence.
Here are the methods covered so far.
- Part 1 〰️ Select, Aggregate, Where, OrderBy (Ascending, Descending)
- Part 2 〰️ Any, Distinct, Concat, SelectMany
- Part 3 〰️ Reverse, Zip, Min/Max
- Part 4 〰️ Union, Intersect, Except
- Part 5 〰️ Sum, Average, Count
- Part 6 〰️ First, Last, DefaultIfEmpty, Skip, Take
- Part 7 〰️ Empty, Repeat, Range
- Part 8 〰️ All, Contains, SequenceEqual
🔴 Overview
In this article, I will cover following methods.
[table id=8 /]
I won't be using Orders collections as I have in previous articles.
🔴 Examples
🔸 Empty
C# is a typed language while JavaScript is not. .Empty returns a typed sequence but since there is no need for a type, you can just return an empty array [] in JavaScript
| private static void EmptyDemo() | |
| { | |
| var emptyOrders = Enumerable.Empty<Order>(); | |
| PrintHeaderFooter("This prints no order since the sequence is empty", () => PrintOrders(emptyOrders), indentBy); | |
| } |
| function emptyDemo() { | |
| const emptyOrders = []; | |
| printHeaderFooter( | |
| "This prints no order since the sequence is empty", | |
| () => printOrders(emptyOrders), | |
| indentBy | |
| ); | |
| } |
Results
| // C# | |
| ==================== Empty DEMO - Get an Empty Order Sequence ==================== | |
| ==================== This prints no order since the sequence is empty ==================== | |
| // JS | |
| ==================== Empty DEMO - Get an Empty Order Sequence ==================== | |
| ==================== This prints no order since the sequence is empty ==================== |
Nothing surprising as shown above 😀.
🔸 Repeat
For this demo, I thought it was an overkill to use Orders collection used in last 6 articles so simply repeated a sentence, "I love your smile" five times.
| private static void RepeatDemo() | |
| { | |
| var texts = Enumerable.Repeat("I love your smile", 5); | |
| foreach (var text in texts) | |
| { | |
| WriteLine(text); | |
| } | |
| } |
| function repeatDemo() { | |
| const texts = Array(5) | |
| .fill() | |
| .map(_ => "I love your smile"); | |
| texts.map(text => WriteLine(text)); | |
| } |
Results
| // C# | |
| ==================== Repeat DEMO - Repeat Texts ==================== | |
| I love your smile | |
| I love your smile | |
| I love your smile | |
| I love your smile | |
| I love your smile | |
| // JS | |
| ==================== Repeat DEMO - Repeat Texts ==================== | |
| I love your smile | |
| I love your smile | |
| I love your smile | |
| I love your smile | |
| I love your smile |
JavaScript version uses Array#fill method to simply fill it with undefined and populate it with map.
🔸 Range
Same here with the Repeat Demo, I will use a simple case to demo how to generate a range of numbers.
| private static void RangeDemo() | |
| { | |
| var oneToTen = Enumerable.Range(1, 10); | |
| WriteLine($"One to Ten => {string.Join(",", oneToTen)}"); | |
| var randomRange = Enumerable.Range(999, 3); | |
| WriteLine($"Three numbers from 999 => {string.Join(",", randomRange)}"); | |
| } |
| function rangeDemo() { | |
| const oneToTen = Array(10) | |
| .fill() | |
| .map((_, i) => i + 1); | |
| WriteLine(`One to Ten => ${oneToTen.join(",")}`); | |
| const randomRange = Array(3) | |
| .fill() | |
| .map((_, i) => i + 999); | |
| WriteLine(`Three numbers from 999 => ${randomRange.join(",")}`); | |
| } |
Results
| // C# | |
| ==================== Range DEMO - Sequence Increased by Factor of two ==================== | |
| One to Ten => 1,2,3,4,5,6,7,8,9,10 | |
| Three numbers from 999 => 999,1000,1001 | |
| // JS | |
| ==================== Range DEMO - Some Generic Examples ==================== | |
| One to Ten => 1,2,3,4,5,6,7,8,9,10 | |
| Three numbers from 999 => 999,1000,1001 |
I used he same technique but utilizing an index property of map callback, which is where initialization of a range occurs (index starts from 0).
🔴 Closing Remark
If you have been following the series, you might have noticed that many JavaScript implementations uses map and reduce.
It's because many LINQ methods are simply a syntactic sugar over what "map" and "reduce" can do.
Please let me know should you find any errors or improvements I can make to the codes. The full source code and instructions on how to run them are on GitHub.