Skip to content

Added an example on how to use anonymous functions in the "Anonymous … #628

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 5 commits into from Feb 8, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tutorials/tour/anonymous-function-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,33 @@ Function1[Int, Int]
Function2[Int, Int, String]
Function0[String]
```

The following example shows how to use anonymous function of the beginning of this page

```tut
package tour
Copy link
Member

Choose a reason for hiding this comment

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

The tut error you're getting will go away if you simply omit this line, which isn't necessary anyway.

Copy link
Author

Choose a reason for hiding this comment

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

I removed the 'package tour'


object AnonymousFunction {

/**
* Method to increment an integer by one.
*/
var anonymousIncrementFunction = (x: Int) => x + 1

/**
* Main method
* @param args application arguments
*/
def main(args: Array[String]) {

// Create an integer to test the anonymous function with
var myInteger: Int = 0;
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 suggest omitting : Int. (It's idiomatic in Scala to omit type declarations when the type is obvious, as it is here.)

You can omit the semicolon too.

Copy link
Author

Choose a reason for hiding this comment

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

I updated that part.


println(myInteger) // Prints: 0

myInteger = anonymousIncrementFunction(myInteger)
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 suggest that the example avoid using var at all. In idiomatic Scala code, it's not normal for var to appear unless it's truly needed.

Copy link
Author

Choose a reason for hiding this comment

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

I couldn't ommit the var in the two places it appeared, else the code did not compile. I guess it is 'truly needed' in those cases.

Copy link

Choose a reason for hiding this comment

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

You could do something like this to show that a previously incremented val will be incremented again:


println(myInteger) // Prints: 0

val myIncrementedInteger = anonymousIncrementFunction(myInteger)

println(myIncrementedInteger) // Prints: 1

println(anonymousIncrementFunction(myIncrementedInteger)) // Prints: 2

println(anonymousIncrementFunction(anonymousIncrementFunction(myInteger)) // Prints: 2

It is more idiomatic to assign a result to a new val rather than reusing it.


println(myInteger) // Prints: 1
}
}
```