![]() This means when the initial value is passed to the reducer along with another value x, the output is always x. The initial value needs to be an identity with respect to the reducer. A reducer takes two values-the current accumulation and the current array element-and returns the new accumulation. The binary operations add, mult and max are called reducers. There are three things regarding the fold that are worth noting. What we're going to do is implement the fold ourselves in JS, because why not? ![]() This function is one of the fundamentals of functional programming. ![]() There's a name for such a function, the Fold ( Wikipedia). We're currently repeating a lot of code, so if we could abstract it into a function, we'd have code that's much cleaner and more expressive. This process of collecting items clearly follows a pattern. A binary operation that combines the accumulation and the array item we're currently working with ( add, mult and max).An initial value for the accumulation (0, 1 and -Infinity).A place that holds the final result, commonly referred to as the accumulation or acc.They all involve some very similar parts: The above examples have a few things in common. In each of these examples, we took an array of things and performed some operation that collected those things into a single thing. Enter fullscreen mode Exit fullscreen mode
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |