DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

param-19
param-19

Posted on

Higher Order Functions

Abstraction

Abstraction is the process of hiding information from the users. They give us the ability to understand and read code at a higher level, while hiding the details, to give us an overview.

  • There exists a filter method which builds a new array that contains the results that pass given test.
console.log(filter(SCRIPTS, script => script.living));
// β†’ [{name: "Adlam", ...}, ...]
Enter fullscreen mode Exit fullscreen mode

Maps

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.

function map(array, transform) {
    let mapped = [];
    for (let element of array) {
        mapped.push(transform(element));
    }
    return mapped;
}
let rtlScripts = SCRIPTS.filter(s => s.direction == "rtl");
console.log(map(rtlScripts, s => s.name));
// β†’ ["Adlam", "Arabic", "Imperial Aramaic", ...]
Enter fullscreen mode Exit fullscreen mode
  • The reduce method is another function that takes in the input, and a condition based on which it combines elements. You can easily understand how reduce works based on the given example.
console.log([1, 2, 3, 4].reduce((a, b) => a + b));
// β†’ 10
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!