tag:blogger.com,1999:blog-5089773352404981635.post7155941879432405408..comments2023-10-04T08:15:13.812-07:00Comments on Daily scala: Return value of a blockAnonymoushttp://www.blogger.com/profile/07600430363435495915noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5089773352404981635.post-92194471226992434192010-09-24T19:28:36.544-07:002010-09-24T19:28:36.544-07:00I'm just starting to learn Scala and this kind...I'm just starting to learn Scala and this kind of details are perplexing. Nevertheless, I think I found a place where it makes sense:<br /><br />scala> List(5,6,7,8).map{var num = 0; i:Int=> {num = num+1; num.toString + "_" + i}}<br /><br />res44: List[java.lang.String] = List(1_5, 2_6, 3_7, 4_8)<br /><br />I'm using the part that is executed only once to do some initialization, and the rest of the block (the function) is executed once per element in the list, using the initialized values.Mario Sánchezhttps://www.blogger.com/profile/14660102441467032313noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-85639717731123352962010-06-20T14:06:14.478-07:002010-06-20T14:06:14.478-07:00I think there is a case when the whole block will ...I think there is a case when the whole block will be executed multiple times, and that is when you use a "call by name" argument. E.g.<br /><br />scala> var count=0<br />count: Int = 0<br /><br />scala> def g(x: =>Unit) = { for (i<-1 to 4) {x} }<br />g: (x: => Unit)Unit<br /><br />scala> g{count+=1; println("hi")}<br />hi<br />hi<br />hi<br />hi<br /><br />scala> count<br />res13: Int = 4Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-12851148932428556702010-05-27T13:00:25.807-07:002010-05-27T13:00:25.807-07:00This is not a bug. It is an effect of consistency...This is not a bug. It is an effect of consistency. As I mention in the post a block is a sequence of statements and only the last statement is used in the return. The {x => xxx} is actually a special construct of:<br /><br />val x = (m:Int) => {println("hi"); m + 1}<br /><br />The { x=> xxx} construct is nice when passing a function as a paramter:<br /> <br />method(x => {x+2})<br /><br />vs<br /><br />method {x=> x+2}<br /><br />It is a case of reducing syntactic noise.<br /><br /><br />In summary all the choices separately make sense but when combined obviously has an unexpected side effect.Anonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-4649899002665408472010-05-27T08:12:11.018-07:002010-05-27T08:12:11.018-07:00I wonder why... is just a bug or it has some purpo...I wonder why... is just a bug or it has some purpose?Anonymoushttps://www.blogger.com/profile/17525961934302794120noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-38863407163969070392010-05-27T06:09:50.768-07:002010-05-27T06:09:50.768-07:00I guess that depends on what I mean by placeholder...I guess that depends on what I mean by placeholder params :). <br /><br />{_.toString} has a placeholder param (by my definiition) and my rule is that it should always be a single statement. otherwise use explicit parameters like:<br /><br />{x => x.toString}Anonymoushttps://www.blogger.com/profile/07600430363435495915noreply@blogger.comtag:blogger.com,1999:blog-5089773352404981635.post-88874912818421703352010-05-27T05:48:52.129-07:002010-05-27T05:48:52.129-07:00Whoa, I had no idea...
"Rule of thumb: Funct...Whoa, I had no idea...<br /><br />"Rule of thumb: Functions with placeholder parameters should be a single statement."<br /><br />That should be _without_ placeholder params, right?Adam Rabunghttps://www.blogger.com/profile/08575207879136726271noreply@blogger.com