When developing in JavaScript I think it is beneficial to have short feedback cycles. For instance, every time I change or write a unit test I want to see the results immediately. The same applies for changes in the application itself.
I came across a handy tool named xdotool. It simulates keyboard or mouse presses and sends them to a window.
The following bash script sends a reload command to a window named Jasmine Spec Runner every time a file is changed in the directories src and spec.
Both xdotool and inotifywait work on Linux based systems. However, equivalent tools or ports may exist for Windows or Mac OS X.
Recently, I discovered that I needed a way to accumulate results of function calls. My first attempt utilized a recursive function that accumulated the results in a parameter. However, after looking at my solution I noticed that the same behavior can be achieved with Clojure’s built-in function reduce.
To illustrate this point, I’ll use the following example: An ordered list of dates is processed until a specified date. To each matching date a function is applied. In addition, the results of the function calls are accumulated and returned.
My first attempt is shown below:
The process-dates-accumulator iterates over the range of dates. For each date more recent than until the function f is applied. In this example get-month is used. Furthermore, the accumulation is done in results.
Accumulating With reduce
Being a functional language Clojure embraces the use of generic combinator functions like map and reduce.
Consider this implementation of the example described above:
First, the initial list of dates is filtered to include all candidates. Afterwards, reduce accumulates the results of applying the function f to each candidate.
Conclusion
As shown above standard functions like reduce offers powerful abstractions. The advantage of using these functions is that those are highly optimized. Moreover, they can be applied to a wide range of situations.
It seems that when the concept of recursion is introduced, a lot of people (including myself) have a hard time to understand it. Although, it is a powerful technique that is used in important algorithms. In addition, recursion often leads to simple and straightforward solutions.
I think one way of making things more clear is to visualize the process that is generated by a recursive algorithm.
Fibonacci Numbers
The classical example for introducing recursion is Fibonacci numbers. Consider this implementation in Python:
What is interesting about this algorithm is that it generates a process with a tree structure. This is illustrated in the following.
Just by looking at the source code it is hard to reason about its generated process. In contrast, I think the illustration helps.
The picture also shows why this naive implementation is quite slow. A large number of function calls are computed multiple times.
Merge Sort
Another classical recursive algorithm is Merge sort. It is a divide and conquer algorithm that recursively breaks the input down into smaller problems and merges it back in sorted order. The algorithm is implemented like this (full implementation is available here):
The generated process of sorting 9, 1, 4, 6, 3, 2, 7, 8 is shown below.
Constructing All Permutations
The problem of constructing all permutations of a string can be solved recursively. For instance, this implementation:
produces the following process for “abc”.
Moreover, the resulting permutations of “abc” reside at the leaves of the process tree: “abc”, “acb”, “bac”, “bca”, “cab” and “cba”.
Conclusion
Of course, there are different strategies that help with understanding recursive processes. I think visualizing the process of a recursive algorithm is very helpful. Especially in the beginning.
A feature of git I think is useful sometimes is the ability to utilize the ssh protocol. Therefore, it is possible to work with a remote repository by using an existing ssh configuration.
Welcome on my blog! This is the first post on this page.
Which kind of content can you expect to find here? I am planning to write about programming, technology, ideas and things in general I am interested in.