-
Notifications
You must be signed in to change notification settings - Fork 1k
Rewrite Currying tour as Multiple Parameter Lists #986
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
Conversation
cc: @SethTisue @jvican PS: The build failure is due to the stale link '/tour/currying.html' in files, other than the one in English. I will fix them asap. |
_tour/multiple-parameter-lists.md
Outdated
redirect_from: "/tutorials/tour/multiple-parameter-lists.html" | ||
--- | ||
|
||
Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments. |
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.
"This is formally known as currying." and then maybe a link to the currying wikipedia page? https://en.wikipedia.org/wiki/Currying
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.
I agree, but I would also suggest changing the title to "Multiple Parameter Lists (Currying)". That would make it easy for people to recognise sections according to specific names they have heard of, with regards to functional programming. One often hears about the formal names for features of the language.
In general, I think that titles should contain a pedagogical title part, such as "multiple parameter lists" and a part containing some sort of reference to the more formal name.
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.
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.
I see value in having the technical term in the title just for those that are already familiar with the term and want to see what the Scala tour says about it, or how Scala allows to encode currying in the language. 😄
_tour/multiple-parameter-lists.md
Outdated
def foldLeft[B](z: B)(op: (B, A) => B): B | ||
``` | ||
|
||
`foldLeft` applies a binary operator `op` to an initial value `z` and all elements of this traversable, going left to right. Here is an example of its usage: |
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.
I would mention here one of the primary reasons why currying is useful: to fix the parameter z
and reuse it in the call site several times. For example, when we're using the same parameter z
all the time, it's better to reuse the same function.
I'm trying to find a good example that illustrates this scenario, but cannot come up with a good one. Do you happen to have a good one in mind? 😄
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.
I think that being able to reuse the parameter is just one of the advantages here.
Another is the special syntax you can get, list.foldLeft(z) { (acc, value) => /* ... */ }
.
A bigger advantage is, though, to circumvent the limitation that scalac
doesn't use type information in one parameter for another parameter's inference.
With a def foldLeft[B](z: B, op: (B, A) => B)
, you can't write List(1, 2).foldLeft(0, _ + _)
. It complains about the first _
having a missing parameter type.
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.
A bigger advantage is, though, that scalac doesn't use type information in one parameter for another parameter's inference.
Yup, that's quite technical but worth being mentioned.
_tour/multiple-parameter-lists.md
Outdated
|
||
Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include: | ||
|
||
1. ##### Implicit parameters |
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.
Is this rendered correctly in markdown?
_tour/multiple-parameter-lists.md
Outdated
``` | ||
|
||
2. ##### Single functional parameter | ||
In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. |
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.
I think it would be good to elaborate more here and maybe add a good example, as I described in the previous comment.
Thanks for picking this up @sujithjay, it's appreciated. |
cc: @jvican @Adowrath @tfinneid @SethTisue |
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.
LGTM! Thanks 😄
I defer merging to @SethTisue |
---@sujithjay, as @jvican also states, I think the title should also contain the formal name of the language feature.--- Sorry, I see there is a change which was hard to find in this thread about the title, I only found the revision through one of the emails from the list. |
thanks, @sujithjay, for taking this on! |
followup PR to further clarify the relationship between multiple parameter lists and currying: #2490 |
replaces #732 . This is a work in progress; raising a PR to elicit comments.