Opening Sourcegraph from Emacs

Sourcegraph is super useful when browsing through code and dependencies, there is currently no plugin for emacs, but it is pretty easy to configure git-link to do the trick as @sqs pointed out to me on Twitter.

This is all the configuration to make git-link work as expected, and it should be pretty generic.

Emacs Markdown list DWIM

Markdown has multiple types of lists, and in my mind checking items from those list means different things depending on the type.

- [ ] this item is open
- [x] this item is close
- this item is also open
- ~~this item is also close~~

For checklists I want the box to be checked when I toggle the item for, others I want the item to be crossed out via strike-through. All of those actions already exist in Emacs Markdown-Mode, but they are different depending on the list, I want Emacs to do what I mean no matter the list so I added this

(defun coder--markdown-toogle-list-item-dwim ()
    "Toogle the current list item depending on the type do the right thing.

1. When it is not a markdown list, ignore
2. When the list is a checklist indicated by [ ] check the checkbox
3. When the list is a normal list, strike-through the current item
4. When the item already has strike-through applied, un-strike it"
    (interactive)
    (save-match-data
      (save-excursion
        (when-let ((bounds (markdown-cur-list-item-bounds)))
          ;; move to the beginning of the item after the list marker
          (goto-char (cl-first bounds))
          (forward-char (cl-fourth bounds))

          (cond ((looking-at "\\[[\s-xX]\\]") (markdown-toggle-gfm-checkbox))
                ((thing-at-point-looking-at markdown-regex-strike-through) (markdown-insert-strike-through))
                (t (progn
                     (set-mark (point))
                     (end-of-line)
                     (activate-mark)
                     ;; remove trailing whitespace from line first, this
                     ;; otherwise breaks strikethrough rendering
                     (delete-trailing-whitespace)
                     (markdown-insert-strike-through))))))))

Happy markdown editing!

Lauching Emacsclient via Spotlight

When installing Emacs via

$ brew cask install emacs

it automatically installs emacsclient, but this needs to be launched via the terminal. When running the Emacs.app from Applications it will launch a new instance of emacs every time. Most of the time when I want to quickly edit something I tend to prefer to launch emacsclient to create a new window or frame in the existing running instance. Since I launch almost all my applications via Spotlight the simple way to achieve this is to create a script with the .command and use this instead.

$ cat ec.command 
#!/bin/bash

EMACSCLIENT=/usr/local/bin/emacsclient
$EMACSCLIENT -n -c -a '' $*

With this in place configure the script to be run via the terminal of your choice, in my case iTerm, by right clicking on the file and Get Info.

ec.command GetInfo

Thats it now Spotlight will execute the script when typing ec and hitting Enter on the top hit, assuming it is indexed.

My experience in getting my AWS Solutions Architect Professional certification

Disclaimer: While studying for the exam I was working Amazon as a Software Engineer, not part of AWS but Prime Video. Given that I largely didn’t have any special AWS resources or knowledge. I did however work work with AWS technologies for quite some time especially CloudFormation, Dynamo and EC2.

Here we go, a couple of weeks ago I finally got around to taking my AWS Certified Solutions Architect – Professional exam and therefor I am now certified.

giphy.gif

I decided to take the exam mostly because I was looking for a reason to invest the time to really get to know what services are available and get a good overview of AWS in general (at least at this given point in time since AWS changes very quickly after all). Given this background I have to say the certification is well worth it, it is a great motivation to get studying and is a good way to guide the learning towards the more relevant (less buzzwords more value) parts of AWS.

My most reliable resource has been A Cloud Guru, which provide a quite comprehensive course with exercises, which make studying less dry but also are actually valuable in getting to know AWS better. I don’t think they are required to pass the exam, but given my motivation of getting to know AWS better they most certainly were helpful! Besides A Cloud Guru, I mostly ended up reading white-papers, and watching reinvent videos. Personally I found the white-papers mostly useful to pickup on the terms used, I don’t think the contain much new knowledge outside of that. I think as with most AWS exams the main enemy is time, with each question allowing for on average 2 minutes. It comes down to knowing what you know well and plow through those question quickly to have time for the ones which are less clear and require more analysis. While taking the exam I flagged multiple questions I was unsure about, but didn’t have time to go back, it was a close one for me.

Overall would I take it again: Yes, Is it worth it? Depending on the motivation for me it was. Thanks to A Cloud Guru for putting together a worth while course.

Grep in sections of a file

Processing files which are obviously organized in sections, chunks whatever you want to call it, happens and trying to find elements in it in an AND relationship is my most common use case. Sadly grep does not seem to have a nice way of processing based on a separator, but purely goes by line, so the easy way around this is to join all the lines in a chunk and grep in the result. Think input like this

SECTION
foo=bar
xyz=111
baz=blob
SECTION
foo=1
bat=man
baz=3

Some AWK magic does the joining trick,

awk '/SECTION/ {printf "\n%s\n",$0;next} {printf "%s ",$0} END {print "\n"}' INPUT_FILE

And now

awk '/SECTION/ {printf "\n%s\n",$0;next} {printf "%s ",$0} END {print "\n"}' INPUT_FILE | \
grep 'foo=bar' | \
grep 'baz=blob'

Gives the matching section.

Solving Sudoku with Clojure core.logic

Recently I had some fun solving Sudoku using Clojure.core.logic. Afterwards figuring out that it would actually be the example on the Wiki, but hey fun non the less and my solution actually is very close to the Wiki one, especially after cleaning it up.

I find core.logic fascinating as it allows to express logic problems in a very straight forward fashion, and given I will not learn Prolog anytime soon, it get me to some understanding of the ideas.

Solving a puzzle like Sudoku basically breaks down towards the following steps:

  1. Break down the puzzle according to the rules, in case of Sudoku this means defining rows, columns and squares
  2. Defining the variables for the pieces to be computed, in my case this means replacing input zeros with core.logic lvars.
  3. Define the rules, in case of Sudoku there are only, constraint on the numbers to 1-9, constraint on rows, columns and squares to have each number show up only once.
(defn solve
  "Takes a sudoku as a vector, with all empty cells set to 0 and returns
  a solved sudoku as a vector.

  (solve
    [0 0 0 2 6 0 7 0 1
     6 8 0 0 7 0 0 9 0
     1 9 0 0 0 4 5 0 0
     8 2 0 1 0 0 0 4 0
     0 0 4 6 0 2 9 0 0
     0 5 0 0 0 3 0 2 8
     0 0 9 3 0 0 0 7 4
     0 4 0 0 5 0 0 3 6
     7 0 3 0 1 8 0 0 0])
  "
  [sudoku]
  (let [board (vec (map #(if (zero? %) (lvar) %) sudoku))
        rows (indexed-sub-board board row-indexes)
        cols (indexed-sub-board board column-indexes)
        squares (indexed-sub-board board square-indexes)]

    (first
     (run 1 [q]
       (== q board)
       ;; only 1 - 9 are allowed
       (everyg #(fd/in % (fd/interval 1 9)) board)

       ;; every number can appear only once per row
       (everyg fd/distinct rows)
       ;; every number can appear only once per column
       (everyg fd/distinct cols)
       ;; every number can appear only once per square
       (everyg fd/distinct squares)))))

Overall the whole solution ended up very straight forward, the only problem I faced was the very macro heavy definition of core.logic, making debugging not as straight forward and I ended up reading a log of library code to figure out how things were supposed to work. But I guess this also comes from my general lack of knowledge in this specific kind of programming, and therefore having to read up on very basic concepts along the way.

The full code is available on github.

A Lisp in Clojure? How useless! But fun non the less.

Having found a nice post on how to built a simple lisp like (scheme like) language (How to Write a (Lisp) Interpreter (in Python)) I was wondering what would this look like in Clojure. Writing a lisp in Clojure sounded very useless and fun so here we go the result in on Github.

A couple interesting observations:

Since Clojure lends itself to using immutable datastructures the inital implementation of the evaluation seemed a little complex as it needs to receive the environment along with the next token. But as soon as I was implementing function calls this suddenly made the stacking of environments trivial. No complex chain maps etc. needed, nice!

clojure.core.match is a very nice little lib, I wish it was part of core Clojure by default. I had first implemented a giant cond statement, match made it much more concise and robust, as it not only asserts the matching symbol but also the parameters to it.

(defn lclj-eval
  "Eval a expression X in the context of environment ENV, defaults to
  base-env."
  ([x] (lclj-eval x lclj-base-env))
  ([x env]
   (match [x]
          [(_ :guard symbol?)] {:result ((keyword x) env) :env env}
          [(_ :guard number?)] {:result x :env env}
          [['if _ _ _]] (lclj-eval (if-branch x env) env)
          [['define _ _]] {:env (define-in-env x env)}
          [['quote _]] {:env env :result (-> x rest first)}
          [['lambda _ _]] {:env env :result (lclj-fn x env)}
          :else (lclj-fn-call x env))))

Obviously this can all be done better but it was a fun (I guess in total) afternoon for sure, having never written a lisp interpreter before.