Stratus3D

A blog on software engineering by Trevor Brown

Inserting Code Samples Into PowerPoint

Displaying code in PowerPoint seems to be a pain. I came up with a simple way to get syntax highlighting in powerpoints using GitHub Gist. There may be better ways of doing this, but I found this way to be the most convenient for me on a Mac:

  1. Copy code from terminal (or whatever application you are viewing it in) and paste it into a GitHub Gist.
  2. Save gist and copy code out of the gist and into TextEdit (make sure TextEdit is in rich text mode before you paste in your code).
  3. Copy from TextEdit into your powerpoint and choose “keep formatting” from the paste icon that appears.

I found that creating a new text box before pasting in the code is a good idea as it allows you to control the font size of the code separate from everything else on the slide. It also allows you to set a background color for the code sample.

I know there are other ways of doing this out there, but I thought I would post my method as it’s very simple and uses GitHub’s syntax highlighting. If you have a better way of doing this I would love to hear about it!

Working With HTML in Vim

I discovered another Vim command this week that made working with raw HTML much easier and figured I would share it.

The command sequence is dit. Focus the cursor over an HTML tag and in normal mode type dit. The contents of the tag will be deleted and the empty tag will remain. The cursor will be positioned at the end of the first tag. In order to insert more content all you need to do is type i and begin entering the new contents of the tag. This command comes in very handy when having to make lots of manual changes to an HTML document.

Example: Before: Title

After:

When working with raw HTML it’s helpful to have the markup indented so you can easily see parent/child and sibling relationships. In your .vimrc you will want to have a line turning on the file type indent on: filetype indent on. Whenever you open up a html file or paste html into an empty buffer make sure the file type is set by running :set ft in normal mode. Vim should print filetype=html as the result. If it doesn’t you want to correct the file type before proceeding. Then select all the markup and simply type = in normal mode. The HTML should be nicely indented with the settings you specified in your .vimrc or filetype file. Note that on large files this can take a while (sometimes almost a minute).

If you have any other vim commands that you frequently use when working with Vim I would love to hear about them!

Currying and Partial Application in Elixir

I often forget what currying and partial application are and how they are used, so I am writing this blog post to solidify these concepts in my memory.

###Currying

Currying is when a single function with multiple arguments is converted into multiple functions, each expecting one of the arguments of the original function. For example, we could curry the :erlang.atom_to_binary/2 function in Elixir like this:

> binary_fn = fn(encoding) ->
>     fn(atom) ->
>         :erlang.atom_to_binary(atom, encoding)
>     end
> end
#Function<6.80484245 in :erl_eval.expr/5>

> binary_fn.(:utf8).(:bar)
"bar"

We now have a function that takes the first argument, and returns another function that expects the second. This is currying. As you can imagine there are many possible uses for this. Including partial application…

###Partial Application

Partial application is when you curry a function and pass fewer arguments than the original function expected. Continuing with our atom_to_binary example, this would be partial application:

> atom_to_utf8 = binary_fn.(:utf8)
#Function<6.80484245 in :erl_eval.expr/5>

We now have provide one of the two arguments required to invoke atom_to_binary/2. We receive a new function which we store in atom_to_utf8 variable. This function expects the second argument of atom_to_binary/2 to be passed in. When this function is invoked, it calls atom_to_binary/2 with the values :utf8 (which was passed in earlier) and :foo.

> atom_to_utf8.(:foo)
"foo"

This is partial application. Partial application relies on currying to produce functions that contain references to the arguments that have been provided(via closures) and expect the remaining arguments of the original function.

Currying allows us to break a function down into many functions, and partial application uses currying to create functions with some of the arguments already applied. Both of these concepts are extremely useful. We can, as we did above, create new functions on the fly by providing a subset of the arguments, and we can also pass around the curried functions, providing the arguments they require as those arguments become available. There are quite a few possible uses for currying. I may write another post about currying and closures in Elixir sometime in the future but this is all for today.