viernes, 8 de abril de 2016

My mail signatures: 2006 - 2016

2006

--
Vi is clearly superior to emacs, since "vi" has only two characters
(and two keystrokes), while "emacs" has five.  (Randy C. Ford)

2016

--
"Emacs saves you time when you work, and takes it back when you play with it"

sábado, 2 de abril de 2016

TIL: 2 extra comment-dwim features

Today I just learned (by accident) a couple of tricks related to`comment-dwim'. The emacs command is bound to M-; , and it has different behaviours  depending on the line, region, and contents of the selection.

Appart from the ovbious behaviour we all know, today I found that:

  • Called with universal argument in an empty line, it adds ARG comment markers in the line. try C-u M-; in an empty line.
  • In a line with comments, c-u m-; removes the comments. that means deleting the comment, NOT UNCOMMENTING but removing all commentted text and the comment marker itself.

viernes, 1 de abril de 2016

definitely not planar

Once in a while, you find something enlightening, that changes your perspective. One of them is geometric progressions. How they grow so fast, and how dense graphs (k-graphs) deal with this kind of progressions al the time. But I digress...

Irreal (a very active blogger in the emacs scene) has been posting about a series of posts from John Kitchin, about Hy. All this referred by Karl Voit.  It's kinda funny when for a given John's post, there is an Irreal's counterpart, that links to John's, and probably to older Irreal's posts of the same topic.  This reminds me somehow of classes pointing to superclasses and metaclasses (In smalltalk). 

Here's how I imagine it.


Also, let me add myself to the chain, and also mention that planet emacs is logging all this activity, so probably this picture is an oversimplification of the situation.  It's probably more similar (in complexity) to the real smalltalk class-metaclass structure:


When adding tweets and retweets that refer one to another (and probably with cycles), it's kinda funny how things lay together. OOP (smalltalk one) is definitely a nice inspiration for many things, way beyond the explicit implementation side of the things, but in a systems aproach. Ah, here's where you can learn about the smalltalk(pharo's) class/metaclass beauty.

domingo, 20 de marzo de 2016

console tools for structured trext

The lingua franca of unix is plain text and raw files. That's why we can do so many things using its standard tools and pipes as combinators.

But there are some special formats of files which have a concrete structure, and we can extract meaning from its structure. For example CSV.

JQ

jq is like sed for json files. It allows you to parse, grep, replace, match and join json files. For example, /tmp/issues.json being the output of a github repo issues:

cat /tmp/issues.json | jq '.[] |select(.labels[].name | in({"S-zendesk": 12})) | {labels: [.labels[].name | match("^A-.*") | .string] }'

Select issues which have label S-zendesk, and pick the label A-* of it. To know which are the areas that have zendesk issues.

Xmlstarlet

Xmlstarlet is the same as jq but for xml. Allows you to print and match fields from xmls. For an xml file like this:
We can list title of files and id's with:

cat files.xml | xmlstarlet sel  -T -t -m 'files/file' -v 'title' -o ' => ' -v 'id' -n


dateutils  

I just discovered dateutils. But it seems a very good companion for tail, or just to do standalone date calculations.

 pbmtools

Part of netpbm. compare and operate on images from the commandline. Sucks less (apparently) than imagemagick.

Praising MJD

Mark Jason Dominus is one of those guys that is worth following everywhere in the net.

  • Web: His site is quite old school. It reminds me of c2, or norvig.com, that have really old look but is full of insights and no-bullshit content. Also, loads instantly.
  • Blog: Wide range of topics. From systems programming to functional programming, haskell, monads, git, books, alien codes, mathematics.... 
  • Book: Higher Order Perl is a great book on functional programming. It uses Perl, but the concepts are really language agnostic. It clearly follows a similar approach to SICP, maybe with a more practical approach.  Higher Order Perl is on my top 10 list of tech books. Top5 if we exclude lisp books.
  • Talks: He excels at giving talks also. There are very nice talks about. 

viernes, 18 de marzo de 2016

programming quizzes with org-babel

When trying to write some proof of concept, or an algorithm in isolation (or programming quizes), I usually create a new file (foo.lua, for example), and run it with my patched version of shell-execute that replaces '%' by the current file name .

But sometimes I want to give more context or write the code as a story, or a question-answer dialogue.  In those occasions, org-babel is the way to go.
This piece of elisp enables a bunch of languages to be evaluated in org file blocks:

(org-babel-do-load-languages
 'org-babel-load-languages
 '((ditaa . t)
   (tcl . t)
   (ruby . t)
   (sqlite . t)
   (clojure . t)
   (lisp . t)
   (http . t)
   (sql . t)))


Now, in the org file itself, place this kind of block:
#+BEGIN_SRC sqlite :db pieces-and-providers.sqlite
#+END_SRC

Now, when pressing c-c c-c inside a block, a new block called RESULTS will be created underneath with the result of the eval'ed block. 

 There are many other options on exporting org-blocks and other options for fine tunning how results are shown, or formatted. For me, this is a nice way to have my experiments documented, or leave exercises for my future me to solve. For example, I copied most of the sql exercises wikibook and created this repo with the stories/exercises as org files so I can try them in an interactive way.

viernes, 12 de febrero de 2016

Test spies with Lua metatables

Dabbling with Lua metatables, I tried to write a minimal testing library that does not impose you any funny 'describe(...)' or 'it(....)' nesting, and one can just organise the tests as he pleases.

What

I called it spacesuit.lua as it wraps your functions and gives you minimal support to write tests (assertions and spies) in the wild. If you need your tests to be TAP compliant, runnable from any platform, and a well known solution, I can recommend busted, but for me, I tried to keep it minimal so I can put it in my bag and run the files I need from my console, using some silly bash/zsh script using globbing. no need for luarocks, native compilation of lfs or anything.

Apart from providing some sugar for assert_equal (which I'll probably delete in favour of plain assert(foo==42)), it provides:

  • assert_raise(fun): runs the function and asserts an error is thrown during its execution.
  • spy(fun): returns a proxy function (it's a table with __call in its metatble) that logs all the calls (both actual parmeters and results). The usage is quite simple:
  • s = spy(function(x) return x+1 end)
    s(42)
    s(45)
    -- inspect the log
    s.called_with(42) -- true
    s.called_with(42).and_returns_with(43) -- true
    s.called_with(43) -- error
    s.called_with(42).and_returns_with(44) -- error
    
    --number of times called
    s.called() -- true
    s.called(1) -- error
    s.called(2) -- true
    
  • make_spy(Module, 'fun_name'): hijacks Module.fun_name so that you can track executions of functions inside modules. It provides a clean() method that releases the hijacking.
  • stub(Module, 'fun_name', fun): hijacks Module.fun_name and substitutes it for 'fun'.
The whole ungolfed code is (without tests) about 100 lines of lua, which is very impresive for a non-batteries included language.

How


The how is what is interesting. When you make a spy out of a function, spacesuit creates a func table which responds to called_with. called_with  returns a table with and_returns_with key which will do the matching. It's quite a nice usage of lexical scope juggling.

For the hijacking part, I wanted to wrap everything into another table which would have the 'clean' method, and use __call to call the spy table (that would cascade to its __call entry in its metatable, but lua doesn't let you chain __call's. So you have to write the outer one as a function that calls the inner one (and then the __call is run).