> The takeaway is that you don't need to know any of this crap (and a lot of
> other crap we all already know from Java) if you're not performing side
> effects.
>
+1 to that!




> On Jul 27, 2012 11:13 AM, "Kevin Wright" <kev.lee.wri...@gmail.com> wrote:
>
>> You can certainly capture/"close over" the surrounding environment with
>> any Scala anonymous function.  So you can say quite correctly state that
>> any method accepting a function will accept a closure.
>>
>> As to whether or not you must actually exercise this capability before
>> you're allowed to call it a closure - I guess that's a matter of semantics.
>>
>> Going on the definition that you must "use it or lose it", then your
>> example is a closure, but probably not in the way you're thinking:
>>
>>    - {y+= 1; println} is evaluated.  It adds one to y and returns a
>>    String=>Unit function
>>    - This function is then executed within the for(x <- 1 to 10)comprehension
>>
>> At first glance, nothing is obviously captured from the surrounding
>> environment.  But... println is about as impure as you can get, working by
>> pure side effect; it has behaviour that depends very much on the
>> surrounding system.
>>
>>
>> Don't believe me?  Then try this:
>>
>> def foo(): Unit = {
>>   var y = 0
>>   1 to 10 map {y+= 1; println}
>>   println(y)
>> }
>>
>> foo()
>> System.setOut(someOtherPrintStream)
>> foo()
>>
>>
>>
>>
>> On 27 July 2012 14:30, Josh Berry <tae...@gmail.com> wrote:
>>
>>> On Fri, Jul 27, 2012 at 8:32 AM, Ricky Clarkson
>>> <ricky.clark...@gmail.com> wrote:
>>> > 1 to 10 map { println("Yo"); println }
>>> >
>>> > Yo gets printed once, println happens 10 times.  Just because you're
>>> > providing a function doesn't mean you're in a closure.  If it was a
>>> closure
>>> > (and certain other magic happened to make it well-typed) you'd see Yo
>>> 10
>>> > times with a blank line between each.
>>>
>>> I'm lost.  A closure simply means it captures the local environment,
>>> right?  So:
>>>
>>> var y = 0
>>> 1 to 10 map {y+= 1; println}
>>> println(y)
>>>
>>> Now, I confess I am surprised that it appears this closure is called
>>> once to get a function from Int => Any.  I'm assuming it has always
>>> been this way in Scala?
>>>
>>> Of course, this does as expected, and looks similar.
>>>
>>> for (x <- 1 to 10) {y+=1; println("hello")}
>>>
>>> Is this is not a closure, as well?
>>>
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups "Java 
Posse" group.
To post to this group, send email to javaposse@googlegroups.com.
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to