layout: post
title: “Using functions as building blocks”
description: “Function composition and mini-languages make code more readable”
nav: why-use-fsharp
seriesId: “Why use F#?”
seriesOrder: 11

categories: [Conciseness, Functions]

A well-known principle of good design is to create a set of basic operations and then combine these building blocks in various ways to build up more complex behaviors. In object-oriented languages, this goal gives rise to a number of implementation approaches such as “fluent interfaces”, “strategy pattern”, “decorator pattern”, and so on. In F#, they are all done the same way, via function composition.

Let’s start with a simple example using integers. Say that we have created some basic functions to do arithmetic:

  1. // building blocks
  2. let add2 x = x + 2
  3. let mult3 x = x * 3
  4. let square x = x * x
  5. // test
  6. [1..10] |> List.map add2 |> printfn "%A"
  7. [1..10] |> List.map mult3 |> printfn "%A"
  8. [1..10] |> List.map square |> printfn "%A"

Now we want to create new functions that build on these:

  1. // new composed functions
  2. let add2ThenMult3 = add2 >> mult3
  3. let mult3ThenSquare = mult3 >> square

The “>>“ operator is the composition operator. It means: do the first function, and then do the second.

Note how concise this way of combining functions is. There are no parameters, types or other irrelevant noise.

To be sure, the examples could also have been written less concisely and more explicitly as:

  1. let add2ThenMult3 x = mult3 (add2 x)
  2. let mult3ThenSquare x = square (mult3 x)

But this more explicit style is also a bit more cluttered:

  • In the explicit style, the x parameter and the parentheses must be added, even though they don’t add to the meaning of the code.
  • And in the explicit style, the functions are written back-to-front from the order they are applied. In my example of add2ThenMult3 I want to add 2 first, and then multiply. The add2 >> mult3 syntax makes this visually clearer than mult3(add2 x).

Now let’s test these compositions:

  1. // test
  2. add2ThenMult3 5
  3. mult3ThenSquare 5
  4. [1..10] |> List.map add2ThenMult3 |> printfn "%A"
  5. [1..10] |> List.map mult3ThenSquare |> printfn "%A"

Extending existing functions

Now say that we want to decorate these existing functions with some logging behavior. We can compose these as well, to make a new function with the logging built in.

  1. // helper functions;
  2. let logMsg msg x = printf "%s%i" msg x; x //without linefeed
  3. let logMsgN msg x = printfn "%s%i" msg x; x //with linefeed
  4. // new composed function with new improved logging!
  5. let mult3ThenSquareLogged =
  6. logMsg "before="
  7. >> mult3
  8. >> logMsg " after mult3="
  9. >> square
  10. >> logMsgN " result="
  11. // test
  12. mult3ThenSquareLogged 5
  13. [1..10] |> List.map mult3ThenSquareLogged //apply to a whole list

Our new function, mult3ThenSquareLogged, has an ugly name, but it is easy to use and nicely hides the complexity of the functions that went into it. You can see that if you define your building block functions well, this composition of functions can be a powerful way to get new functionality.

But wait, there’s more! Functions are first class entities in F#, and can be acted on by any other F# code. Here is an example of using the composition operator to collapse a list of functions into a single operation.

  1. let listOfFunctions = [
  2. mult3;
  3. square;
  4. add2;
  5. logMsgN "result=";
  6. ]
  7. // compose all functions in the list into a single one
  8. let allFunctions = List.reduce (>>) listOfFunctions
  9. //test
  10. allFunctions 5

Mini languages

Domain-specific languages (DSLs) are well recognized as a technique to create more readable and concise code. The functional approach is very well suited for this.

If you need to, you can go the route of having a full “external” DSL with its own lexer, parser, and so on, and there are various toolsets for F# that make this quite straightforward.

But in many cases, it is easier to stay within the syntax of F#, and just design a set of “verbs” and “nouns” that encapsulate the behavior we want.

The ability to create new types concisely and then match against them makes it very easy to set up fluent interfaces quickly. For example, here is a little function that calculates dates using a simple vocabulary. Note that two new enum-style types are defined just for this one function.

  1. // set up the vocabulary
  2. type DateScale = Hour | Hours | Day | Days | Week | Weeks
  3. type DateDirection = Ago | Hence
  4. // define a function that matches on the vocabulary
  5. let getDate interval scale direction =
  6. let absHours = match scale with
  7. | Hour | Hours -> 1 * interval
  8. | Day | Days -> 24 * interval
  9. | Week | Weeks -> 24 * 7 * interval
  10. let signedHours = match direction with
  11. | Ago -> -1 * absHours
  12. | Hence -> absHours
  13. System.DateTime.Now.AddHours(float signedHours)
  14. // test some examples
  15. let example1 = getDate 5 Days Ago
  16. let example2 = getDate 1 Hour Hence
  17. // the C# equivalent would probably be more like this:
  18. // getDate().Interval(5).Days().Ago()
  19. // getDate().Interval(1).Hour().Hence()

The example above only has one “verb”, using lots of types for the “nouns”.

The following example demonstrates how you might build the functional equivalent of a fluent interface with many “verbs”.

Say that we are creating a drawing program with various shapes. Each shape has a color, size, label and action to be performed when clicked, and we want a fluent interface to configure each shape.

Here is an example of what a simple method chain for a fluent interface in C# might look like:

  1. FluentShape.Default
  2. .SetColor("red")
  3. .SetLabel("box")
  4. .OnClick( s => Console.Write("clicked") );

Now the concept of “fluent interfaces” and “method chaining” is really only relevant for object-oriented design. In a functional language like F#, the nearest equivalent would be the use of the pipeline operator to chain a set of functions together.

Let’s start with the underlying Shape type:

  1. // create an underlying type
  2. type FluentShape = {
  3. label : string;
  4. color : string;
  5. onClick : FluentShape->FluentShape // a function type
  6. }

We’ll add some basic functions:

  1. let defaultShape =
  2. {label=""; color=""; onClick=fun shape->shape}
  3. let click shape =
  4. shape.onClick shape
  5. let display shape =
  6. printfn "My label=%s and my color=%s" shape.label shape.color
  7. shape //return same shape

For “method chaining” to work, every function should return an object that can be used next in the chain. So you will see that the “display“ function returns the shape, rather than nothing.

Next we create some helper functions which we expose as the “mini-language”, and will be used as building blocks by the users of the language.

  1. let setLabel label shape =
  2. {shape with FluentShape.label = label}
  3. let setColor color shape =
  4. {shape with FluentShape.color = color}
  5. //add a click action to what is already there
  6. let appendClickAction action shape =
  7. {shape with FluentShape.onClick = shape.onClick >> action}

Notice that appendClickAction takes a function as a parameter and composes it with the existing click action. As you start getting deeper into the functional approach to reuse, you start seeing many more “higher order functions” like this, that is, functions that act on other functions. Combining functions like this is one of the keys to understanding the functional way of programming.

Now as a user of this “mini-language”, I can compose the base helper functions into more complex functions of my own, creating my own function library. (In C# this kind of thing might be done using extension methods.)

  1. // Compose two "base" functions to make a compound function.
  2. let setRedBox = setColor "red" >> setLabel "box"
  3. // Create another function by composing with previous function.
  4. // It overrides the color value but leaves the label alone.
  5. let setBlueBox = setRedBox >> setColor "blue"
  6. // Make a special case of appendClickAction
  7. let changeColorOnClick color = appendClickAction (setColor color)

I can then combine these functions together to create objects with the desired behavior.

  1. //setup some test values
  2. let redBox = defaultShape |> setRedBox
  3. let blueBox = defaultShape |> setBlueBox
  4. // create a shape that changes color when clicked
  5. redBox
  6. |> display
  7. |> changeColorOnClick "green"
  8. |> click
  9. |> display // new version after the click
  10. // create a shape that changes label and color when clicked
  11. blueBox
  12. |> display
  13. |> appendClickAction (setLabel "box2" >> setColor "green")
  14. |> click
  15. |> display // new version after the click

In the second case, I actually pass two functions to appendClickAction, but I compose them into one first. This kind of thing is trivial to do with a well structured functional library, but it is quite hard to do in C# without having lambdas within lambdas.

Here is a more complex example. We will create a function “showRainbow“ that, for each color in the rainbow, sets the color and displays the shape.

  1. let rainbow =
  2. ["red";"orange";"yellow";"green";"blue";"indigo";"violet"]
  3. let showRainbow =
  4. let setColorAndDisplay color = setColor color >> display
  5. rainbow
  6. |> List.map setColorAndDisplay
  7. |> List.reduce (>>)
  8. // test the showRainbow function
  9. defaultShape |> showRainbow

Notice that the functions are getting more complex, but the amount of code is still quite small. One reason for this is that the function parameters can often be ignored when doing function composition, which reduces visual clutter. For example, the “showRainbow“ function does take a shape as a parameter, but it is not explicitly shown! This elision of parameters is called “point-free” style and will be discussed further in the “thinking functionally” series