sábado, 30 de enero de 2021

Shell Oneliner Compression

I'm back to Lisp. Now, Clojure. For real. But this post is about zsh

Part of my relearning of clojure is to read and watch everything clojure that crosses my path. And I discovered https://github.com/clojure-cookbook/clojure-cookbook , which seems super useful.  But I have too many open tabs and books already. So I thought I'd do a 'Tip Of The Day' like thing, and I'd pop a different page every day.

 Relevant files have .asciidoc  extension. And the ones with content start with a number. The other ones are index pages or headers/footers.

Here's a version of what I typed. and it worked (at the second try!)


random-clojure() {
   e $(ls ~/clojure-cookbook/**/*asciidoc(.) | awk -F/ '$NF ~ /^[0-9]/' | shuf -n1 | xargs realpath)
}

Quite a lot of things going on there, so I'm gonna explain the oneliner, because there's a lot of compression there.


  • e. e is my shellscript that opens files in emacs, moving to the line, and massaging input. in this case, it's the same as emacs, vi, $EDITOR.
  • ~/clojure-cookbook/**/*asciidoc(.). This one expands recursively files that contain 'asciidoc'. excluding directories. '**/' means 'recursively' in zsh, and the '(.)' globbing is for files only.
  • awk. '-F/' makes slash a field separator. Then, we pick the latest field, and we regex match it with '/^[0-9]/', beginning with a number. Cool!
  • shuf -n1 picks a line at random
  • xargs realpath. As we'll be running this function from all kinds of directories, expand the file path to an absolute path. as realpath asks for a parameter, we either nest the whole thing or use the xargs trick.

 

While writing this, I noticed that the whole thing is much simpler than that:

 e $(ls ~/clojure-cookbook/**/[0-9]*asciidoc(.)|shuf -n1| xargs realpath)

Master-ing git

 


miércoles, 23 de diciembre de 2020

org, roam, zettelkasten links.

Here we have a couple of links talking about the new wave on PIM, bookmarking, concept-linking.

It used to be the case that each one of us had a smilar but not quite the same system. But it seems that zettelkasten, and roam are getting some gravitational pull so people are joining them.

Usual links are org-roam, zettelkasten, and another link to zettelkasten-in-plain-org

martes, 8 de diciembre de 2020

N Good Javascript Tutorials

There's so much about javascript on the web that it's always been hard (for me) to find good resources.  But I think I found these N tutorials that are pretty good.

Good tutorials are hard to find and keeping the good links is important. I'm keeping also this docker tutorial. I just wish I've had it 3 years ago.

domingo, 29 de noviembre de 2020

A pipe inspector with tee

I've kept writing my scripting-field-guide, adding some more common pitfalls and cool tricks I've used in the last years. 

Something I started using not long time ago is the "tee >()" incantation. It's super cool to be able to branch off the output of a command to another command, and make some sort of tree. 

For now, there's this snippet I created to be able to inspect the contents of a pipe:

The default to the current timestamp isn't going to really work, as the string is evaluated only once, but I hope I'll get something more decent soon.

Another cool usage is when selecting something from a grep/fzf/dmenu and you want to do something with it like open that file, but also copy it to your clipboard.

The clearest example is my daily-zoom-selector, where I'm not just opening the room, but also copying it.

domingo, 15 de noviembre de 2020

Programming with Constraints

I've been following https://www.hillelwayne.com/ for some time and there's always something valuable to take to your daily life from each one of his posts.

Highest level is queuing theory and TLA+ stuff. But the couple of articles that touched home the most are his J lang article and the decision table patterns (and its intro from long time ago). 

He uses productivity tools that reasonate a lot with my way of tooling. AutoHotKey, editors, minilanguages and, Constraint Propagation Systems.

On CPS, he talks about MiniZinc and OR-Tools. For me, Oz/Mozart rang a bell (from CPM book), and cl:screamer. I don't know if Z3 overlaps with it, but worth mentioning too.

Now onto my usage/takeaway of this: I used screamer to create a magic-square some time ago, and last week I was thinking if I could use screamer, Prolog, or PiLog to solve the coin change problem.

Well, as just today, I read about this MiniZinc tool and when this watching the MiniZinc videos, it was clear that yes, it should be possible, and it might be worth to try the coin change there. Here's what I came with (using screamer):



I haven't figured out how to do the performance analysis of this, but I suspect it will be less efficient than the usual manual way, because the only 'fitness' function is v=, but it's a complete hit-or-miss. once the current factors add up to an already bigger num than out target, this algorithm will keep trying "what if I add one coin of 1cent?", "and what about 2?".  

IIRC, in CPM, there are some explanations of smarter CPS, but I'm not sure if they apply here, as there is only a single 'cut', which is the final objective function.