## Posts

Showing posts from March, 2017

### Partial functions, currying and composition

I've been working through Functional Programming in Scala by Chiusano and Bjarnason lately, and it's been a really awesome book. This morning I worked on a few exercises on partial function application, currying and composition that I think demonstrate how powerful Scala can be. I just wanted to paste my solutions here and their translations to Python (3.6), because it's really cool and I'd love feedback on them.

Partial Function Application
First is partial function application. In Scala my solution to this exercise looks as follows.
object HOF_Testing { def partial[A, B, C](a: A, f: (A, B) => C): B => C = (b: B) => f(a, b) def main(args: Array[String]): Unit = { // Test this partial function method on an anonymous function val x = 5 val y = 6 println("** Partial function application: " + partial[Int, Int, Int](x, (u, v) => u + v)(y)) } } // ** Partial function application: 11 In Python we can achieve the same result…

### Fixing 'ImportError: No module named _ssl' while building Python from source in Ubuntu

tl;dr just apt install libssl-dev.

This is just a reminder in case I ever come across this issue again.

Upon switching my Laptop's OS back to Ubuntu 16.04 today, I re-installed Python 3.6. Then I tried to pip install a module, and I found that the library ssl had a problem. The solution was, quite simply, to install libssl-dev (apparently this doesn't ship with Ubuntu), then rebuild Python 3.6.

### Automate content inclusion after a certain date in LaTeX

While writing my resume in $$\LaTeX$$ there were several events I wanted to add, but only after a certain date. Also, I don't want to wait until that date to include them; I want to type them up now, while I have time.

After a little research, I came across pgfcalendar. This page was probably the most helpful in describing its contents/functionality. It allows you to perform some action after, before, or on a certain date, in addition to a bunch of other things.

I've written the following macro utilizing this package. As an example date, we'll use PyCon (because I'm really hoping to be able to attend).
\usepackage{pgfkeys} \usepackage{pgfcalendar} \def\PyConDate{2017-05-17} % YYYY-MM-DD \newcommand\ifdateisbeyond[3]{% \pgfcalendarifdate{\year-\month-\day}{at least=#1} {#2} % if true {#3} % if false } We may use this command as follows.

### Iterators and Iterables in Python 3

Lately I've been examining the difference between iterators and iterables in Python more closely. There's a subtle but distinct difference between the two that's easy to overlook (or simply ignore); however, most every time you've typed a for-loop, such as for i in [1, 2, 3, 4, 5]: print(i), you've used both an iterable and an iterator.

According to the Python 3.6 documentation, iterator types define special methods __iter__ and __next__. Together these form something in Python called the iterator protocol. Moreover, __iter__ typically just returns self, so we only have to worry about writing a definition for __next__. While collections.abc already contains an abstract Iterator base class (see e.g. collections.abc.Iterator.__abstractmethods__), instead of importing it I'm going to write it as follows.
from abc import abstractmethod from typing import Any class Iterator: # c.f. PEP 234, PEP 322 @property @abstractmethod def _state(self) -> …

### RX Algorithm in C

Earlier this year I was trying to learn more C by working on a project formerly left over from drone club. After a couple weeks of toiling with memory leaks (Valgrind is an awesome tool), I finally came up with an implementation of the RX algorithm that's almost fast enough to be useful on a drone for basic anomaly detection.

The code is hosted on GitHub, and I welcome suggestions for improvements as C is definitely not my first language. I suspect there are many memory improvements that could be made; nevertheless, it can compute the Mahalanobis metric on one million RGB vectors in about 1/5 of a second.

As a test, I created the following video from data collected by Terry Sims. Even 4K frames only take about 2s to compute on my modest laptop.
The hardest part was probably figuring out how to read images from disk with libpng. I mostly read through the example.c file to understand how this library works.

I understand now that file specifications/formats are extremely intricate …