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

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

merged 5 commits into from Feb 8, 2017

Conversation

ghost
Copy link

@ghost ghost commented Nov 17, 2016

…Function Syntax" page (tutorials/tour/anonymous-function-syntax.md).

…Function Syntax" page (tutorials/tour/anonymous-function-syntax.md).
@ghost
Copy link
Author

ghost commented Dec 2, 2016

I didn't know how to compile the code, that is why I asked about it on this page:

https://getsatisfaction.com/scaladocs/topics/how-do-i-check-if-contribution-is-free-of-errors

I noticed in your log you are running a

./scripts/run-tut.sh

script on linux with ruby installed. Since I don't have all this and it's too much effort for the small change I wanted to commit, we can just leave it out.

Thanks anyways for trying to merge my changes.

@SethTisue
Copy link
Member

the Travis log shows:

[tut] compiling: ./tutorials/tour/anonymous-function-syntax.md
[tut] *** Error reported at /home/travis/build/scala/scala.github.com/tutorials/tour/anonymous-function-syntax.md:58
<console>:2: error: illegal start of definition
package tour
^

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.

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'


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.

@SethTisue
Copy link
Member

SethTisue commented Dec 10, 2016

Thanks, this is now closer to being mergeable.

But — and I'm sorry, I don't mean to be difficult — but there's really not a good reason to use var here, and it's really not a good idea for introductory Scala materials to send a signal to Scala newcomers that unnecessary use of var is normal, when it really isn't.

@ghost
Copy link
Author

ghost commented Dec 12, 2016

hey, thats all right. thanks for the feedback, but i really don't feel comfortable applying your requested change, since the code does not compile on my machine.

i am using eclipse mars with scala ide v4.4.1, i tried both scala library 2.10.6 and 2.11.8 and just to make sure, i additionally tried to compile the code directly in my CMD running the scala interpreter. this is the output of the latter:

`Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> object AnonymousFunction {
|
| /**
| * Method to increment an integer by one.
| /
| 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
| myInteger = 0
|
| println( myInteger ) // Prints: 0
|
| myInteger = anonymousIncrementFunction( myInteger )
|
| println( myInteger ) // Prints: 1
| }
| }
:12: error: not found: value anonymousIncrementFunction
anonymousIncrementFunction = ( x : Int ) => x + 1
^
:21: error: not found: value myInteger
myInteger = 0
^
:23: error: not found: value myInteger
println( myInteger ) // Prints: 0
^
:25: error: not found: value myInteger
myInteger = anonymousIncrementFunction( myInteger )
^
:27: error: not found: value myInteger
println( myInteger ) // Prints: 1
^
`

are you telling me that this code compiles and runs on your machine without errors, or am i getting something wrong here???

i can change var anonymousIncrementFunction to val anonymousIncrementFunction, but ommiting it completely does not work. i cannot change var myInteger = 0 to val myInteger = 0, since i get a 'reassignment to val' error in the line myInteger = anonymousIncrementFunction( myInteger ).

@SethTisue
Copy link
Member

SethTisue commented Jan 11, 2017

if we want to keep the exact structure of your original code, then we would need two vals, each with a different name.

but what about dodging the issue entirely and just doing:

val plusOne = (x: Int) => x + 1
println(plusOne(0))  // Prints: 1

this seems to me like the core of what your example is trying to convey.

@SethTisue
Copy link
Member

oh by the way about getsatisfaction.com, sorry about that, see #645

jjELT added 2 commits January 11, 2017 08:35
Omitted the, apparently for Scala unconventional, usage of 'var's in the code.
Example is now slimmer, i.e. there is only one line in the main method where the anonymous function is called once and the return value is immediately printed.
@SethTisue SethTisue merged commit 336411e into scala:master Feb 8, 2017
@SethTisue
Copy link
Member

thank you!

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

Successfully merging this pull request may close these issues.

3 participants