The Metallica document is gone. Surprised? I definetly was.
The behavior may seem a bit contrived, but I actually encountered it while trying to optimize a produciton database. This example just boils it down to something trivial to reproduce. I should mention that if the index is created without the sparse option, the results are correct. The sparse option allows saving space on the index itself, by only creating an entry for documents that have the field. A non-sparse index, creates a record for all documents and sets the value to null.
In my opinion, the above-described behavior is awful. It is up to the database engine to decide which index to use. A sparse index may be useful in less queries than a non-sparse index. However, my expectations of indexes is that they are all about performance and trading off disk space and insert time for query time. The existance of an index should never change the result set for the same query and dataset.
In order to teach myself Elixir, I have been working my way through Exercism.io, which is a set of practice coding exercises with mentorship from the community. All exercises have the tests written for you and it’s up to the user to write a passing implementation.
Being new to Elixir and functional programming, the exercises are a great way for me to learn about syntax, idiomatic code and functional programming patterns. One of exercises consists of re-implementing common list operations, like count, map and reduce.
Implementing Count With Recursion
The test that the implementation must pass looks like this:
defmoduleListOpsTestdoaliasListOps,as:LuseExUnit.Case,async:truetest"count of empty list"doassertL.count()==0endtest"count of normal list"doassertL.count([1,3,5,7])==4endtest"count of huge list"doassertL.count(Enum.to_list(1..1_000_000))==1_000_000endend
First thing of note: count/21 is defined twice. This is part of the language provided functionality. In Java, method overloading required a different number of parameters which is how the dispatching picked the correct method at runtime. In Ruby, there can’t exist to method definitions in the same scope. In Elixir, the correct function is called at run-time depending on which pattern is matched.
On our first test, when L.count() is called, the count/1 function matches, because it only has one parameters. That function calls count(0, ). This will match the first count/2 definition, because it is being passed with an empty list. (Any acc will match). That in turn returns acc, which is 0, making the test pass.
For the second test, count/1 is matched, which ends up calling count(0, [1,3,5,7]). That call, matches the second count/2 definition, because it matches a list that is not empty2. That function call will call recursively, adding 1 to the accumulator each call, until the list is empty and the accumulator is returned.
The same trick as before is used here, where matching on an empty list returns the accumulator. When a list has at list one member, the function is called for that member and reduce/3 is called with the tail of the list recursively.
With reduce/3 in place, the count/1 implementation becomes much simpler:
The exercise has some other operations as well: map, reverse, filter, append and concat. I learned a lot working on the solutions and started to get a feel for functional programming. If you are learning a new language, I would recommend trying Exercism.io. It currently supports 23 languages!
In Elixir, when referring to functions, it is customary to add / and the arity to the name. foo/2 refers to the function foo defined with 2 parameters.↩
Elixir includes matching a list to it’s head and tail with the [head|tail] syntax. The _ signals that the parameter will not be used.↩
Based on a talk at Strange Loop 2014, this post was eye-opening. Although it’s supposed to be about Apache Samza, most of the talk is devoted to talking about databases in general and what they are good at: Keeping global state, replication, secondary indexing, caching, and materialized views. This high-level view provided me with a lot of new perspective of how to think of databases. The many illustrations in the article are beautiful. Please go and read.
The legendary Chad Fowler makes the case that empathy is a skill that everyone will benefit from developing further. Provides great list of why that is. Most importantly, he also details how to practice.
Git has often been criticized for having an inconsistent interface and leaking unneeded abstractions to the user. Some of that criticism is warranted. Nonetheless, git is one of my favorite programs. I use it hundreds of times throughout the day, always on the command-line, complemented by tig, the ncurses client for git. This article talks about the internals of git: How it stores data on disk for commits, trees, objects, tags, branches, etc. It is well written, well organized and a pleasure to read. If you read this guide, it will make it easier for you to interact with git because you will understand it’s intrenals. However, I think you should read it because it shows how great functionality can be achieved with software with minimal dependencies and using only the local filesystem as a data store.
I recently came across two interesting posts about the Phoenix framework: A benchmark comparing the performance against Rails and a how-to for creating a simple JSON API. I was interested by the performance characteristics described in both articles. What really got my attention though was the syntax: The feel for it was very ruby-like.
Phoenix is written in Elixir, a relatively young functional programming language design by Jose Valim, committer in the Rails Core team. Elixir compiles to byte-code that runs on the Erlang virtual machine, which lists as it’s main features scalability and fault-tolerance.
The main Elixir website has the best getting started guide I have ever read. I started off by installing Elixir, which on my Mac was as simple as $ brew install elixir. That took care of the Erlang VM and whatever else it needed. A few moments later I had a working elixir installation, ready to go.
The guide is written in clear and concise manner, introducing the reader to the language, the syntax, the tools and the concepts in a gradual manner. I am not familiar with functional programming, other than what I have heard described in a few podcast. Even so, I was able to follow along quickly and start exploring. The guide also includes a more advanced section covering mix and OTP.
mix is the main tool that Elixir uses for compiling code, resolving and getting dependencies, running tests, etc. Think of it as the equivalent of make or rake. OTP stands for “Open Telecom Platform” and is a set of libraries included with Erlang that allows developers to build fault-tolerant, distributed applications.
mix is a pleasure to work with: Be it creating a new project, installing dependencies or running your test, you go through mix. (As opposed to rails, rake, bundler)
ExUnit, the included testing framework is familiar to anyone having used any XUnit framework before. The error messages are helpful by default. It includes a great feature where examples inside the documentation of a module can be executed as tests directly. I was very happy to see that the guide introduces tests and TDD early on. It shows the values of the Elixir community, load and clear.
Concurrency is prominent. In fact, in order to have any kind of state at all, you need to use separate Elixir Processes, which are like light-weight threads, not system processes. Messages are sent between processes. I like the idea of having to think about concurrency early, as opposed than doing later in the application life-cycle. Elixir processes are allowed to fail fast on errors. It’s not a big deal, since they are under Supervisors that will just restart them if they fail.
Pattern-matching at first seems weird. They grow on you. It allows the programmer to deal with different cases easily and separate them out into their own functions, foregoing a lot of conditionals. Defining functions many times, with different guard clauses is great. It’s hard to describe (because I don’t know the correct language yet), but here is my take on the Fibonacci sequence:
In conclusion: I have barely gotten my feet wet, but have been impressed with what I have seen. I was not expecting this level of polish from a new language. I am itching to find a personal project to write in Elixir!
Probably one of the most well-known books among rubyists, “The Ruby Way” by Hal Fulton with André Arko, has now been updated and released in its third edition. The first part of the book is dedicated to the language itself and covers syntax, semantics, some comparison to other languages and specific issues, like garbage collection, that developers are well served to know when writing ruby.
We do find OOP to be a useful tool and a meaningful way of thinking about problems; we do not claim that it cures cancer
The majority of the book is divided into sections that deal with specific task that a developer may encounter. From basics like working with String, numerical calculations and Enumerable collections to more advanced techniques like Threads and Concurrency, Metaprogramming, Network Programming and Distributed Ruby. Each chapter has plenty of code examples and thorough explanations.
I expect my copy to get plenty of used as my programming takes me to unknown or forgotten parts of ruby.