JSON And S-Expressions Redux

After yesterday’s post on JSON Versus S-Expressions I thought that one obvious solution for people who want to use JSON tools to process logs is to have the (Lisp-based) logs convert themselves to JSON much like we did for S-expressions to XML. I thought about issuing another challenge to write Elisp code to do the conversion but it’s so similar to the Sexpr-to-XML example that I decided not to.

I’m pretty sure that I remember seeing Lisp code that converts JSON into Sexprs and vice versa so you could start with either Sexprs or JSON and easily convert to the other if that was more convenient. I think the code I saw was for either Scheme or CL so a good exercise would be to write Elisp code that converts JSON into Sexprs. That way you could work with JSON in Emacs.

Actually, that seemed like such a good idea that I thought someone must surely have done it and, of course, someone has. It’s even included with Emacs. Is there anything Emacs can’t do?

Posted in Programming | Tagged , , | Leave a comment

More On JSON Versus S-Expressions

I’ve written several posts on JSON, most recently here and here. These last two posts considered the advantages of JSON over S-expressions for writing log files. Although S-expressions are more powerful and flexible than JSON, as explained in the 7-part series referenced in the Writing Log Files In JSON post, I concluded that JSON might, nevertheless, be the right answer because there are tools available for working with JSON in almost every language, whereas S-expressions, with a few exceptions, are most naturally handled with some flavor of Lisp.

Now, a post on the SHiNKiROU Blog entitle S-Expressions: The Fat-Free Alternative to JSON is giving me second thoughts. The context of the post is a bit larger than log files and includes such things as document markups. JSON is certainly a win over XML for key-value-pairs, the post says, but not so good for things like HTML markup. S-expressions, on the other hand, can do both jobs equally well. Chapter 16 Paul Graham’s ANSI Common LISP, for example, describes a set of functions for turning S-expressions into HTML.

I find the post pretty persuasive although I do still worry about non-Lispers being comfortable with sexprs or at least being able to do useful things with them right off the bat. Say what you will about JSON, it’s very easy to learn just about everything you need to know about them—excluding the various tools that work on them, of course. S-expressions are pretty easy too but I don’t think there are as many (non-Lisp) tools available to work with them. Much as I would like everyone to learn a bit of Lisp, we all know that’s not going to happen so where does that leave us? Should we use the more powerful and simple S-expressions, or should be use JSON because of its multi-language support? What do you think?

Posted in General | Tagged , | 1 Comment

PostgreSQL 9.2 To Support JSON As A Core Type

Andrew Dunstan has a post in his Andrew’s PostgreSQL blog reporting that PostgreSQL 9.2 will support JSON as a core type. This is yet another reason to favor JSON as a universal log reporting format as I discussed in my Writing Log Files In JSON post.

Of course, as a Lisper I’d still use S-expressions for any of my own projects due to their flexibility and self-transforming capabilities. Still, if you’re going to have non-Lispers using those logs, JSON makes a lot of sense. Certainly more than the common “printf” style logs, and especially more than an XML abomination.

Posted in General | Leave a comment

Functional Thinking

Here’s an interesting talk about thinking in a functional way by Neal Ford. I’ve tagged this post as Lisp but Ford doesn’t use Lisp—he uses some weird mutation of Java, a little Groovy, and a little Scala. The point of the talk, he says, is not about functional programming languages but about learning to think functionally.

Be sure not expand the embedded video because the slides appear in the bigger box under the video.

Posted in Programming | Tagged | Leave a comment

Useful Emacs Commands Reference

Xah Lee has several useful reference pages, such as this one of HTML/XML Entities, that I use all the time. I have them bookmarked for easy reference so that when I, for example, need to know the Unicode character for ρ, I can just bring up the page in my browser to look it up. I’ve also been bookmarking pages from my blog that I describe as “notes to myself” for the same reason.

Over time I find that I have a bunch of pages with a lot of explanatory text and then a summary at the end that is what I’m really interested in. I decided to combine those summaries into a single document to make things easier to find. Since those notes-to-myself posts tended to generate a lot of comments (by Irreal standards) I thought I would make the page available in case anyone else found it useful.

The page is here if you’d like to bookmark it and I’ve also added a link to it over on the right under Links.

Posted in General | Tagged | 5 Comments

Emacs On Google+ And Strange Keybindings

Xah Lee announced on his Emacs Blog that he has started an Emacs stream on G+. If you’re interested in Emacs, you should check it out. You don’t have to be a G+ member to read the posts.

Also interesting in that post is a link to A Curious Look at GNU Emacs’s 1000+ Default Keybindings. In it, Lee looks at the default keybindings and finds many strange things. Some of the bindings are historical artifacts and not used anymore. Some are accessed with keys that don’t appear on any modern keyboards. It’s an entertaining post with lots of things I didn’t know before. There’s even a picture of the Symbolics Space Cadet keyboard that had some of those mysterious keys that are no longer extant.

Update: binding → bindings

Posted in General | Tagged | Leave a comment

Safety And The Lisp Read Function

For all my blathering about security, here’s a potential exploit in Common Lisp that I never thought about. William Halliburton has a nice explanation of Lisp’s read function and the assoicated read-macro #.. When read sees the sequence #. it will evaluate the next Lisp object that it reads. For example, given the following inputs to read the corresponding object is returned as the result of the read:

(+ 1 (/ 8 4)) → (+ 1 (/ 8 4))
#.(+ 1 (/ 8 4)) → 3
(+ 1 #.(/ 8 4)) → (+ 1 2)

That’s pretty neat but as Halliburton points out it also poses a security threat because a user can use it to execute arbitrary code in a Lisp program that naively reads data from the user.

Follow the link for a more complete explanation of the #. read-macro, an example of using it for an exploit, and a simple safe-read function that prevents the problem.

If you’d like to play around with read and #. to see how they act and what they return, here’s a function that acts as a REPL without the evaluation that you can use.

(defun rpl ()
  "A REPL without the evaluate. Type (quit) to exit."
  (princ "rpl> ")
  (let ((obj (read)))
    (prin1 obj)
    (terpri)
    (unless (equal obj '(quit))
      (rpl))))
Posted in Programming | Tagged | 1 Comment

Blogging With Emacs—Another Method

Today, someone on Hacker News pointed to an old post on Blogging With Emacs over at Work In Progress. Since some of my readers had expressed an interest in how I use Emacs and Org-mode to blog, I thought there might be some interest in seeing how someone else does the same thing.

The blog’s author takes a very different approach to blogging with Emacs than I do. First of all he uses Jekyll to generate a static site rather than using WordPress. That means that he doesn’t have the wonderful org2blog at his disposal but he solves that problem by using Org mode’s project publishing facility. I’ve never used that part of Org mode but it seems to be pretty nice. Bernt Hansen has a nice writeup on Publishing and Exporting with Org mode that you should read if you want to use this facility.

The Work In Progress post is interesting and shows how easily Emacs can handle different work flows to solve the same problem. My only quibble with the post is in the first sentence where he says, “I don’t want to waste time explaining why I use Emacs all day, every day, and eschew more traditional development environments.” I would argue that Emacs is the traditional editor—or at least that Emacs shares that honor with Vi. Yes, yes, I know all about ed but let’s be serious.

By the way, DJCB over at EMACS-FU also has an old post on using Org mode to blog, this time with Blogger. As always, there’s a lot to learn at EMACS-FU.

Posted in Blogging | Tagged , | Leave a comment

Working With Matching Lines In Emacs

This is another note to myself. For some reason I have a hard time remembering the handful of commands that do things to the lines in a buffer that match a regular expression. The table below are the ones that I know about but have a hard time remembering. If you have any similar commands that you find useful, leave a comment.

Command Alias Action
keep-lines delete-non-matching-lines Delete lines not matching regexp
flush-lines delete-matching-lines Delete lines matching regexp
how-many count-matches Count lines matching regexp
occur list-matching-lines Show lines matching regexp in another buffer
highlight-lines-matching-regexp hi-lock-line-face-buffer Highlight lines matching regexp

Update Phil reminds me of highlight-lines-matching-regexp, which I meant to include but forgot. (See what I mean?) He also mentions multi-occur (just like occur but it acts on multiple buffers) and multi-occur-in-matching-buffers (like multi-occur but you specify the buffers with a regexp), which he says he finds particularly useful.

Posted in General | Tagged | 8 Comments

The Popularity Of Programming Languages

TIOBE Software has published its list of programming language popularity for January 2012 and awarded Objective-C the TIOBE Programming Language Award for 2011. The award is given for the language with the most growth in market share for the year. Objective-C’s growth is driven, of course, by the popularity of the iPhone and iPad and their App Store infrastructure.

There aren’t a lot of surprises. Java is in first place (but losing share) and C is second and gaining share. Surprisingly, Python lost a significant amount of share and dropped from 5th place last year to 8th this year. Lisp has positive growth and maintained its 13th place from last year.

I don’t take the list too seriously but it does, as TIOBE says, give programmers a view of what languages are “important” at the moment and an indication of whether or not their skills need updating. Other than that the list is interesting on its own terms. Who would have thought, for instance, that Objective Pascal is in 11th place ahead of Ruby in 12th?

Posted in Programming | Leave a comment