Blog#19
Conversation
|
|
||
| ### Functional Solution | ||
|
|
||
| Even though the first part of Day1 problem can be easily solved in one line of code, we purposely break it down into multiple small functions so that we can demonstrate how to apply typeclass to the problem later. We first write a fuel() function that takes in a mass of the module, divides by three and subtracts two to find the fuel required to launch a module of a given mass. In the problem, your puzzle input is a long list of all the modules on your spacecraft. To find the fuel required for each module, you could apply the fuel() function to each module of the long list by using map function. |
There was a problem hiding this comment.
What do you think about having the function names marked up as code? E.g.
We first write a fuel function... you could apply the fuel function to each module of the long list by using map function.
|
|
||
| Even though the first part of Day1 problem can be easily solved in one line of code, we purposely break it down into multiple small functions so that we can demonstrate how to apply typeclass to the problem later. We first write a fuel() function that takes in a mass of the module, divides by three and subtracts two to find the fuel required to launch a module of a given mass. In the problem, your puzzle input is a long list of all the modules on your spacecraft. To find the fuel required for each module, you could apply the fuel() function to each module of the long list by using map function. | ||
|
|
||
| This is what we’ve done by writing calculateFuels() function. Next, to sum up the list of fuels, we make use of the sum method available to Iterable like List in our case, and write a sumFuels() function. Finally, we put all of these small functions into sumOfFuel() function and solve our Part 1’s problem. |
There was a problem hiding this comment.
What do you think about interspersing the actual code snippets with the explanation?
e.g.
We first write a fuel() function that takes in a mass of the module, divides by three and subtracts two to find the fuel required to launch a module of a given mass.
def fuel(mass: Int): Int = mass / 3 - 2In the problem, your puzzle input is a long list of all the modules on your spacecraft. To find the fuel required for each module, you could apply the fuel() function to each module of the long list by using map function. This is what we’ve done by writing calculateFuels() function.
def calculateFuels(masses: List[Int]): List[Int] = masses.map(fuel)|
|
||
| What the error message is saying is the compiler cannot find `/`, division operation under type `A`. Previously, when the mass parameter was a concrete type `Int`, the function compiled because `/` is defined under type `Int`. Actually, it’s not only value `/` undefined, `-` (minus), `3` and `2` are also undefined under type A. | ||
|
|
||
| To make the generic `fuel[A]` function compile, we need to define all of them and we do that in an interface, `trait Mass[A]`. |
There was a problem hiding this comment.
Let's write the Mass trait out here too
Added blog.md