Project Euler (FP) - Problem 6
The Problem
The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640
.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
A Solution
Here’s the compose function introduced in problem 3 that sends the output of each function to the input of the next function. The difference is that this time it’s implemented with the reduceRight(..)
array method making it capable of accepting more than one argument for the first function in the composition.
This function called curry which deals with partially applying arguments to functions like in problem 5. I guess is the first I’ve addressed the idea of arity in this blog. Arity is a fancy word for the number of arguments that a function takes. You can find the arity of a function fn
through its length property fn.length
Partially applying arguments to functions can be very useful if you know part of the info but not all of it at any point in your code. In addition the compose
function relies on all of the functions being unary, that is having an arity of one.
This unboundMethod
is a tricksy way of ‘unbinding’ a method from an object. In essence, it makes a function that takes the object as an argument but then uses that argument’s method in the end. In this case I wanted to use the native array methods reduce
and map
in that way. The second argument is the arity of the method which the curry method can not know without us providing it.
Here’s a little helper function to create an array of natural numbers within certain bounds.
So here’s the payoff. The preceding functions are tough to read at first, but they are all very testable utilities that support concise and readable code below. Indeed, there are libraries like Ramda that implement those utilities.
Conclusion
compose
can make a concise, readable function pipeline.
curry
can deal with arity to create unary functions that are composable.
unboundMethod
is a fun way of turning a method into a standalone function.