Today, we learned about Lists and Partial Application. Lists are similar to Javascript arrays except that they cannot contain elements of the same type. It is defined similarly as Javascript arrays with square brackets []
. Boring stuff.
The interesting thing we learned today was Partial Application. A partially applied function is a function called with some of its arguments omitted. This is a fairly standard issue in Javascript, PHP, and I imagine other programming languages. What I find interesting is how Elm deals with the problem as a functional programming language.
The difficulty in learning Functional Programming as say, a Javascript or PHP developer, is the conflict between your mental model of different concepts and how they are actually applied in FP.
In Javascript, when an argument is omitted, the function uses a default argument in its place. The default argument is undefined
[unless the developer specifies one] and the function executes with this default argument. Boring stuff. For example:
function pluralize(singular, plural, quantity){
// ...
}
pluralize('boy', 'boys') // the third argument here will be `undefined`.
function pluralize(singular, plural, quantity = 1){
// ...
}
pluralize('boy', 'boys') // the value 1 will be passed to pluralize as the third argument.
In Elm, the concept is entirely different. If you omit an argument, the function will be partially executed and another function will be returned to you that takes the missing argument and completes the evaluation. Let's see the same example in Elm:
pluralize singular plural quantity =
if quantity === 1 then
singular
else
plural
pluralize "boy" "boys" -- will return an anonymous function that I will express as `f` that takes a number and finishes the evaluation of pluralize:
f quantity = pluralize "boy" "boys" quantity.
This is so interesting and different from what I am used to I have written various versions of this to make it stick.
Another example I adapted from the the intro to elm to make it sink in:
Elm is great. Pass it on!
Top comments (0)