This is the fifth blog in the series of twenty-one blogs. These blogs are to summarise my learning of the book Eloquent JS.
Loved this quote from the book "There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies."
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.
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 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.
But UTF-16 is a bad bad idea today.
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.
(Yes this was a little flat read will make sure blog six is entertaining)