I figured out what it did even before following the link to your website. The binary string trick is pretty cool, but I could think of a lot of better ways to do this. Were you just trying to do it or were you intentionally trying to come up with something more obfuscated? Also, I think there are two problem, although they only occur in certain cases and I'm not exactly sure what your preconditions are. I am assuming that the goal of the lambda function xkcd is to take some value x, some key k, and 2 dictionaries c and d, and return c[k] if c has a key k, otherwise d[k] if d has a key k, or x otherwise. If that is so I find the following problems. First, what if k is in both c and d? You will get an index error. If you really wanted it to match that PHP expression then all you'd need to do is add another c:

Code: Select all

`xkcd = lambda x,k,c,d: (({k:False},d,c,c)[int(''.join([str(int(k in t)) for t in [c,d]]),2)])[k] or x`

Second, what if c[k] or d[k] exists but is False (or any other false value)? The function would return x instead. This could be a very good time for one of my favorite lambda tricks (wrapping ever term in a list and then taking index 0 at the end) but wouldn't also be as easy to write it as:

Code: Select all

`xkcd = lambda x,k,c,d: (({k:x},d,c,c)[int(''.join([str(int(k in t)) for t in [c,d]]),2)])[k]`

Now, I think a much more straightforward way to write this (using the trick I described above to prevent the previous problem):

Code: Select all

`xkcd = lambda x,k,c,d: ((k in c) and [c[k]] or ((k in d) and [d[k]] or [x]))[0]`

But I think the simplest way (although which does not get to benefit from any short-circuit logic) is:

Code: Select all

`xkcd = lambda x,k,c,d: c.get(k,d.get(k,x))`