Skip to main content

Posts

Showing posts from February, 2024

5. 7 Summarizing with reduce

 Summarizing with reduce   Understanding Reduce Purpose of Reduce : reduce is a higher-order function used to compute a single value from an array. It's instrumental in operations like summing up numbers or finding an item in an array that meets a specific criterion (e.g., the script with the most characters). Operation : It works by repeatedly taking an element from the array and combining it with a current value until all elements have been processed. This process is akin to folding or reducing the array into a single value. 🎨: Think of reduce like making juice from oranges. You start with a bunch of oranges (the array), and then you squeeze them one by one into a jug (the single value). Your hands (the combining function) do the squeezing, and you might already have some juice in the jug to start with (the start value). 👩‍🎓: The Mechanics of reduce - The reduce function takes three arguments: the array to reduce, a combining function, and a starting value. The combin...

5. 7 Summarizing with reduce

 Summarizing with reduce   Understanding Reduce Purpose of Reduce : reduce is a higher-order function used to compute a single value from an array. It's instrumental in operations like summing up numbers or finding an item in an array that meets a specific criterion (e.g., the script with the most characters). Operation : It works by repeatedly taking an element from the array and combining it with a current value until all elements have been processed. This process is akin to folding or reducing the array into a single value. 🎨: Think of reduce like making juice from oranges. You start with a bunch of oranges (the array), and then you squeeze them one by one into a jug (the single value). Your hands (the combining function) do the squeezing, and you might already have some juice in the jug to start with (the start value). 👩‍🎓: The Mechanics of reduce - The reduce function takes three arguments: the array to reduce, a combining function, and a starting value. The combin...

5. 6 Transforming with map

Transforming with map   This paragraph  describes a concept called "mapping" in the context of array operations in JavaScript. This concept is fundamental for transforming arrays - taking each element in an array and applying a function to it to produce a new element in a new array. Let's break down the explanation and the provided code for a detailed understanding. Understanding the Concept of Mapping Mapping is a process where you take an input array and apply a transformation function to each of its elements. The result is a new array where each element is the transformed version of the corresponding element in the input array. This process does not modify the original array, making the map function a pure function. Code Breakdown 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(rtl...

5. 5 Filtering arrays

 Filtering arrays   ⚙️:Let's break it down step by step: Introduction to Data Processing with Higher-order Functions : The text begins by highlighting the usefulness of higher-order functions in data processing. Data Set about Scripts : It mentions the usage of a data set related to scripts, such as Latin, Cyrillic, or Arabic writing systems. The Unicode system assigns a unique number to each character in written language, and most characters are associated with a specific script. Unicode standardizes 140 different scripts, with 81 still in use and 59 historic. The author expresses appreciation for the diversity of writing systems. Example Data Set and SCRIPTS Binding : An example data set called SCRIPTS is introduced, containing information about the 140 scripts defined in Unicode. Each script is described by an object containing various properties like name, Unicode ranges, direction, origin time, living status, and a link for more information. Filtering Arrays : The text pr...

5. 4 Script data set

 Script data set The provided information describes a data set about scripts, including details such as the name of the script, Unicode ranges assigned to it, writing direction, origin time, current usage status, and a link for more information. Each script is represented by an object in the data set, with the following properties: Name : The name of the script. Ranges : An array of Unicode character ranges assigned to the script. Each range is represented by a two-element array containing a lower bound (inclusive) and an upper bound (non-inclusive). Direction : The direction in which the script is written, represented as "ltr" for left to right, "rtl" for right to left, or "ttb" for top to bottom. Year : The (approximate) origin time of the script. Living : A boolean indicating whether the script is still in use. Link : A link to more information about the script.   ⚙️: The paragraph introduces the concept of using higher-order functions for data processi...

5.3 Higher-order functions

 5.3 Higher-order functions     This paragraph  introduces the concept of higher-order functions in programming, specifically within the context of JavaScript. Higher-order functions are a powerful aspect of functional programming that allow you to write more abstract, concise, and reusable code . Let's break down the examples provided to clarify their meanings and uses.     Higher-order Functions Overview Definition : Functions that take other functions as arguments or return them as results are called higher-order functions. This capability is not unique or unusual in programming, especially in languages like JavaScript where functions are treated as first-class citizens, meaning they can be passed around just like other values (numbers, strings, etc.). Examples Explained 1. Creating New Functions function greaterThan(n) { return m => m > n; } let greaterThan10 = greaterThan(10); console.log(greaterThan10(11)); // → true greaterThan(n)...

5. 2 Abstracting repetition

  Abstracting repetition   ⚙️: The transition from a simple for loop to a versatile repeat function encapsulates the essence of "Abstracting Repetition" in programming. This evolution illustrates a powerful approach to abstraction, enabling programmers to not only simplify repetitive tasks but also to inject customizable behavior into their loops. 👩‍🎓: The repeat function showcases a fundamental principle of functional programming: higher-order functions. These are functions that can take other functions as arguments or return them as results. By abstracting the repetition mechanism into a higher-order function, you allow for a wide range of operations to be performed during each iteration of the loop, far beyond just logging to the console. This pattern increases the reusability and flexibility of your code. 🎨: Think of the repeat function as your personal programming assistant, capable of performing any task you delegate, not just the mundane act of counting. It'...