This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Add(values) { | |
| let sum = 0 | |
| for (var value of values) { | |
| sum += value | |
| } | |
| return sum | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Add(values) { | |
| let sum = 0 | |
| values.forEach(val => sum += val) | |
| return sum | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Add(values) { | |
| return values.reduce((sum, val) => sum += val) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqAggregateAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) => values.Aggregate((sum, val) => sum += val); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqSumAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) => values.Sum(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let addInts ints = | |
| let rec loop list acc = | |
| match list with | |
| | head :: tail -> loop tail (acc + head) | |
| | [] -> acc | |
| loop ints 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class ArrayHeadTailAdder : IIntegerAdder | |
| { | |
| //This will fail with large arrays | |
| public int Add(int[] values) | |
| { | |
| var (head, tail) = GetHeadAndTail(values); | |
| return AddImplentation(head, tail); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal class LinqHeadTailAdder : IIntegerAdder | |
| { | |
| private const int maxSupportedLength = 10000; | |
| public int Add(int[] values) | |
| { | |
| if (values.Length > maxSupportedLength) | |
| { | |
| throw new NotSupportedException($"Max supported length is {maxSupportedLength}"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal unsafe class PointerHeadTailAdder : IIntegerAdder | |
| { | |
| public int Add(int[] values) | |
| { | |
| fixed (int* head = values) | |
| { | |
| return AddImplementation(head, values.Length); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| internal static class ExtensionMethods | |
| { | |
| public static bool TryDequeue<T>(this Queue<T> queue, out T result) | |
| { | |
| result = default; | |
| if (queue == null || !queue.Any()) | |
| { | |
| return false; | |
| } | |
| result = queue.Dequeue(); |