Edd Mann Developer

Using For-Comprehensions in Scala

Scala can be a very deceptive language, type-inference is a very good example of this. Another less understood example that you will soon be welcome to upon closer exploration of the language is the ‘for-comprehension’. The first point I wish to highlight is that in Scala everything is an expression which returns a value, even if this be Unit (which is equivalent to nothing). This is a fundamental design principle of Scala which allows for productive use of its functional nature. In an imperative manner for example, we have become very accustom to maybe declaring and assigning a default value, only to reassign it with another if a condition is meet on the next line. Due to the expressive nature of the language this can instead be condensed into one-line, immutably and as a result less prone to error.

Simple Function Driven-Development

I recently had the chance to rewrite the backend of my personal website. I was surprised at how accustom I had become to using heavy-weight web frameworks (with plenty of accompanying depencies) in larger projects I am involved in, that I instead decided to do the complete opposite. As a result, I built a very simple single-page Markdown file-based blogging platform (inc. pagination, caching) that only takes a few moments to read. I find myself sometimes being blinded by the need to abstract everything with the Object-oriented philosophy, never taking the time to consider that in many cases it pays off to keep things simple. Simple, single purpose functions that can be used for multiple use-cases within your application is a very good mind-set to try and incorporate. In this post I wish to discuss a couple of the functions that I created to keep the file so simple.

Open external links in a new window using JavaScript

It is good practise to open external links in a new window, however, it can be a bit tedious having to remember to include 'target="_blank"', especially in Markdown. To get around this I have incorporated a simple raw JavaScript solution, which can be found below.

Twitter API v1.1 User Timeline JavaScript Solution

When I re-designed my site earlier this year I wanted to include the last couple of twitter interactions in the footer. Using v1.0 of the Twitter API this was a very simple process, giving access to a JSONP response with the publicly available tweets of a specified handle. This all changed in v1.1 with the introduction of required OAuth. Fortunately, I was able to find a work-around here which took advantage of the response made available from a widget you create. However, the returned tweets in this solution were already styled somewhat and I could not find a unminified version of the source. So in the end I decided to spend 45 mins last night implementing my own solution.

Solving the k-combinations problem in Scala

More often than not there are many different ways to solve a particular task. I found this trait present when coding up a solution to the k-combinations (N-choose-k) problem. A combination is the action of selecting a set amount of elements from a larger group, where order is not considered (dissimilar to a permutation). An example of a combination is in the cards you are dealt in a poker-hand, out of the possible 52 cards you are drawn 5 (52 choose 5). We are able to calculate the unique hand offerings in many different ways, allowing us to predict how likely it is for an individual card to be dealt. In this post I will be showing examples solving the 10 choose 2 problem.