Hiding repetition with helper functions
We can hide the repetition in a function’s parameters in a higher-order function.
Read full articleTo make sure all numbers in an array are within a given range, we can “clamp” them. Values below the minimum become min
, values above the maximum become max
.
This can be useful when working with data coming from home automation sensors. By clamping what they return, unrealistic spikes in temperature could be moved into a realistic range.
// takes a minimum and maximum value, returns a function that clamps a value
const clamp = (min, max) => value => Math.max(Math.min(value, max), min)
// we can call this function on individual values
clamp(2, 3)(1) // ⇒ 2 (because 1 is smaller than the minimum value of 2)
clamp(2, 3)(2) // ⇒ 2
clamp(2, 3)(3) // ⇒ 3
clamp(2, 3)(4) // ⇒ 3 (because 4 is greater than the maximum value of 3)
// because the function is curried, we can pass it to .map() like this
[1, 16, 9, 0, -2, 8, 14].map(clamp(0, 10)) // ⇒ [1, 10, 9, 0, 0, 8, 10]
// takes a minimum and maximum value, returns a function that clamps a value
const clamp = (min, max) => value => Math.max(Math.min(value, max), min)
// we can call this function on individual values
clamp(2, 3)(1) // ⇒ 2 (because 1 is smaller than the minimum value of 2)
clamp(2, 3)(2) // ⇒ 2
clamp(2, 3)(3) // ⇒ 3
clamp(2, 3)(4) // ⇒ 3 (because 4 is greater than the maximum value of 3)
// because the function is curried, we can pass it to .map() like this
[1, 16, 9, 0, -2, 8, 14].map(clamp(0, 10)) // ⇒ [1, 10, 9, 0, 0, 8, 10]
// takes a minimum and maximum value, returns a function that clamps a value
const clamp = (min, max) => value => Math.max(Math.min(value, max), min)
// we can call this function on individual values
clamp(2, 3)(1) // ⇒ 2 (because 1 is smaller than the minimum value of 2)
clamp(2, 3)(2) // ⇒ 2
clamp(2, 3)(3) // ⇒ 3
clamp(2, 3)(4) // ⇒ 3 (because 4 is greater than the maximum value of 3)
// because the function is curried, we can pass it to .map() like this
[1, 16, 9, 0, -2, 8, 14].map(clamp(0, 10)) // ⇒ [1, 10, 9, 0, 0, 8, 10]
// takes a minimum and maximum value, returns a function that clamps a value
const clamp = (min, max) => value => Math.max(Math.min(value, max), min)
// we can call this function on individual values
clamp(2, 3)(1) // ⇒ 2 (because 1 is smaller than the minimum value of 2)
clamp(2, 3)(2) // ⇒ 2
clamp(2, 3)(3) // ⇒ 3
clamp(2, 3)(4) // ⇒ 3 (because 4 is greater than the maximum value of 3)
// because the function is curried, we can pass it to .map() like this
[1, 16, 9, 0, -2, 8, 14].map(clamp(0, 10)) // ⇒ [1, 10, 9, 0, 0, 8, 10]
We can hide the repetition in a function’s parameters in a higher-order function.
Read full articleTo filter all falsy values from arrays, all we need to do is pass the Boolean constructor to the filter function.
Read full articleThe ternary operator isn’t limited to assigning values based on a condition. It also lets us switch between two near identical function calls.
Read full articleTo find the largest value in an array of numbers, we can spread that array into Math.max() instead of manually iterating over it.
Read full articlePutting complex logic into a function increases code readability. By giving that function a descriptive name, it can explain what is happening in it.
Read full articleWith the Fisher-Yates algorithm, we can shuffle an array with true randomness. The likelihood of two shuffles giving the same result is minimal.
Read full article