From efd7afdc1019ec1d5b8de27dac8b0056208eb996 Mon Sep 17 00:00:00 2001 From: Dennis Cual Date: Tue, 13 Feb 2018 13:33:49 +0800 Subject: [PATCH] Add answer to Higher-order function and add initial Contribution guidelines file (#11) * add answer to Higher-order function questin * add initial contribution guidelines file --- CONTRIBUTING.md | 10 ++++++ README.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 000000000..6c0f8a9ec --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,10 @@ +Contributing +When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change. + +Please note we have a code of conduct, please follow it in all your interactions with the project. + +Pull Request Process +Ensure any install or build dependencies are removed before the end of the layer when doing a build. +Update the README.md with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters. +Increase the version numbers in any examples files and the README.md to the new version that this Pull Request would represent. The versioning scheme we use is SemVer. +You may merge the Pull Request in once you have the sign-off of two other developers, or if you do not have permission to do that, you may request the second reviewer to merge it for you. diff --git a/README.md b/README.md index 0bf20b0e6..39f573703 100644 --- a/README.md +++ b/README.md @@ -1397,7 +1397,90 @@ TODO ### What is the definition of a higher-order function? -TODO +A **higher order function** is a function that takes a function as an argument, or returns a function. Higher order function is in contrast to first order functions, which don’t take a function as an argument or return a function as output. Higher order function can help you to step up your JavaScript skills by making your code more declarative. The three most used higher-order function in javascript are `map, filter and reduce` of `Array` object. + +##### Map +Let say we have an array of names which we need to transform each element to uppercase string. + +`const names = ['irish', 'daisy', 'anna']`; + +The imperative way will be like: + +```js +const transformNamesToUppercase = names => { + const results = []; + for (let i=0; i names.map(name => name.toUpperCase()); +transformNamesToUppercase(names); // ['IRISH', 'DAISY', 'ANNA'] +``` +##### Filter +We want to filter all names which their initial character starts with **i**. + +The imperative way will be like: + +```js +const filterNames = names => { + const results = []; + for (let i=0; i names.filter(name => name.startsWith('i')); +filterNames(names); // ['IRISH'] +``` + +##### Reduce +Sum all the values of an array + +`const numbers = [1,2,3,4,5];` + +Imperative way: + +```js +const sumOfNumbers = numbers => { + let sum = 0; + for (let i=0; i numbers.reduce((total, number) => total+=number, 0); +sumOfNumbers(numbers) // 15 +``` + +Use **higher-order function** to make your code easy to reason about and improve the quality of your code. This became your code more **declarative** instead imperative, say **what you want done** not **how to do it**. + +###### References + +* https://medium.com/javascript-scene/higher-order-functions-composing-software-5365cf2cbe99 +* https://hackernoon.com/effective-functional-javascript-first-class-and-higher-order-functions-713fde8df50a + ### Can you give an example for destructuring an object or an array?