Scala can be a very deceptive language, as type inference is a good example of this.
Another less understood example that you will soon be introduced 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 accustomed to declaring and assigning a default value, only to reassign it with another if a condition is met 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 is less prone to error.
I recently had the chance to rewrite the backend of my personal website.
I was surprised at how accustomed I had become to using heavy-weight web frameworks (with plenty of accompanying dependencies) 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 are 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.
It is good practise to open external links in a new window.
However, it can be a bit tedious 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.
When I redesigned 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 workaround 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 an unminified version of the source.
So in the end I decided to spend 45 minutes last night implementing my own solution.
More often than not there are many different ways to solve a particular task.
I encountered this trait when coding a solution to the k-combinations (N-choose-k) problem.
A combination is the action of selecting a set number of elements from a larger group, where order is not considered (unlike a permutation).
An example of a combination is in the cards you are dealt in a poker hand - from the possible 52 cards, you are dealt 5 (52 choose 5).
We can 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 show examples of solving the 10 choose 2 problem.