01 Imperative vs declarative
Lesson goal Understand the difference between imperative code, which tells you how you do it, and declarative code, which tells you what the code does, and learn to rewrite a loop with filter and a predicate.
Imperative code tells you "how you do it". In other words, you have to dig into the code itself.
Declarative code tells you what your code does; it is more often modular, and you do not need to dig into the whole codebase. If changes are needed, you make them in a specific section of the code in a modular way.
// for example, get all numbers greater than 3 from the array
// Imperative code
// ========
let result = [] // the resulting array
const numbers = [1, 2, 3, 4, 5] // the source array
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > 3) {
result.push(numbers[i])
}
}
console.log(result) // 4, 5
// =========
// Declarative code
// =========
// isGt3 :: Number -> Boolean
const isGt3 = number => number > 3
const declarativeResult = numbers.filter(isGt3) // we don't dig into the implementation, we just read the name
console.log(declarativeResult) // 4, 5: the same result, but more declarative
// =========
// We can go further and raise the level of abstraction
// =========
// don't worry if it isn't clear right away, it will be later
// filter :: ((a -> Boolean), [a]) -> [a]
const filter = (predicat, arr) => arr.filter(predicat)
// filterBy :: (a -> Boolean) -> [a] -> [a]
const filterBy = (predicat) => (arr) => filter(predicat, arr)
// gt :: Number -> Number -> Boolean
const gt = pointer => number => number > pointer
// now we can create any predicate functions
// for filtering
// gt2 :: Number -> Boolean
const gt2 = gt(2) // predicate that keeps numbers greater than 2
const gt3 = gt(3) // predicate that keeps numbers greater than 3
// and now we can filter an array by any predicate
const arr = [1, 2, 3, 4, 5]
const more2 = filterBy (gt2) (arr) // numbers greater than 2
const more3 = filterBy (gt3) (arr) // numbers greater than 3
// we can also prepare the main filtering function for pipelines
// without calling it right away
// filterByGt2 :: [a] -> [a]
const filterByGt2 = filterBy (gt2) // waits for an array
// filterByGt2 (arr): the name already tells us it keeps numbers greater than 2
const resultGt2 = filterByGt2 (arr) // the final result: numbers greater than 2
// ======Key points
- Imperative code tells you how you do it: to understand it, you have to dig into the code itself.
- Declarative code tells you what it does. It is more often modular, and changes are made in a specific section of the code.
- filter with the isGt3 predicate replaces a loop with a condition and push: we don't dig into the implementation, we just read the name.
- The level of abstraction can be raised further: gt(2) creates a predicate, and filterBy(gt2) creates a filtering function that waits for an array. The name filterByGt2 already tells you what it does.
Exercises
Exercise 1
Rewrite the imperative code declaratively: get the words longer than four characters from the words array using filter and an isLonger4 predicate. The imperative version is already in the starter; both versions should print the same result.
Starter code
const words = ['map', 'filter', 'reduce', 'pipe']
// Imperative code
const imperativeResult = []
for (let i = 0; i < words.length; i++) {
if (words[i].length > 4) {
imperativeResult.push(words[i])
}
}
console.log(imperativeResult)
// Declarative code
// isLonger4 :: String -> Boolean
// your codeExpected output
[ "filter", "reduce" ] [ "filter", "reduce" ]
Show solution
const words = ['map', 'filter', 'reduce', 'pipe']
// Imperative code
const imperativeResult = []
for (let i = 0; i < words.length; i++) {
if (words[i].length > 4) {
imperativeResult.push(words[i])
}
}
console.log(imperativeResult)
// Declarative code
// isLonger4 :: String -> Boolean
const isLonger4 = word => word.length > 4
const declarativeResult = words.filter(isLonger4) // we read the name, not the implementation
console.log(declarativeResult) // the same resultExercise 2
Following the gt example from the lesson, write a divisibleBy function: it takes a divisor and returns a predicate "the number is divisible by it with no remainder". Use it with filterBy to get the filterByDivisibleBy3 and filterByDivisibleBy5 functions, and apply them to the numbers array.
Starter code
// filterBy :: (a -> Boolean) -> [a] -> [a]
const filterBy = (predicate) => (arr) => arr.filter(predicate)
const numbers = [3, 5, 9, 10, 15]
// divisibleBy :: Number -> Number -> Boolean
// your code
// filterByDivisibleBy3 :: [Number] -> [Number]
// filterByDivisibleBy5 :: [Number] -> [Number]
// your code
console.log(filterByDivisibleBy3(numbers))
console.log(filterByDivisibleBy5(numbers))Expected output
[ 3, 9, 15 ] [ 5, 10, 15 ]
Show solution
// filterBy :: (a -> Boolean) -> [a] -> [a]
const filterBy = (predicate) => (arr) => arr.filter(predicate)
const numbers = [3, 5, 9, 10, 15]
// divisibleBy :: Number -> Number -> Boolean
const divisibleBy = (divisor) => (number) => number % divisor === 0
// filterByDivisibleBy3 :: [Number] -> [Number]
// filterByDivisibleBy5 :: [Number] -> [Number]
const filterByDivisibleBy3 = filterBy(divisibleBy(3)) // waits for an array
const filterByDivisibleBy5 = filterBy(divisibleBy(5))
console.log(filterByDivisibleBy3(numbers))
console.log(filterByDivisibleBy5(numbers))Playground
Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.