-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
…Function Syntax" page (tutorials/tour/anonymous-function-syntax.md).
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. |
the Travis log shows:
|
def main(args: Array[String]) { | ||
|
||
// Create an integer to test the anonymous function with | ||
var myInteger: Int = 0; |
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 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.
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 updated that part.
The following example shows how to use anonymous function of the beginning of this page | ||
|
||
```tut | ||
package tour |
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.
The tut
error you're getting will go away if you simply omit this line, which isn't necessary anyway.
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 removed the 'package tour'
|
||
println(myInteger) // Prints: 0 | ||
|
||
myInteger = anonymousIncrementFunction(myInteger) |
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 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.
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 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.
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.
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.
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 |
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). scala> object AnonymousFunction { 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 |
if we want to keep the exact structure of your original code, then we would need two 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. |
oh by the way about getsatisfaction.com, sorry about that, see #645 |
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.
thank you! |
…Function Syntax" page (tutorials/tour/anonymous-function-syntax.md).