Saturday, April 19, 2014

BASH: syntax error near unexpected token `('

After making some portability and readability improvements to shell-utils, I used BASH, sh, and dash to test it. While sh and dash were fine, BASH returned the error:

line 358: syntax error near unexpected token `('
line 358: `ls ()'

That is strange. BASH usually introduces shell portability issues because of the extra features it provides, so I would expect to have a problem with the other shells.

It turns out that BASH did a pretty good job with reporting the source of the error. Note that shell-utils redefines a few everyday commands as functions, to make them more verbose and secure (eg. ls becomes ls --color=auto, rm becomes rm -i, etc.). But usually those everyday commands are already defined as aliases in .bashrc. Aliases are evaluated before functions, and defining a function that has the same name as an alias is not allowed. And that's what BASH is trying to tell me in the error message. Commenting all aliases in .bashrc fixed the issue :)

Saturday, March 8, 2014

Joy of Coding 2014 - My impressions

I haven't been to a conference for years, but this year I decided to join Joy of Coding. And I don't regret it!

The conference started with a keynote by Dan North: "Accelerating Agile: hyper-performing without the hype". Dan described what he learnt about Agile while working in the trading domain. The most interesting advice that I kept from his presentation is that being a good programmer is not enough: What really makes a difference is to become a domain expert. For example, if you are working as a stock market programmer, your superior programming skills don't matter if you have no clue about what the numbers mean.

Next, I joined the "Let Me Graph That For You: An Introduction to Neo4j" workshop, by Ian Robinson. The first part of the workshop was an introduction to Graph Databases and Neo4j. At the second part we used Neo4j and its query language Cypher to create a few graphs and query them. I am impressed by how easy is to get started with Neo4j. I find its web interface very intuitive. We had a few questions for Ian (S = Sakis, I = Ian, O = Other conference participant):
  • S: Is there any relation between Neo4j and RDF?
  • I: Not really. In RDF you typically end up with more connections because everything is modelled as a triple. But there are libraries that can extract a Neo4j graph as RDF.
  • S: Are all the common graph algorithms (eg. SPF, BFS, Bellman-Ford, etc.) available?
  • I: Most of the well-known graph algorithms are available, and furthermore there is a Java API that is exposed and can be extended with your own algorithms.
  • S: Is Cypher case-sensitive?
  • I: Partly. The identifiers of a query are case-sensitive, but the rest parts of the query aren't.
  • O: Are there any cases where RDBMS should still be used instead of Graph Databases?
  • I: If you have tabular data and you want to focus on set theory operations (eg. union, intersection, etc.) an RDBMS is preferable.
 I'll definitely look more into Graph Databases and Neo4j.

The next keynote was "Contravariance is the Dual of Covariance", by Erik Meijer. I'm not very familiar with Reactive Programming and Rx, thus I couldn't follow everything. But at least I enjoyed the jokes and the funny examples that Erik used. Using Scala as a reference, he explained the meanings of covariance and contra-variance, and showed how they can be used to create reusable code.

The second workshop that I joined was "An Introduction to Actors and Concurrency", by Michel Rijnders and Matthijs Ooms. The first part of the workshop was basically an introduction to Erlang, thus nothing special if you are familiar with Prolog. The fun started at the second part, where we experienced how straightforward is to communicate asynchronously through the network using Erlang's actor model and message-passing primitives.

Last but not least was the keynote "The Tao, of the Joy, of Coding", by Dick Wall. This was by far the most inspiring keynote. Dick, using ancient Chinese philosophy as a reference, talked about many interesting topics, including programming honesty (saying "I don't understand this" and learning from your colleagues is a good thing), looking back as a programmer (eg. if you used your programming skills to find a cure for a disease you really changed the world), and getting a life (being proud about working until 2 AM is a very bad mentality).

All in all, a great conference that I will keep in my agenda every year!

Thursday, November 28, 2013

My first book review

During the last month(s) I participated in the review process of a book about Design Patterns in Python. I am happy to see that the book has been released. The title of it is Learning Python Design Patterns.

Reviewing a book is definitely not harder than writing one, but that doesn't mean that it's not challenging. The comments must be clear in context, useful, and not offensive. The process requires a considerable amount of time since all comments/remarks/notes must be backed by research and reliable references.

Nevertheless, I enjoyed the whole procedure which thanks to the people of packt publishing (packtpub) was flexible and clear. Plus, I'll receive a free hard-copy of the book that I reviewed and a copy of my favourite packtpub ebook. Not bad!

In the future I hope that I'll have the chance (and the time) to write my own book about a topic that I like.

Sunday, July 28, 2013

mbed USB voltmeter

USB communication with mbed is easy. That's because USB is the default protocol used to transfer programs from a computer to an mbed.

In this prototype I measure the analog value of a pot, I convert it to voltage, and finally I use USB to print it on my computer's screen. That's a simplistic voltage meter! Here's the schematic:


USB voltage meter schematic. Image courtesy of Bert Van Dam


And a picture of the actual circuit:

USB voltage meter circuit

Note that the pot is used just for testing purposes. It can actually be replaced by any other part. Just make sure that you're not trying to measure voltage > 3.3 volts, or your mbed is in danger!

The code

First the header file. It defines the USB connection, the analog input pin used to read the pot's value, a few constants, and a function that will be used for converting the pot's value from [0 - 1] to the voltage [0 - 3.3] volts.


And the source file. It contains the body of the conversion function, two terminal control commands for clearing the screen and moving to the top, and the commands for reading the pot's value and printing it (together with its voltage) to the terminal through the USB port.



Demo

I'm on a GNU/Linux system, where the mbed is identified as /dev/ttyACM0. A simple tool to use for connecting with it is screen (eg. sudo screen /dev/ttyACM0). Pardon me for the bad quality of the video, it's the first time that I used YouTube capture with my phone and it didn't work as well as I expected :)


 

Acknowledgements

Special thanks to my good friend and Arduino hacker efxa, for without his parts it wouldn't be possible to play with mbed.

References

Saturday, June 15, 2013

Numerical string sorting in Ruby

The problem: You have an unsorted array of strings (product codes, area codes, etc.) containing both letters and numbers (for example 'A28', 'A3', 'A1', etc.) and you want to sort them numerically (that is, 'A1', 'A2', ..., 'An'). Ruby offers a sort method, so let's see what it does...

Nope, that's not what I want. But no worries, enumerable is here to help me. Enumerable is a great mixin inspired by functional programming. Functional programming relies heavily on data transformations: The input is a data structure, we transform the data of the structure by applying one or more functions, and the output is a new data structure. In this particular example the input is an unsorted array of strings, I want to apply a function that will change their order, and the output will be a new array with the strings sorted numerically.

What is the data transformation in this case? A string is a data structure that can be treated as an array in most programming languages, and ruby is not an exception. Therefore the problem can be solved by using as a sorting key the numeric part of a string and treating that key as a number.

And we are done :)

Saturday, March 23, 2013

Why you should try Emacs if you are a Bash user

Are you a GNU Bash user but you are not using Emacs? Perhaps you should consider trying it. You will be amazed by the similarities...

I know, your IDE (Eclipse, QT, etc.) is superior and you are extremely productive with it. Why bother using an ancient editor like Emacs? I will not begin another flamewar by comparing popular IDEs with Emacs. Choose whatever you prefer during programming. But if you want to be productive while using a UNIX-like system, Emacs (and of course Vim but I can't speak for Vim since I'm an Emacs user) can really help you.

The truth is that if you are already productive in Bash (mandatory reading: Bash Shortcuts For Maximum Productivity) you are familiar with Emacs but you don't know it yet :) The default key bindings of Bash are also used by Emacs. Let me show you some examples (my apologies to screencast haters but that's the best way of demonstrating keyboard typing):


So, what does this screencast demonstrate? My goal is to use the find command for searching all files with the suffix .conf that live under the /etc directory. First fix: I mistyped .cofn instead of .conf and used the transpose character function (C-t)* to correct the mistake. Second fix: I forgot that find expects the searching path as its first argument and incorrectly provided it as the second. To fix that I first used C-p to access the most recent command in the command history (this functionality is not offered by Emacs as such). I then used the kill function (C-w) to "cut" backwards one word, C-a to go to the beginning of the line, M-f to go one word forward, and the yank function (C-y) to "paste" the most recently killed word (that is, the search path) at the expected place.

The moral of the story: When you learn about a new shortcut in Emacs, try it also in Bash. There's a great chance that its behavior will be very similar (if not exactly the same).

* In case you are not familiar with the Emacs terminology: C typically means the Ctrl keyboard button, and M means the Alt keyboard button. As an example M-f means pressing first Alt followed by f (move the prompt one word forward).

Friday, January 4, 2013

Data Structures and Algorithms 101 part two: Queues

Definition

Think of a queue as a line of people waiting to buy a ticket for an event (music concert, film, etc.).

Photo Credit: Bryan Bedder/Getty Images for Tribeca Film Festival

In the queue shown at the picture, a person who just arrives to buy a ticket goes to the left side (unless cheating) and a person who just bought a ticket leaves the queue from the right side (to avoid conflicts).

In queue terminology, adding a new item means enqueuing it to one side of the queue (either side is fine), while removing an item means dequeuing it from the opposite side of the queue.

Fundamental queue operations
The first item that enters a queue is the first item that exits the queue (first come-first served). For that reason, we call queues First In First Out (FIFO) data structures.

Usage and performance

Queues have many practical applications (search for "applications"). A simple example is printing documents. Documents are sent to the printer using a first come-first served policy (let's skip priorities for simplicity). If the printer is busy and we are trying to print a document, it will enqueue it and begin printing it only after it has finished with printing (dequeuing) all the existing documents.

Enqueuing and dequeuing items in a queue takes constant time - O(1). On the other hand, queues (like stacks) are not optimized for searching or adding/removing items from arbitrary positions. These operations take linear time - O(n).

Implementation

Like my stack implementation, I will use python and the built-in list data structure to implement a queue. Notice the similarities between stacks and queues. The only difference is the way the elements are accessed.

If you are not familiar with __iter__ and __next__ they are special functions for implementing iterators in python. In short, we can now say for i in mq where mq is an instance of myqueue. Quite convenient! I am going to use it in the sample problem.

Sample problem

Implement a song playlist for a jukebox/media player, etc. New songs are added to the back (left side) of the playlist. The next song to be played is picked from the front (right side) of the playlist.

Obviously that's not a "real" playlist since it doesn't track time so that can be automatically triggered after the end of a song. That's left as an exercise for you dear hacker. Enjoy!

References