Blog

As Smart as a Smart Type – Theory

Recently I listened to a “Happy Path Programming” podcast episode about Smart Types. And that inspired me for this double post. The first part (this one) is about what a smart type is and why you should employ smart types in your code. The second part (yet to come, hopefully soon) is about the troublesome way I implemented an arithmetic smart type template in C++.

Continue reading “As Smart as a Smart Type – Theory”

Watchdogging the Watchdog

Maybe one of the main differences between embedded software/PC programmers and server/backend programmers is their attitude toward system resets. A server programmer will try as hard as possible to avoid any sort of system reboot since this could make a bad situation even worse. They would always strive for graceful service degradation (i.e. the system would not provide its full or top-level service) whenever forced to take action against unexpected or failing conditions.

Continue reading “Watchdogging the Watchdog”

Scala Job Interview – Reactive Programming

Here we are with the fourth installment in this Scala Job Interview series. The first was on general questions, the second on Scala language, and the third on functional programming.

Explain the actor model

In this case, the Actor Model has nothing to do with the Star System 🙂 I’m not sure where the term actor in “actor model” comes from. but, quite for sure, it has nothing to do with Hollywood, but more with the idea of something that “acts”, i.e. makes actions.

In fact, the Actor in the Actor Model is a processing entity that receives and reacts to messages. More precisely an Actor can only perform computation on the receipt of a message, when message processing is done, the actor just idly waits for the next incoming message.

Continue reading “Scala Job Interview – Reactive Programming”

Scala Job Interview – FP questions

Welcome to the third installment of the Scala Job Interview Questions series. This time I’ll try to answer functional programming questions, likely my score will be a bit less than the first two editions (General Questions and Language Questions) because I like Functional Programming, but I’m still a traditional programmer (imperial?) who studied Algebra at high school and uni and then consider Algebra as useful (for programmers) as a doorstop in a tent.

Young and foolish I was, but who could imagine, back then that to run the dance of the bits I would ever need monoids?

Let’s not waste other time in void introduction, and start with the questions and my answers.

Continue reading “Scala Job Interview – FP questions”

Scala Job Interview – Language Questions

This is the second installment of this series. The first post in the series gives you a bit more context. Anyway, this is my try to answer Scala Interview grade Questions I found in a blog post by Signify. You can find alternate answers here.

Language Questions

What is the difference between a var, a val and def?

All these keywords are for declaring something. var declares a variable, val declares a variable that cannot be re-assigned and def declares a function.

scala
var a = 3
val b = 3
def c = 3

In the above code, a is a variable as you can find in almost every language, you can increment it, re-assign it. Standard stuff.

b is a variable bound to value 3 forever? Attempting to change its value results in a compile-time error. c is a parameterless function that returns 3. Interestingly a, b and c evaluate to the integer 3. Also note that val just prevents reassignment, not value change. E.g. in the following code:

scala
class Foo {
  private var a: Int = 3
  def change( newA : Int ) : Unit = { a = newA }
}

val x = new Foo()
x.change(8)

There is no compile-time error because you are not changing the bound between x and the instance that references. You are just changing the instance.

Continue reading “Scala Job Interview – Language Questions”

Scala Job Interview Answers

Idling over Twitter I came across a post on Signify blog about a list of Scala Job Interview Questions. The post in fact referenced a GitHub repository. Now the post came with a pointer to ready-made answers, but I decided to take the exercise and try to reply myself. The intent was both to check my knowledge and to learn something new since I’m no Scala guru.

The post contained many questions, grouped by topic. So I prefer to split my answers along several posts to keep them short and manageable.

Let’s start with general questions.

Continue reading “Scala Job Interview Answers”

Is C++ Ready for Functional Programming? – Wrap Up

Wrap Up

Recently I had the questionable pleasure of watching “Cosmic Sin” courtesy of Netflix. The movie is a sci-fi show starring Bruce Willis. I was lured into wasting my time on it, by the trailer promising a space-operish feat and I took the presence of such a star playing in the movie for a warranty on quality. I couldn’t be farther from the truth.

The movie plays out confusingly, lacking a coherent script and motivated actors, pushing the watcher into an undefined state (not UB luckily). The helpless watcher, astonished by how bad a film could be, hopes until the very last for something interesting and entertaining to happen until the mixed relief-disbelief emotion of closing titles puts an end to the suffering.

When thinking about C++ and functional programming I have some of the feelings I had watching the movie. Before being persecuted by my friends from the C++ community I have to make clear that C++ is not that bad, although there are some similarities.

Continue reading “Is C++ Ready for Functional Programming? – Wrap Up”

Is C++ Ready for Functional Programming? – Types

Types

Featured image vector created by brgfx – www.freepik.com

Although functional programming can be done in a dynamically typed language (Lisp, the functional programming language forerunner, had no types), strong static type adds a very natural complement to functional programming.

From a mathematical point of view, a function sets a relationship between the set defined by the function argument (domain in math speaking) and the corresponding results (co-domain). Algebra studies exactly these relationships and may help the programmer in proving certain properties of the functions composing the programs.

Algebraic Data Types (ADT) are aggregated types in the algebra context. Two ADTs are of particular interest – sum types and product types. With sum and product referring to the resulting number of possible values (i.e. cardinality of sets).

Continue reading “Is C++ Ready for Functional Programming? – Types”

Is C++ Ready for Functional Programming? – Standard Library

Time to take a closer look to the standard library from the functional programming point of view.

In the first installment of this series, I stated that using a library to implement functional programming structures would not be an ideal solution, but as C language pioneered in the 70s, part of the language finds its proper location in a library.

C++ standard library has grown disorderly oversize during the years, so let’s have a look at what kind of support is available for those that want to use C++ with the functional paradigm.

Continue reading “Is C++ Ready for Functional Programming? – Standard Library”