Jump to content

Wikipedia:Reference desk/Archives/Computing/2018 February 14

From Wikipedia, the free encyclopedia
Computing desk
< February 13 << Jan | February | Mar >> February 15 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


February 14

[edit]

Python: does lambda dereference?

[edit]
for ...:
    ...
    f = a callable object
    t0 = a number
    ListOfFunctions.append( lambda t: f(t-t0) )
    ...

Will ListOfFunctions[k](x) subsequently do the right thing, or is the body of a lambda interpreted afresh whenever it's called and thus with the last values of f and t0? —Tamfang (talk) 06:47, 14 February 2018 (UTC)[reply]

  • Couldn't you just try it...? Anyway, lambda are evaluated at call time, so what you intended will not work. For instance:
>>> f = lambda x: t+x  # t was not defined yet
>>> f
<function <lambda> at 0x02400A30>
>>> f(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
NameError: global name 't' is not defined
>>> t=5
>>> f(5)
10
>>> t=10
>>> f(5)
15
TigraanClick here to contact me 09:40, 14 February 2018 (UTC)[reply]
Well, no ;-). This may be the behaviour if t is not defined, but one of the primary features of a classical lambda is that it captures the environment of the definition. So:
>>> t = 10
>>> f = lambda x:x+10
>>> f
<function <lambda> at 0x1010cdc08>
>>> f(5)
15
>>> t = 0
>>> f(5)
15
...Python does actually capture the value of t (if any) and keeps it. --Stephan Schulz (talk) 11:00, 14 February 2018 (UTC)[reply]
Stephan Schulz - should your second line of Python be f = lambda x:x+t instead of f = lambda x:x+10 ? Gandalf61 (talk) 11:37, 14 February 2018 (UTC)[reply]
Aaaargh! I withdraw everything. Without the think/type disconnect, it does not capture t (which leaves me very disappointed). --Stephan Schulz (talk) 20:36, 14 February 2018 (UTC)[reply]
The idiom for capturing t is "lambda x,t=t: x+t". 73.71.213.123 (talk) 05:27, 16 February 2018 (UTC)[reply]
Wow, that seems to work, though I haven't found any documentation on it. —Tamfang (talk) 06:59, 16 February 2018 (UTC)[reply]
That's good to know. (Although, lambda-ing a function that can take optional arguments is an... interesting approach to Python, let's say.) TigraanClick here to contact me 20:29, 16 February 2018 (UTC)[reply]
Python lambdas capture variables, not values, so all your lambdas will be equivalent. This is a subtle subject: the answer for C# depends on how the variables are declared, but Python doesn't have scopes within a function. (If this code is not in a function, the lambdas capture nothing at all, so they're equivalent in that case too.) The way to fix it (other than the default argument trick) is to introduce another stack frame so as to have different variables for each closure:
def offset(f,t0): return lambda t: f(t-t0)
def makeFunctions(...):
  for g,u0 in ...:       # different names for clarity
    ListOfFunctions.append(offset(g,u0))
This works the same way even if the body of makeFunctions is direct module/script code. --Tardis (talk) 02:12, 17 February 2018 (UTC)[reply]
Not my question, but thanks nonetheless. This makes me happier! --Stephan Schulz (talk) 13:03, 18 February 2018 (UTC)[reply]

Mojibake conversion

[edit]

I've just run across an Arabic name (a publishing company) that's been misencoded and rendered as mojibake:

مركز التراث Ù„لبرمجيات

It appears on a webpage that's encoded in UTF-8, but that's not the source of the problem — an Arabic book title (الإحكام لابن حزم : الإحكام في أصول الأحكام, written by someone named إبن حزم ، أبو محمد علي بن أحمد بن سعيد الأندلسي 384 - 456 هـ.; مركز التراث) appears two lines away, so this mangled title's come from somewhere else, and nothing tells me what kind of encoding produced this mess.

Can someone help me convert this gibberish into Arabic? I just need the publisher name, not the author. Most of the pages I'm finding merely talk about conversion; the only conversion site that Google found is a page whose entire contents are Project web is currently offline pending the final migration of its data to our new datacenter. Nyttend backup (talk) 18:03, 14 February 2018 (UTC)[reply]

Sadly, I think the actual characters have already been lost - when I view what you have pasted using my hex editor, it looks like we have neither ASCII nor UTF-8, nor UTF-16, nor ISO/IEC 8859-6, nor even Windows-1256.
Rather, it looks like we have UTF-8 printable characters that correctly encode the gibberish as a string literal.
Although I'm still looking for any sign that the original text is recoverable, I'm not sure we're going to find it in what you've pasted.
Can you link to the troublesome document, in case that incarnation of the malformed string contains data that is more recoverable than the data already pasted here?
Nimur (talk) 19:52, 14 February 2018 (UTC)[reply]
Thank you for the note. Unfortunately, I can't provide any details — I got the string from a subscription-access library database, and when I copy/paste the ISBN or the actual Arabic title into WorldCat, I get no results. Nyttend backup (talk) 20:40, 14 February 2018 (UTC)[reply]
By any chance, is it this historical book: الإحكام في أصول الأحكام لابن حزم, by author أبو محمد علي بن أحمد بن سعيد بن حزم الأندلسي القرطبي الظاهري (transliterated, roughly, Ahmad bin Said bin Hazam Al-Andalusy ...and most likely the guy our English Wikipedia calls Ibn Hazm )? That webpage claims that the book was republished in limited edition by الناشر: دار الآفاق الجديدة بيروت (something like, "New Horizons,") Beirut.
Nimur (talk) 01:29, 15 February 2018 (UTC)[reply]
I even found their address, thanks to some sketchy internet yellow-page-style advertisement websites!
Dar al Afaq Al Jadidah, (House of the New Horizons Publishers): 2nd floor, Hanna Building. Makdessi Street, Hamra, Beirut. Telephone: +961 1 351246.
I can't find any official internet website for the publishing company, so I can't verify their contact is up-to-date! But that sounds about right for an old-school post-colonial boutique academic book publisher!
I found a lot of other publications, generally of a historical or intellectual/academic nature, that this publishing house has churned out. A lot of their publications appear to be reprints or annotations of ancient works. But they also have printed some pretty modern stuff! Here's my favorite so far:
العرب والتكنولوجيا
سمير عبده. Written/published (1981).
If you manage to find an ISBN, or find anywhere these books are distributed, lemme know! I have a hunch that there are still publishers in some far corners of the world that don't "do" ISBNs...
Nimur (talk) 03:50, 15 February 2018 (UTC)[reply]
No subject beyond "Religion", and no LC, although it has a Dewey call number of 260. Book is 148 pages, and its print publication date was apparently 1 July 2012. The cover is composed of the title, large stylized writing in a corner (maybe the author's name?), and the date 2012 in Hindu-Arabic numerals, plus a geometric design that repeats eighteen times around the edge. ISBN is 9789957636739, which gets zero Worldcat hits, zero Google hits when searched as a phrase, and when I search it as "978-9-95-763673-9", all I get is Google's calculator telling me that 978 minus nine minus ninety-five etc. equals -762808 and a list of the results I'd get if I left off the quotation marks. Nyttend (talk) 02:38, 16 February 2018 (UTC)[reply]
978-99-57 is the prefix for a Jordanian publisher - so evidently we haven't found the real publisher yet... Nimur (talk) 07:43, 16 February 2018 (UTC)[reply]
And according to the ISBN Global Register of Publishers, ...978-9957-636 is the prefix for "qettaf al danaih for publishing and distribution", in Amman. I can't find any more detail than that. You might actually make some headway searching the National Library of Jordan's catalog - but the English web interface is a little difficult to use. You could try telephoning the national librarian! Nimur (talk) 08:12, 16 February 2018 (UTC)[reply]