In this exercise there are several small problems that can be solved using map
, filter
, and reduce
.
The challenges should be really easy to solve – if you know the language. The aim is to get hands on with doing regular, routine workflows, rather than learning syntax and solving maths problems.
I’m going to specify the problems as a quasi-Gherkin syntax, intentionally language agnostic. I am aware these are not brilliant examples of Gherkin. Perhaps I should have said Kwasi Gherkin. A UK economy joke there.
For specific implementations, or to follow along, see either the specific language pages, or Github.
Easy Reduce
Scenario: Using reduce to sum numbers
Given an array of zero elements
When reduced
Then the outcome should be zero
Given an array of one number
When reduced
Then the outcome should be that same number
Given an array of multiple numbers
When reduced
Then the outcome should be the sum of all numbers
Scenario: Using reduce to concatenate strings
Given an array of zero elements
When reduced
Then the outcome should be an empty string
Given an array of one string
When reduced
Then the outcome should be that same string
Given an array of multiple strings
When reduced
Then the outcome should be a concatenation of all strings
Scenario: Using reduce to find nothing but the truth
Given an array of zero elements
When reduced
Then the outcome should be false
Given an array of one boolean
When reduced
Then the outcome should be that same boolean
Given an array of multiple booleans
When all booleans are truthy
Then the outcome should be true
Given an array of multiple booleans
When all booleans are falsy
Then the outcome should be false
Given an array of multiple booleans
When any value is falsy
Then the outcome should be false
Code language: PHP (php)
Easy Filter
Scenario: Using filter to find even numbers
Given an array of zero elements
When filtered
Then the outcome should be an empty array
Given an array of one odd number
When filtered
Then the outcome should be an empty array
Given an array of one even number
When filtered
Then the outcome should be equivalent to the original array
Given an array of multiple numbers
When filtered
Then the outcome should be an array of only the even numbers
Scenario: Using filter to remove empty strings, and any string containing the letter A
Given an array of zero elements
When filtered
Then the outcome should be an empty array
Given an array of one empty string
When filtered
Then the outcome should be an empty array
Given an array of one string with the value "a"
When filtered
Then the outcome should be an empty array
Given an array of one string with the value "b"
When filtered
Then the outcome should be an array containing "b"
Given an array of multiple strings
When filtered
Then the outcome should be an array of any non empty elements that do not contain the letter A
Scenario: Using filter to find nothing but the truth
Given an array of zero elements
When filtered
Then the outcome should be an empty array
Given an array of one boolean with the value "false"
When filtered
Then the outcome should be an empty array
Given an array of one boolean with the value "true"
When filtered
Then the outcome should be an array containing "true"
Given an array of multiple boolean values where all are "false"
When filtered
Then the outcome should be an empty array
Given an array of multiple boolean values where all are "true"
When filtered
Then the outcome should be equivalent to the original array
Given an array of multiple boolean values where there are both "true" and "false" values
When filtered
Then the outcome should be only the truthy values
Code language: PHP (php)
Easy Map
Scenario: Using map to square numbers
Given an array of zero elements
When mapped
Then the outcome should be an empty array
Given an array of one number
When mapped
Then the outcome should be an array with the original number squared
Given an array of multiple numbers
When mapped
Then the outcome should be an array with each original number squared
Scenario: Using map to convert strings to title case
Given an array of zero elements
When mapped
Then the outcome should be an empty array
Given an array of one empty string
When mapped
Then the outcome should be an empty array
Given an array of one string with the value "a"
When mapped
Then the outcome should be an array containing "A"
Given an array of one string with the value "XYZ AbC"
When mapped
Then the outcome should be an array containing "Xyz abc"
Given an array of multiple strings
When mapped
Then the outcome should be an array of where all elements are in "Title case"
Scenario: Using map to stringify boolean values
Given an array of zero elements
When mapped
Then the outcome should be an empty array
Given an array of one boolean with the value "false"
When mapped
Then the outcome should be an array containing the string "false"
Given an array of one boolean with the value "true"
When mapped
Then the outcome should be an array containing the string "true"
Given an array of multiple boolean values where there are both "true" and "false" values
When mapped
Then the outcome should be an array where all boolean values have been stringified
Code language: PHP (php)
Predicted challenges here may be:
- The zero element array, and satisfying the type system that the given input is the same type that the function expects
- Strings vs Chars in some languages
- Logic for tracking the boolean outcomes