Emacs 26, Threads, and Generators

One of the things that a lot of people are exited about in Emacs 26 is the introduction of basic threading. Another, less mentioned, feature is generators. The excellent Chris Wellons has a must-read post that talks about both of these innovations.

He first considers generators, which he describes as mostly modeled after Python generators and, to a lesser extent, JavaScript generators. Generators return an iterator object that performs a calculation but can stop in middle of the operation to return a value. When it’s called again, it produces the next value and stops again. Python users will be familiar with the idea.

In Elisp, the generators are implemented as closures. Wellons describes how they work and some of the gotchas involved but if you’ve ever implemented a counter function that return the next sequential integer each time it’s called, you’ll be familiar with the concept.

Next Wellons talks about the threading implementation. He says that they’re basically pthreads and that they should be considered a first step in bringing threads to Emacs. Again, he discusses some of the things that can go wrong.

Finally, being Wellons, he wrote an implementation of generators using threads. It was just a proof-of-concept implementation meant to give him some experience in using the new facilities. You can take a look at the code on GitHub if you’re interested.

As always with Wellons’ posts, I learned some new things by reading it. If you do any Elisp at all, you should take a look.

Posted in Programming | Tagged | Leave a comment

Recursion

Mike Zamansky has an interesting post on recursion. The question at hand is whether it makes sense to teach recursion to beginning programming students. Zamansky was a high school CS teacher for many years and now he’s involved—at least in part—with teaching those who might want to become high school CS teachers themselves. It’s no surprise, then, that the beginners he’s concerned with are high school students.

Like most of you, I have no experience teaching high school students so I’m not competent to offer an informed opinion on the question. Nonetheless, I was struck by some of the comments to the post. Most were from HS CS teachers who also had an industry background before going into teaching. What these commenters all said was that they seldom, if ever, had had occasion to use recursion as developers. One even felt that it was a code smell.

Most of you know that these days I’m mostly interested in Lisp and that I’ve put in a bunch of time with Scheme. It’s no surprise, therefore, that recursion is one of the main tools in my kit. But before I ever wrote a single line of Lisp I was still a recursion user. I can’t tell you how many depth-first searches I’ve written in C using recursion. Sure, if you’re writing accounting software you probably won’t find a good reason to use it but I’ve always found it an irreplaceable tool.

My problem is not with finding opportunities to use recursion but with forcing myself to consider seriously whether it’s the best solution for the problem at hand. Depth-first search? I don’t know how you can do it without recursion (simulating recursion with your own stack is just making a simple problem harder). A less clear cut example is the factorial function. I was trained as a mathematician so to me a natural way of thinking of the factorial is:

\begin{equation*} fact(n) = \begin{cases} n, &\text{if $n<3$;}\\ n × fact(n-1), &\text{otherwise.} \end{cases} \end{equation*}

That just screams for recursion, of course—the definition all but writes the code—but a loop may be a better solution in this case. The canonical example of that problem is the Fibonacci sequence:

\begin{equation*} fib(n)= \begin{cases} n, &\text{if $n<2$;}\\ fib(n-1)+fib(n-2), &\text{otherwise.} \end{cases} \end{equation*}

Again, this screams for recursion but disaster results if it’s implemented that way.

My point is that recursion itself is natural for anyone with even a minimal mathematical background. The real problem is knowing when it’s a good idea and when it’s not. Once you’ve got that under control, you can start considering things like tail recursion.

As to the larger point, I’m interested in how many of you use recursion without thinking of it as a special event. Leave a comment if you’d like to share your experience.

Posted in Programming | Tagged | Leave a comment

Introduction to EWW

I’ve been making a point of trying to use the Emacs Web Wowser (eww) when it makes sense. As much as I’d love to move browsing into Emacs—and thus realize my goal of virtually never leaving it—sadly eww can’t replace a full fledged browser such as Safari, Chrome, or Firefox.

There are places, though, where it’s use does make sense. My most frequent use is with mu4e to display HTML emails that miscreants insist on sending me. Even there, it’s not always adequate: emails from Amazon, for example, really need to be displayed in Safari to see the content correctly. Still, for most HTML emails eww is fine.

The other place where eww makes sense is to look at things like on-line manuals. I don’t always remember to use eww for that but I’m trying to train myself. Another place I find it useful is to find the correct spelling for a word I don’t remember how to spell. Sometimes flyspell can help with that but when it doesn’t, I usually just go to the browser and look up the word with an approximate spelling. Almost always, DuckDuckGo will tell me the correct spelling. This is an ideal use for eww because I don’t have to leave Emacs to find that spelling.

Over at Emacs Notes, Emacks has a nice post on using eww. It’s just the basics but it’s good to get you started. The thing I liked best about the post is that Emacks says the proper way to think of eww is “as a handy HTML-to-text converter that extracts content from your favourite web-pages to your personal notebooks.” Looked at that way, you no-longer care as much about its limitations because you’re not trying to use it to replace your browser

Posted in General | Tagged | Leave a comment

Eshell and Emacs Everywhere

Seven or eight months ago, I wrote about a couple of posts from Pierre Neidhardt (Ambrevar) concerning Eshell and the utility of doing as much as possible in Emacs (1, 2). Since then, he’s revised them a bit from the original reddit postings and made them available on his site at Eshell as a main shell and Emacs Everywhere.

I rediscovered them when @hideo_is tweeted links to them. These are really interesting posts and well worth reading again. Ambrevar states his belief that although eshell is not a standard Unix shell, it is better suited for use with Emacs—not being tied to ancient terminals in the way traditional shells are—and that its differences should therefore be celebrated rather than looked upon as shortcomings. He makes, I think, a convincing case for that belief.

In the Emacs Everywhere post he describes the joy of doing almost everything from within Emacs and pleads the case that Emacs is not a violation of the Unix philosophy of a program doing one thing well. He says, essentially, that the one thing Emacs does and does well is to provide a standard and powerful interface to other programs and packages. In that way, it is no different from Qt, GTK, Tk, or even curses.

I enjoyed reading the posts again and you probably will too. If you haven’t already read them, be sure to take a look. They’re definitely worth your time.

Posted in General | Tagged | Leave a comment

The Cost of Adtech

As a result of the GDPR going into effect, USA Today is—presumably temporarily—running a special version of their site for those in the European Union. This version has all the ads and tracking scripts disabled. Marcel Freinbichler shows us the shocking—if not really surprising—results:

If you click on the tweet, you can see the rest of the thread, which includes these statistics:

  • Load time went from more than 45 to 3 seconds.
  • Javascript files loaded went from 124 to 0.
  • HTML requests went from more than 500 to 34.

As I’ve said many times in the past, I understand and accept the need for content providers such as USA Today to serve advertisements. They have to pay for running their business some way after all. But when you get 10% content and 90% tracking and ads, it seems to me that they’re abusing their readers’ goodwill and understanding.

Posted in General | Tagged | Leave a comment

Emacs 26.1 Is Out

Nico Petton tweets the good news:

I’ve already downloaded, compiled, and installed it and am writing this with the new version.

If you’re on macOS, here’s my recipe for the compilation and installation:

configure --with-ns CFLAGS="-g3 -O2 -I /usr/local/include/libxml2"
make
make install
make install-info
sudo mv nextstep/Emacs.app/ /Applications/Emacs.app

The last time I installed Emacs, the last step in the recipe was

sudo cp -R nextstep/Emacs.app/ /Applications/

but this time macOS complained so I used the mv instead.

If you’ve got macOS set to not run “unknown applications” and get an error message when you try to run it, follow these directions.

Posted in General | Tagged | Leave a comment

f-strings in Elisp

John Kitchin, as you probably know, is a fan of Python as well as of Emacs. One of the things he really likes in Python 3 is f-strings. They allow you to do things like

import datetime
today=datetime.datetime.today()
name="J. Random User"
print (f'{name:14s} signed up for Bozoid Services on {today}')

You may or may not like the idea but if you do and you’re an Elisp programmer, Kitchen has a simple implementation in Elisp. They’re not quite as powerful as Python’s but Kitchin says they do what he usually needs them to do.

Unless you’re in the camp that eschews the loop macro and format strings on the grounds that they’re “unlisp-like” you may want to try it out.

Posted in General | Tagged | Leave a comment

The Irreal Privacy Policy

As far as I can tell, the GDPR does not apply to Irreal. Nevertheless, Irreal is trivially in compliance because we don’t collect or store any information about our readers. Even if we did collect such information, we would never sell or share it with anyone.

If you post a comment to Irreal, you are using the Disqus Commenting system and they do collect some minimal information needed to run their commenting network1. That data is under Disqus’ control and Irreal has no say in its use or disposition. If you’re concerned about the information Disqus maintains, log in to the Disqus Data Sharing Page to adjust your settings.

These policies apply to everyone, everywhere, not just readers in the European Union. We’re an equal opportunity non-abuser.

Footnotes:

1

Before Disqus, Irreal used the built-in WordPress commenting system. All logins and associated information to that system have been deleted.

Posted in Administrivia | Tagged | Leave a comment

Using Request to Download Financial Data

Sebastian Schweer has a nice post on using the request package to query an online financial service and download current stock quotes. That data is placed in an Org table along with certain historical data (such as purchase date and original cost) and used to calculate the current value of his stock holdings. The table can, of course, be exported to produce a nicely formatted report.

This post is similar to the one by Charl Botha that I wrote about previously. If you need to programmatically retrieve data from a restful website, you should carefully study these two posts. They show how to use the request package and then parse out the data. Sadly, I haven’t had a need to use these techniques but I’m really looking forward to when I do. Request, let-list, and the rest are tools that I’m dying to try out.

UPDATE [2018-05-25 Fri 18:48]: Added link to Sastibe’s post.

Posted in General | Tagged , | Leave a comment

Going Dark: How Many Locked Phones Are There?

In news that will surprise exactly no Irreal readers, the Washington Post and Techdirt are reporting that the FBI has been lying about the “going dark” problem. One of their favorite refrains is that terrorists, pedophiles, drug dealers, and whatever today’s fourth horseman of the Infocalypse is have been using their encrypted smartphones to frustrate the FBI’s ability to keep us and the American way safe. Their latest estimate is that they are currently locked out of nearly 7,800 phones. Therefore, the next line goes, we need a law that gives us a backdoor into that encrypted information.

Everyone who knows anything at all about the matter knows that this is all nonsense and that the backdoor or golden key or whatever the current euphemism is would make us less, not more, safe. Now it turns out that even the underlying fact for these claims is wrong. Critics have long been suspicious of the 7,800 phones that the FBI claims they can’t access. Now the FBI has admitted that the actual number is probably around 1,200.

It wasn’t that they were lying, they say, it was merely an error. It was, in fact, the ever popular “computer error” that caused the miscount. They mumble about multiple databases and phones being counted more than once. But here’s the thing: According to Wikipedia, there are just 56 FBI field offices in the U.S. What’s wrong with calling them up and asking, “How many locked phones are you currently trying to access?” One agent, or even secretary, could easily get that done in a day or two. Remember that these phones are evidence and are thus recorded in chain of custody logs. I find the claim that it’s hard to suss out this information to be disingenuous if not absurd.

The FBI is not having a good year. Practically everything they’ve told us has turned out to be false. At the same time, they appear unable to handle even routine investigative tasks like following up on the tip that Broward County, Florida school shooter Nikolas Cruz wanted to “kill people” and that there was a potential of him being involved in a school shooting. That potential was realized when Cruz killed 17 people and wounded 17 more. The FBI lied about that too, of course.

Posted in General | Tagged | Leave a comment