DEV Community

Srividya Mysore Venkatesh
Srividya Mysore Venkatesh

Posted on

Chapter 5: Higher-Order Functions.

Abstractions hide details and give us the ability to talk about problems at a higher (or more abstract) level.
It is a useful skill, in programming, to notice when you are working at too low a level of abstraction.

Plain functions, as we’ve seen them so far, are a good way to build abstractions. But sometimes they fall short.
This is structured a little like a for loop—it first describes the kind of loop and then provides a body. However, the body is now written as a function value, which is wrapped in the parentheses of the call to repeat. This is why it has to be closed with the closing brace and closing parenthesis.

Higher-Order Functions:
Functions that operate on other functions, either by taking them as arguments or by returning them, are called higher-order functions.
Higher-order functions allow us to abstract over actions, not just values. They come in several forms.
We can have functions that change other functions.
We can even write functions that provide new types of control flow.
There is a built-in array method, forEach, that provides something like a for/of loop as a higher-order function.

Script Data Set:
One area where higher-order functions shine is data processing.
This chapter will use a data set about scripts—writing systems such as Latin, Cyrillic, or Arabic.

The example data set contains some pieces of information about the 140 scripts defined in Unicode.Such an object tells us the name of the script, the Unicode ranges assigned to it, the direction in which it is written, the (approximate) origin time, whether it is still in use, and a link to more information.
The direction may be "ltr" for left to right, "rtl" for right to left (the way Arabic and Hebrew text are written), or "ttb" for top to bottom (as with Mongolian writing).

Filtering Arrays:
Filtering arrays just work like a seive. But just that The function to filter is pure. filter function, rather than deleting elements from the existing array, builds up a new array with only the elements that pass the test.It does noy modify the parent array.

Transforming With Map:
To Map an array means to have another same array but at another location.
The map method transforms an array by applying a function to all of its elements and building a new array from the returned values. The new array will have the same length as the input array, but its content will have been mapped to a new form by the function.

Summarising with Readuce:
Another common thing to do with arrays is to compute a single value from them. The higher-order operation that represents this pattern is called reduce (sometimes also called fold). It builds a value by repeatedly taking a single element from the array and combining it with the current value. When summing numbers, you’d start with the number zero and, for each element, add that to the sum.

String and Character Codes:
One use of the data set would be figuring out what script a piece of text is using.

JavaScript strings are encoded as a sequence of 16-bit numbers. These are called code units. A Unicode character code was initially supposed to fit within such a unit (which gives you a little over 65,000 characters). When it became clear that wasn’t going to be enough, many people balked at the need to use more memory per character. To address these concerns, UTF-16, the format used by JavaScript strings, was invented. It describes most common characters using a single 16-bit code unit but uses a pair of two such units for others.
But UTF-16 is a bad bad idea today.

Recognizing Text:
We have a characterScript function and a way to correctly loop over characters. The next step is to count the characters that belong to each script.

for/of) and a function that computes a group name for a given element. It returns an array of objects, each of which names a group and tells you the number of elements that were found in that group.

It uses another array method—findIndex. This method is somewhat like indexOf, but instead of looking for a specific value, it finds the first value for which the given function returns true. Like indexOf, it returns -1 when no such element is found.

Happy Reading!
(Yes this was a little flat read will make sure blog six is entertaining)

Top comments (0)