--- Log opened Sun Jun 09 00:00:39 2019 |
01:24 | | Kindamoody is now known as Kindamoody[zZz] |
03:07 | | celticminstrel [celticminst@Nightstar-6an2qt.dsl.bell.ca] has quit [[NS] Quit: And lo! The computer falls into a deep sleep, to awake again some other day!] |
05:28 | | Vorntastic [uid293981@Nightstar-6br85t.irccloud.com] has joined #code |
05:28 | | mode/#code [+qo Vorntastic Vorntastic] by ChanServ |
08:32 | | Kindamoody[zZz] is now known as Kindamoody |
09:09 | | Kindamoody [Kindamoody@Nightstar-eubaqc.tbcn.telia.com] has quit [Connection reset by peer] |
09:10 | | Kimo|autojoin [Kindamoody@Nightstar-eubaqc.tbcn.telia.com] has joined #code |
09:10 | | mode/#code [+o Kimo|autojoin] by ChanServ |
09:11 | | Kimo|autojoin is now known as Kindamoody |
10:47 | | Emmy [Emmy@Nightstar-9p7hb1.direct-adsl.nl] has joined #code |
11:12 | | Kindamoody is now known as Kindamoody|afk |
11:57 | | Kindamoody|afk is now known as Kindamoody |
13:16 | | Kindamoody is now known as Kindamoody|out |
14:37 | | Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has joined #code |
14:37 | | mode/#code [+qo Vornicus Vornicus] by ChanServ |
15:18 | | celticminstrel [celticminst@Nightstar-6an2qt.dsl.bell.ca] has joined #code |
15:18 | | mode/#code [+o celticminstrel] by ChanServ |
15:45 | | * ToxicFrog screams at python |
15:46 | <&ToxicFrog> | a = []; for n in range(0,10): a += [lambda: n]; a[0]() |
15:46 | <&ToxicFrog> | What do you expect this to return? |
15:48 | <~Vornicus> | 10 |
15:48 | <&ToxicFrog> | WHY?! |
15:49 | <~Vornicus> | (but I'm well aware of Python's semantics here; they're quite similar to javascript's.) |
15:49 | <&ToxicFrog> | I mean, I know why, it's because all the lambdas close over the same "n" and retroactively get the same final value for it when called |
15:50 | <&ToxicFrog> | But as someone used to lisp and lua semantics this is both surprising and unpleasant, and leaves me rummaging around in functools to figure out a way to implement this basic functionality |
15:50 | <&ToxicFrog> | Probably using partial() |
15:51 | <&[R]> | Is that: a = []; for (var n = 0; n < 10; n++) { a.push(function() { return n }) }; a[0]() //? |
15:52 | <&[R]> | (JS equiv, I think) |
15:52 | <~Vornicus> | yes |
15:53 | <&ToxicFrog> | Yeah, and in Lua the n gets rebound on each loop iteration, so each function closes over a different n and a[0]() returns 0. |
15:53 | <&[R]> | You could just make a function that returns a function though to fix the issue, yes? |
15:54 | <&ToxicFrog> | (the actual problem I'm trying to solve is: for command in [long list of commands with trivial handler code]: locals()[handler_name(command] = lambda ...: (some stuff involving command)) |
15:54 | <&[R]> | function F(n) { return function() { return n } }; a = []; for (var n = 0; n < 10; n++) { a.push(F(n)) }; a[0]() |
15:54 | <&ToxicFrog> | [R]: yeah but at that point I've just reimplemented a hyperspecialized version of partial(), so I might as well just use partial(). |
15:55 | <&[R]> | Ah, I have no idea what partial() is |
15:57 | <&ToxicFrog> | partial(f, args...) returns a version of f with the args baked in |
15:58 | <&ToxicFrog> | E.g. def mul(x,y) return x*y; double = partial(mul, 2) |
15:58 | <&ToxicFrog> | double is now a function of one argument that's equivalent to calling mul(2, <argument to double>) |
15:58 | <&ToxicFrog> | (the general term for this is "partial application") |
16:01 | <&[R]> | Ah |
16:28 | | Vorntastic [uid293981@Nightstar-6br85t.irccloud.com] has quit [[NS] Quit: Connection closed for inactivity] |
16:29 | | gnolam [lenin@Nightstar-hfrbpd.cust.bahnhof.se] has joined #code |
16:29 | | mode/#code [+o gnolam] by ChanServ |
19:40 | <&jeroud> | ToxicFrog: The loop variable belongs to the outer scope and is mutated for each iteration. The typical solution is to put "n = n" at the top of the loop body. |
19:48 | <&jeroud> | (This is also why "for n in range(10): ...; print(n)" is legal.) |
19:58 | <&ToxicFrog> | jeroud: Python doesn't use block scope, though, so I thought n=n was reassignment, not creating a new binding |
19:58 | <&ToxicFrog> | If it does create a new binding, that's convenient, but also surprisingly inconsistent with the rest of the language :/ |
19:59 | <&jeroud> | I think there are special scoping rules for closures. |
19:59 | <&ToxicFrog> | [screaming intensifies] |
20:00 | <&McMartin> | Note that in Lisp you get ten 10s as well, but you'd never structure the loop like that. |
20:00 | <&jeroud> | And now that I think about it, I suspect any variable defined in the loop body is available after the loop. |
20:00 | <~Vornicus> | oh, I should correct myself |
20:01 | <~Vornicus> | I said 10 up there |
20:01 | <~Vornicus> | you only get 9 |
20:03 | <&ToxicFrog> | McMartin: yeah, I lump lisp in with lua here because the idiomatic way of phrasing it both languages results in each closure closing over a different n. |
20:03 | <&ToxicFrog> | Even if the actual details of that are different |
21:40 | | Kindamoody|out is now known as Kindamoody |
22:45 | | Kindamoody is now known as Kindamoody[zZz] |
23:08 | | Vornicus [Vorn@ServerAdministrator.Nightstar.Net] has quit [Connection closed] |
23:59 | | Emmy [Emmy@Nightstar-9p7hb1.direct-adsl.nl] has quit [Ping timeout: 121 seconds] |
--- Log closed Mon Jun 10 00:00:40 2019 |