tag:blogger.com,1999:blog-8263949408347549596.post8491022368065574875..comments2023-10-23T23:19:01.111+02:00Comments on Object-Oriented Software Development: C# Puzzle No.23 (intermediate)Wiktor Zychlahttp://www.blogger.com/profile/04420514974154487039noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8263949408347549596.post-68660888296968067192013-12-17T23:56:40.634+01:002013-12-17T23:56:40.634+01:00Very nice post :) But for me tip " you are al...Very nice post :) But for me tip " you are allowed to modify only this line of code" is a little bit misleading. It forbids declaration of new variable. So I found same solution as @apl. It looks bad in both languages, but works :)<br /><br />funcs[i] = function(a) { return function() { return a; } }(i);Anonymoushttps://www.blogger.com/profile/00054407674147176978noreply@blogger.comtag:blogger.com,1999:blog-8263949408347549596.post-70366990504097271272013-12-13T08:44:46.228+01:002013-12-13T08:44:46.228+01:00@Mojo: That's exactly the solution I have in m...@Mojo: That's exactly the solution I have in my mind. It does the trick as the inner delegate is returned from an auxiliary method that accepts i as a parameter which effectively stops it from being caught inside the closure. The same solution fixes the javascript version but there is no need for explicit type there.Wiktor Zychlahttps://www.blogger.com/profile/04420514974154487039noreply@blogger.comtag:blogger.com,1999:blog-8263949408347549596.post-22347693363647978822013-12-12T18:03:34.912+01:002013-12-12T18:03:34.912+01:00That's pretty much exactly where I was going, ...That's pretty much exactly where I was going, but it seemed like there must be a nicer way to wrap that into a closure.Mojohttps://www.blogger.com/profile/09353259075625848908noreply@blogger.comtag:blogger.com,1999:blog-8263949408347549596.post-76682163380875931772013-12-12T18:00:06.032+01:002013-12-12T18:00:06.032+01:00For one, we can go lambda-happy:
funcs[i] = ((Fun...For one, we can go lambda-happy:<br /><br />funcs[i] = ((Func<int, Func<int>>)(j => () => j))(i);aplhttps://www.blogger.com/profile/14783143626906780756noreply@blogger.comtag:blogger.com,1999:blog-8263949408347549596.post-23766433378594019472013-12-12T17:49:26.706+01:002013-12-12T17:49:26.706+01:00I'm interested to see some solutions to this. ...I'm interested to see some solutions to this. My first reaction would be to create a variable inside that inner scope which wouldn't be modified i.e.<br /><br />var x = i;<br />funcs[i] = () => x;<br /><br />But I'm not sure how to translate that into only that one line.Mojohttps://www.blogger.com/profile/09353259075625848908noreply@blogger.com