CyberSpy

Rantings from a guy with way too much free time

Go Get Interfaced: Enums in Golang

Golang, Interfaces, and Enums

So here’s a nice golang idiom that I ran across years ago that I found generally useful. Golang, unlike languages like c doesn’t natively support enumerations. Instead, constants typically are used when creating a list of enumerations. But, go is a strongly-typed language, so we can do better than simply using constants - we can type our enumeration with the use of a type declaration.

type DogState uint

By defining a type for our enumeration, we can consistently pass and return typed-values among our functions operating on our enumeration. Okay, that’s all well and good, but what happens when we want to go between uint values and string representations of those values; a commonly used paradigm when working with enumerations. One approach would have us write mapping functions that might switch among the values to return a string. Or, when going from a string name of an enumeration to the uint value, we might use a map[string]. That’s a doable implementation, but there’s an easier, and more idiomatic way.

Continue reading

Gogo

Here we gogo!

Years ago, I wrote an interesting article that I thought might be worth re-posting (and revising) here on my blog. For a while I got into programming in golang and in the early going (pun-alert!), there were a lot of idioms that were not well understood by a noob. One of those paradigms was channels, go-routines, and signals used simultaneously. Taken separately, they are more easily understood. But when taken together, there can be some confusion. In this article, I address how to properly establish a signal-handler and a channel to terminate go-routines when the user interrupts the program with a signal such as ctrl-c or kill(1).

Continue reading
The soundcloud player can not be loaded with disabled JavaScript.
The following title is embedded here:
https://soundcloud.com/user-338167339/sabreras-etude-11-book-2

Sagreras Book 2, Lesson 11

2017-11-28 Music Etudes Rob Baruch

Etude Sagreras Lesson 11

Here’s my first cut at recording Sagreras Etude #11. Although the etude feels like it’s easy to play, recording it reveals an altogether unsatisfying experience when played back to the ear.

My goal is to show how the exercise evolves over time when practicing it with the intention of not just playing it as a site-reading piece, but rather practicing it with the intention of recording it with more attention beyond mere site-reading. Wish me luck!

Continue reading
The soundcloud player can not be loaded with disabled JavaScript.
The following title is embedded here:
https://soundcloud.com/user-338167339/bach

Julio Salvador Sagreras

2017-11-28 Music Guitar Rob Baruch

Études

Someone once asked…

How do you get to Carnegie Hall? Practice! Practice! Practice!

Well, I think the Amtrak is a better way for me to get there at this point in my life!

I’ve decided to spend time recording Sagreras etudes as I’m on a push to finish Book II and start Book III by March of 2018.

Stay tuned for

Recursion Revisited

Recursion, from more than one point-of-view.

A common programming idiom in computer science is solving a problem by self-reference, also known as recursion. In this post, we look at two different implementations of the same problem.

Solve a recursive problem in two different programming language paradigms

Let’s look at the solution to a simple problem, compute $f(x)=e^{x}$.

We will illustrate two separate solutions - one in a procedural language (python), and the other in a functional language (elixir).

Let’s start off with the functional language. Were does recursion come into play?

We define the function $f(x)=e^x$ as the infinite sum, $\text{ }f(x) = \sum_{n=0}^\infty{\frac{x^n}{n!}}$

In our solution below, we define two separate recursive functions, exp/2 and fac/1. What’s interesting to note here is how each of these functions has two separate definitions. This is an aspect of programming in elixir that elegantly uses pattern-matching to return different results depending upon the input. Our two functions nicely dovetail into the base-case and recursive case of a recursive algorithm.

For example, looking at exp/2, the first function definition returns 1 for any value of x (as indicated by the _ preceding the variable) and returns 1. This is the mathematical equivalent of $x^0=1\text{ for any }x$.

The second definition of exp/2 is the recursive case. for any value of $n\gt0$. Moreover, we define exp(x, n) as $\frac{e^x}{n!}$ + exp(x, n-1).

Similarly for the definition of fac/1 we see two definitions; one for $n=0$ and another for all values of $n\gt0$.

Continue reading
Newer posts