Skip to content

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

Merged
merged 12 commits into from
Jan 22, 2018
Merged

Rewrite Currying tour as Multiple Parameter Lists #986

merged 12 commits into from
Jan 22, 2018

Conversation

sujithjay
Copy link

@sujithjay sujithjay commented Jan 11, 2018

replaces #732 . This is a work in progress; raising a PR to elicit comments.

@sujithjay
Copy link
Author

cc: @SethTisue @jvican
Please take a look.

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.

@SethTisue SethTisue mentioned this pull request Jan 11, 2018
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.
Copy link
Member

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

Copy link

@tfinneid tfinneid Jan 12, 2018

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.

Copy link
Author

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.

Copy link
Member

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. 😄

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:
Copy link
Member

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? 😄

Copy link

@Adowrath Adowrath Jan 15, 2018

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.

Copy link
Member

@jvican jvican Jan 16, 2018

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.


Multiple parameter lists have a more verbose invocation syntax; and hence should be used sparingly. Suggested use cases include:

1. ##### Implicit parameters
Copy link
Member

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?

```

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.
Copy link
Member

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.

@jvican
Copy link
Member

jvican commented Jan 11, 2018

Thanks for picking this up @sujithjay, it's appreciated.

@sujithjay
Copy link
Author

cc: @jvican @Adowrath @tfinneid @SethTisue
Please take a look

Copy link
Member

@jvican jvican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks 😄

@jvican
Copy link
Member

jvican commented Jan 21, 2018

I defer merging to @SethTisue

@tfinneid
Copy link

tfinneid commented Jan 21, 2018

---@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.

@SethTisue SethTisue changed the title [WIP] Rewrite Currying tour as Multiple Parameter Lists Rewrite Currying tour as Multiple Parameter Lists Jan 22, 2018
@SethTisue SethTisue merged commit 4d17056 into scala:master Jan 22, 2018
@SethTisue
Copy link
Member

thanks, @sujithjay, for taking this on!

@SethTisue
Copy link
Member

followup PR to further clarify the relationship between multiple parameter lists and currying: #2490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants