first 8 minutes of video 1 complete time to ask @rtfeldman if this is going to be useful...
This commit is contained in:
@@ -213,3 +213,103 @@ So in `JavaScript` you would say "_quantity triple equals 1_" (_the condition_)
|
|||||||
in `elm` you say: "_if quantity double-equals 1 then here's what
|
in `elm` you say: "_if quantity double-equals 1 then here's what
|
||||||
you do if that's `True` and here's what you do if that's `False`_"
|
you do if that's `True` and here's what you do if that's `False`_"
|
||||||
So, _same_ basic idea.
|
So, _same_ basic idea.
|
||||||
|
|
||||||
|
`Day 1 Hour 1 - h1.mp4 @ 05:11`
|
||||||
|
Now the _relevant_ part is that
|
||||||
|
this entire chunk of code is an expression;
|
||||||
|
it evaluates to a single value which means it's "_portable_".
|
||||||
|
You can pick it up and just drop it somewhere else
|
||||||
|
and just say "`foo` equals `if quantity == 1 then singular else plural`..."
|
||||||
|
You can do that anywhere you would have any single value.
|
||||||
|
You can just drop in this entire `if` expression,
|
||||||
|
perhaps with parentheses around it to disambiguate.
|
||||||
|
And it's exactly the same thing with the ternary
|
||||||
|
and so you need this both `if` and `else`
|
||||||
|
because it needs to be _clear_ what value is going to be substituted in there
|
||||||
|
if you do drop this in place of any other value.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
So here's how function _calling_ works
|
||||||
|
this was the code we had in "try `elm`"
|
||||||
|
slightly below the definition of `pluralize`
|
||||||
|
_calling_ a function is just done with
|
||||||
|
[`whitespace`](https://en.wikipedia.org/wiki/Whitespace_character)
|
||||||
|
so we are going to say `pluralize` followed by some `whitespace`
|
||||||
|
followed by its' `arguments`.
|
||||||
|
|
||||||
|
Also note that there are no commas between the arguments
|
||||||
|
so it's just `pluralize "leaf" "leaves" 1`
|
||||||
|
the parentheses here in _this_ case are to disambiguate
|
||||||
|
between two function calls that we are making
|
||||||
|
so this right here, that's all one function call:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
and this is actually a _second_ function call,
|
||||||
|
we're calling a function called `text`:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
and what we're _passing_ to `text` is the _result_ of
|
||||||
|
calling `pluralize` passing `"leaf"` `"leaves"` and `1`
|
||||||
|
|
||||||
|
so this is calling a function (_`pluralize`_) passing 3 arguments:
|
||||||
|

|
||||||
|
|
||||||
|
and this is calling another function (_`text`_) passing 1 argument:
|
||||||
|

|
||||||
|
|
||||||
|
the one argument we are passing is the result of the other function.
|
||||||
|
note that this is a case of the parentheses serving to disambiguate.
|
||||||
|
if we did not have the parentheses here
|
||||||
|
this would be indistinguishable from calling `text` passing in _four_ arguments.
|
||||||
|
the first is the function `pluralize`
|
||||||
|
and then `"leaf"` `"leaves"` and `1`
|
||||||
|
is the other three.
|
||||||
|
So parentheses for disambiguation
|
||||||
|
but otherwise whitespace is just what you do for calling functions.
|
||||||
|
|
||||||
|
Finally we have at the top
|
||||||
|
we have this `import Html` exposing `..`
|
||||||
|

|
||||||
|
we'll get into **modules** a little more later,
|
||||||
|
but it's basically just saying:
|
||||||
|
"_Hey, bring in the `Html` module and expose all of the stuff in there._"
|
||||||
|
such as this `text` function and we will make use of those.
|
||||||
|
|
||||||
|
`Day 1 Hour 1 - h1.mp4 @ 07:15`
|
||||||
|
OK, so "***why bother?***"
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
I just taught you a _bunch_ of new syntax,
|
||||||
|
and showed you how you can write the same thing in `JavaScript`
|
||||||
|
***why bother*** learning a _whole different language_...?
|
||||||
|
is it just learning _syntax_ for _nothing_?
|
||||||
|
|
||||||
|
What can you do in `elm`, like what does it _get_ you that `Babel` does _not_?
|
||||||
|
That's a _great_ question.
|
||||||
|
So, let's say we've got this implementation
|
||||||
|
and we `build` it and we're going to use it in `production`
|
||||||
|
and _hopefully_ we write tests for it,
|
||||||
|
_hopefully_ those tests do a good job covering the different cases.
|
||||||
|
But let's say we make a _mistake_ in our implementation here:
|
||||||
|
|
||||||
|
So we're like:
|
||||||
|
`pluralize "leaf" "leaves" 1`
|
||||||
|

|
||||||
|
and it gives us "leaf" down here (_refering to the `console.log` output at the bottom of the Babel page_)
|
||||||
|
|
||||||
|
If we call `pluralize` passing `"leaf"` `"leaves"` and `3`
|
||||||
|

|
||||||
|
It gives us "leaves".
|
||||||
|
Great! that's what we _expect_!
|
||||||
|
Any number over 1 should give us the `plural` form.
|
||||||
|
|
||||||
|
And then let's say,
|
||||||
|
when we implemented this we _accidentally_ made a typo here:
|
||||||
|
|
||||||
|
Instead of `singular` we said `singula` ...
|
||||||
|
|
||||||
|
Ok, so, as we can see, this code _still works_!
|
||||||
|
|||||||
Reference in New Issue
Block a user