Journal

2018-06-25 A simple hack with the comma operator and console.log

As we all know, console.log is the best debugging tool there is. I know, I know, it’s primitive, it’s slow (you have to insert it and run the program again, though that can be sped up) and it’s uncool to use it. Never mind.

There is one place, however, where it’s not easy to put a console.log (or console.dir, or similar stuff): arrow functions. Assume we have this code:

[1, 2, 3, 4, 5].map(x => x**2)

Assume that I want to insert a console.log(x) before returning the square of x in the arrow function above. Well, I have to first convert it to a “normal” function, and then put my stuff there:

[1, 2, 3, 4, 5].map(function(x) {
    console.log(x);
    return x**2;
})

Or do I?

Remember that JavaScript’s syntax is based on C, and we have the comma operator. Also, console.log is not a statement, but a function, so console.log(...) is an expression. Hence, we can do this:

[1, 2, 3, 4, 5].map(x => (console.log(x), x**2))

Notice that the parentheses around the compound expression console.log(x), x**2 are mandatory, since otherwise the x**2 is apparently interpreted as a second argument to Array.map. Unfortunately, the docs are rather, say, concise on this (lame pun intended). JavScript syntax could be better, I suppose. Still, I find this technique pretty useful.

Interestingly, it has a distinct functional feel to me. While Lisp is not (contrary to the common wisdom) a functional language, it has one important thing that makes functional programming so pleasant: in Lisp, there are no statements, and the program is a series of expressions. (That makes hacks like the ternary expression completely redundant – there is no “conditional statement” in Lisp at all.) The very existence of statements is IMHO one of the most annoying features of JavaScript and many other languages. Happily, console.log is a function (and the comma operator is essentially a progn), making this trick possible.

CategoryEnglish, CategoryBlog, CategoryJavaScript

Comments on this page

2018-06-16 ivy-use-selectable-prompt

It’s been a long-standing problem in Ivy that entering a string which was not on the list of candidates, but was a substring of one of the candidates, was non-trivial. You had to remember to press C-M-j instead of RET, and that requires some finger acrobatics.

Well, it’s no longer a problem. In one of the recent versions of Ivy, a new variable called ivy-use-selectable-prompt was introduced. With it, you can just press the up arrow when on the first candidate to select the contents of the prompt line. Very nice!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

2018-06-10 Git diff in Eshell

I try to use Eshell more, as part of a general drive towards moving everything into Emacs. One thing that annoyed me a bit was issuing a git diff or something similar in Eshell. Invariably, I received a WARNING: terminal is not fully functional.

I am not sure, but I suspect that the reason is that Eshell does not really like less, which seems to be the default Git pager.

Now there exist quite a lot of ways to overcome this issue. One is having

(setenv "PAGER" "cat")

in your init.el. This is, however, an all-or-nothing type solution, since it affects all programs started from Emacs. (Though maybe this is the right way?) I have discovered another, truly remarkable solution of this problem which this blog post is too short to contain;-), and hence it must wait for another time.

Problem solved – at least partially. The diff is not colored at all! Again, this is easy to overcome: just put

[color]
	ui = always

in your .git/config and you’re done.

(Please let me know if this has any undesirable side-effects.)

Of course, I don’t use git diff in Eshell very often, since there is Magit – but sometimes I want to e.g. show someone what it looks like to use Git, and then I start with the command line.

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryEshell

Comments on this page

More...