-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Blog #19
base: master
Are you sure you want to change the base?
Blog #19
Conversation
blog.md
Outdated
|
||
### 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
blog.md
Outdated
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 - 2
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.
def calculateFuels(masses: List[Int]): List[Int] = masses.map(fuel)
blog.md
Outdated
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's write the Mass
trait out here too
Added blog.md