Function Composition (functional programming)

ref – http://chineseruleof8.com/code/index.php/2019/01/03/functions-are-first-class-objects-js/

function vs procedure

Procedure – collection of functionalities.
May or may not have input, or return values, and have multiple tasks.

Functions – Mathematical functions.

  • Have input
  • Return value
  • Simplified to a single task
  • Easy for reuse

Step 1

We first have a string literal.
We have a function called prepareString.

And by scope, it reference the str and trims it for str1.
Then str1 replaces it with regex for str2.
Finally str2 upper cases everything for str3.
str3 then splits into an array.

Finally we loop through this array and try to find A, AN, or THE. If its found, delete it using splice.

Step 2 – separate them into one task functions

becomes

becomes

becomes

becomes

becomes

Using noArticles:

becomes

Step 3

Then we need to change these functions into arrow functions.

Step 4 – Optionally call them together

pipe

The pipe function goes through each function and applies the accumulator string to each function..starting with the initiator x.

So if x is ‘word’, and the first function is !${param}, this will return ‘!word’.
Then the 2nd function is *${param}, then we’d get ‘*!word’.

Basically, it would apply the result of the current function to the param of the next function.

Notice our spread operators in the parameter. Its taking all the params together and bringing them together as the array fns.

Our output would be:

$word
!$word
(!$word)
*(!$word)
*(!$word)–>
<--*(!$word)-->

We can also start from the right function, (by using reduceRight) so now our output would like this:

<--word <--word-->
*<--word-->
(*<--word-->)
!(*<--word-->)
$!(*<--word-->)

Now, going back to our work, we would stick each function to process on the string. Then use the returned string as the input for the next function. Literally, the functions put together act as a pipe for strings to go through and be processed.

Function Composition 2

Given…

and situation is:

1) Convert each statement to a function that can accept and act on any array.

2) Compose a function that will remove both zero or lower scores and scores over 100. Test it using the scores array.

3) Compose a function that will do all the modifications to an array. Test it using the scores array.

4) Create a function that will accept an array and return the average. Use the function that sums scores and the function that counts scores or the length property.

5) Compose a function that will do all the modifications on an array and return an average.

Arity

So we still have our pipe function. Remember that it takes in an array of functions, and an initial string x. (Henry)

It goes through the first function and gives ‘Henry’ as the first parameter. The return value of the 1st function (user object) is then inserted into the 2nd function.

The return value of the 2nd function is then inserted into the 3rd function, so on and so forth.

users array is the data that will be bind to getUsers function.

As you can see we a function like so:

We need to change it using bind so that we can pass in users as the data, and we get a reference to it all. That way, users is matched up to param arr.

We get partGetUser, and then when we execute it inside pipe, we can pass in ‘Henry’ to param name of getUser.

storeUser is just a standard function.

we have our cloneObj that takes an object and clones it using JSON. We leave it as is.

Then we have updateScore:

we need to change it like this:

updateTries is used as is.

We put updateUser, cloneObj, partUpdateScore30, and updateTries into our pipe utility function.
We get the result back when ‘Henry’ is inserted into the beginning of the pipe.

Arity full code