-
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
Changes from 5 commits
16d98b1
d723116
2d872a0
bf5e3ba
f66fa7d
327241a
b5fb699
77deb8b
94cdb4e
24ba9b5
00c5309
e6d87ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
layout: tour | ||
title: Multiple Parameter Lists | ||
|
||
discourse: true | ||
|
||
partof: scala-tour | ||
|
||
num: 10 | ||
next-page: case-classes | ||
previous-page: nested-functions | ||
|
||
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. | ||
|
||
Here is an example, defined in [Traversable](/overviews/collections/trait-traversable.html) trait from Scala collections: | ||
|
||
``` | ||
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 commentThe 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 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 commentThe 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. A bigger advantage is, though, to circumvent the limitation that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yup, that's quite technical but worth being mentioned. |
||
|
||
```tut | ||
val numbers = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | ||
val res = numbers.foldLeft(0)((m: Int, n: Int) => m + n) | ||
print(res) // 55 | ||
``` | ||
|
||
Starting with an initial value of 0, `foldLeft` applies the function `(m: Int, n: Int) => m + n` to each element in the List and the previous accumulated value. | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Is this rendered correctly in markdown? |
||
To specify certain parameters in a parameter list as `implicit`, multiple parameter lists should be used. An example of this is: | ||
|
||
``` | ||
def execute(arg: Int)(implicit ec: ExecutionContext) = ??? | ||
``` | ||
|
||
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 commentThe 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. |
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
Uh oh!
There was an error while loading. Please reload this page.
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.
@tfinneid , I am not sure how much value there is in adding the formal name to the title as well. Although I do not see a downside to it either (other than rather long section names). I would like @jvican 's opinion on this before I make my edits.
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. 😄