FPOO Chapter 1 in Elixir

Note: This article is part of a series working through the exercises in Brian Marick's "Functional Programming for the Object-Oriented Programmer", in Elixir instead of Clojure. The full notes and source code are on GitHub.

Exercise 3: add-squares

First up, we have add-squares . Let’s write a test…

My Elixir version takes a list rather than a variable number of arguments, because Erlang doesn’t do the varargs thing.

As for implementation…

Elixir is all about recursion and pattern matching.

(Aside: I understand why it’s there, but that darn comma after the method and before the do: when writing one-liners gets me every freakin’ time.)

Hmmm… so far FPOO has avoided introducing defun, instead defining functions by creating them anonymously and then assigning them to a name. I’m suddenly curious how easy this is in Elixir.

I realized halfway through writing this that it will lead to a compile error:

I remember now. There’s no way (that I’m aware of) to refer to an anonymous function within itself, so we can’t do recursive anonymous functions in Elixir.

(Aside: why is there no do after fn???)

OK, I guess I’ll stick to named functions. I can always take anonymous-style references to named functions with the capture ( &) operator, so hopefully this won’t get in the way.

Exercise 4: Bizarro-factorial

“Implement a bizarre version of factorial that uses neither iteration of recursion”. Specifically, the instructions say to use range and apply.

This is probably a gratuitous use of the pipeline operator ( |>), but I don’t care!

Note that FPOO specifies that I use only range and apply. The range operator ( ..) is the Elixir equivalent of range. On the other hand, apply doesn’t really translate well. Sure, Elixir has it. But Erlang (and thus Elixir) doesn’t have the concept of functions that take arbitrary numbers of arguments, the way * in lisp can yield the product of an arbitrary number of numbers:

Instead, Kernel.* is a strictly binary operator. So I have to cheat and use Enum.reduce, which is a recursive function under the covers.

Exercise 5: Various sequence functions

I’m going to quickly run through these just so I know what the Elixir equivalents are. I’ll use the Stream versions when they exist, since the Clojure versions demonstrated in the book all operate on potentially lazy sequences.

These translations were delightfully easy to do; in almost every case, the Elixir version of the Clojure function either had a) the same name; or b) the name of the equivalent operation in Ruby (e.g. remove becomes reject).

One thing that has stood out as I’ve worked through these is that the Stream module is a lot more limited than the Enum module. And from my brief experimentation, Enum functions are not lazy. So, for instance, this expression will never return, even though we are only trying to take the first three unique items from the stream:

Evidently Enum.uniq tries to convert the stream into a fixed collection, rather than returning a filtered stream in this case.

Since FPOO is already talking about laziness a lot (and all sequences seem to be treated as lazy and potentially infinite in Clojure), this may become a problem for later examples. More broadly, this doesn’t bode well for writing truly generic functions that can process any kind of collection, including streams, in Elixir. However, Elixir is still very young, and I suspect that the Stream library will grow with time.

Exercise 6: prefix-of?

Exercise 7: tails

Marick says “my solution is very much in the functional style”, and then goes on to offer some hints having to do with using Clojure’s range and map. I’m going to go out on a limb and say that the solution I came up with first is even more in the functional style, since it relies entirely on destructuring and recursion and doesn’t require any library calls at all.

Just for fun, here’s a more direct translation of Marick’s solution:

His solution is actually a bit more involved than this, because it involves mapping over both the range 0..count(seq) and a repeated list of the sequence itself. I’m guessing this is because he hasn’t yet explicitly introduced lambdas apart from the top-level function definitions.

If nothing else, this demonstrates that Clojure’s map can map over multiple sequences in parallel, which is kinda cool.