Continuing from the last thread on how to solve problems with a more functional style
This thread will be quite a bit different, because it's going to focus on a particular FP pattern (algebra).
To start:
Let's start with a simple challenge to create a function that can generate output from the composition of 2 arrays:
Array of single arity functions (and/or closures) from type Integer to type Integer:
Note:
Array of Integers:
Output:

Bonus points:
If your function can supports a generic transformation from any type to any type.
Extra bonus points:
If you can correctly identify this pattern.
Part 2:
Demonstrate how this pattern can be used to streamline processes like data validation, parsing, etc.
Bonus Points:
Demonstrate how using this pattern can be tweaked to capture all the validation errors as opposed to breaking out on the first error encountered.
Extra Bonus Points:
Demonstrate how this pattern can be used to multithread asynchronous processes.
Pattern validation rules (optional for this challenge)
As with most mathematically originated algebras; here are the rules that affirm correct behaviour.

Note:
In this example we are applying a functional pattern to an Array, however this pattern works equally well with any container object, I.e. an object that wraps some data, for example:
Some languages are more difficult than others when extending existing container types, for example: Java
Go ahead and submit your solutions / answers for any or all of the parts you like. Ask away for any further clarity.
Good luck.
After a week I'll again publish a detailed write up on this, to both explain the pattern, and to show how it works for not only Arrays but also the Optional, Result and Future types. Considering the full scope of this challenge I will only be preparing solutions to this in a maximum of 2 languages (let me know if you have a preference).
This thread will be quite a bit different, because it's going to focus on a particular FP pattern (algebra).
To start:
Let's start with a simple challenge to create a function that can generate output from the composition of 2 arrays:
Array of single arity functions (and/or closures) from type Integer to type Integer:
PHP:
[{x => 2 * x - 1}, {x => 2 * x}] // odd and even numbers
- Functions can be declared separately and then just referenced in the array.
Array of Integers:
PHP:
[1, 2, 3, 4, 5]
Output:
PHP:
[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
Bonus points:
If your function can supports a generic transformation from any type to any type.
Extra bonus points:
If you can correctly identify this pattern.
Part 2:
Demonstrate how this pattern can be used to streamline processes like data validation, parsing, etc.
Bonus Points:
Demonstrate how using this pattern can be tweaked to capture all the validation errors as opposed to breaking out on the first error encountered.
Extra Bonus Points:
Demonstrate how this pattern can be used to multithread asynchronous processes.
Pattern validation rules (optional for this challenge)
As with most mathematically originated algebras; here are the rules that affirm correct behaviour.
Note:
In this example we are applying a functional pattern to an Array, however this pattern works equally well with any container object, I.e. an object that wraps some data, for example:
- Optional
- Result
- Either
- LinkedList
- Trie
- Tuples
- Future
- Promise
- Etc.
Some languages are more difficult than others when extending existing container types, for example: Java
- which sadly still doesn't provide a simple way to add extra methods and/or operators without a lot of inheritance boilerplate.
- or alternatively the added complexity of something like Java 8's Stream class e.g. to lift an existing container into a new container with the added methods using type erasure.
- This pattern isn't well formulated with standalone / static functions due bracketing; it preferably needs something like method extension, operator overloading and / or infix functions.
- Languages like Python, Kotlin, Scala, Swift, Ruby, ... are far easier to extend.
Go ahead and submit your solutions / answers for any or all of the parts you like. Ask away for any further clarity.
Good luck.
After a week I'll again publish a detailed write up on this, to both explain the pattern, and to show how it works for not only Arrays but also the Optional, Result and Future types. Considering the full scope of this challenge I will only be preparing solutions to this in a maximum of 2 languages (let me know if you have a preference).
Last edited: