A Type of Happiness Inc.
Tags: fsharp, fsadvent, elves, wishes
Published: 2020-12-15 07:38:56

---
title: A Type of Happiness Inc.
published: true
description: 
tags: #fsharp #FsAdvent #elves #wishes
cover_image: https://images.unsplash.com/photo-1576843018966-8c9e0d9d64cd?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80
---

# Prologue

Once upon a time, an elf nameSanta knows his business, isn't hed Harry joined _A Type of Happiness Inc._ that helped Santa to gather children's wishes and prepare gifts based on the letters.

Everything was manual, laborious, and not easy to wrap an elvish head around - warehouses, packages, colorful ribbons, and tons of glitter.

Fortunately for the company, Harry spoke not only elvish language but also he knew F#. His manager took him to Santa and told a story about how using software might revolutionize the entire business. The old man didn't hesitate, because he was tired of all the paper letters coming to the headquarters. "You need to keep it for the next five years in the guarded warehouse because that's what the law states".

# What if I told you, we can be (f)sharp

Harry thought he will create multi Kubernetes cluster microservices architecture, driven by events, powered by Kafka, and many other crucial pieces to run a business in 2020. Before jumping into the data lakes, he decided to start with the most important thing - the domain.

He had multiple Santa Storming sessions and he transferred as much knowledge as he could cognitively acquire at that moment.

_"Children have wishes, they are getting gifts. Some children might have impolite behavior that needs to be verified."_ - Santa said.

```fsharp
module ChristmasDomain =
        type Gift = Gift of string

        type Wish = Wish of string

        type ImpoliteBehavior = ImpoliteBehavior of string 
```

Harry was typing quickly while listening carefully.

_"Yesterday on Hacker News I saw an interesting article about some kind of machinery learning that might be really helpful. Along with a child wish, maybe it could suggest some other wishes."_ - Santa spoke with a spark in his eyes.

Harry said to himself _"I thought children know best what they would like to get, but well, Santa knows his business, doesn't he?"_. He updated his precious code with the following:

```fsharp
module ChristmasDomain =
    open System
        type Gift = Gift of string

        type Wish = Wish of string

        type ImpoliteBehavior = ImpoliteBehavior of string

        type MachineLearningWish = {
            Accuracy: float
            Precision: float
            Wish: Wish
        }
```

_"We need to create a gift preparation job. This gets tracked with its unique identifier. We need to know when it was created. Then we need to verify a child's behavior against The Great Registry of Children Behavior."_ - Santa started describing how the main process looks like.

Harry suggested that wishes might be sent as HTTP requests by some fancy device sold separately to the shops. Santa said that for him it's just a detail of how they will get all the wishes. But he approved the idea and said that now that's the moment when all the machinery learning should kick in, giving the most trendy wishes among all the children. Harry started building his mental model precisely.

_"Typically, now elves recommend the gift for a kid, but having this learning thingy, we need to require both types of wishes."_ - Santa said contemplatively while thinking how the heck this machinery works.

Harry thought about `Status` type describing all possible cases that every fellow fsharper would do. In his imagination, he saw all those ifs and match expressions and begun sweating a little bit.

_"That's all"_, Santa said. _"Based on the elvish decision if there is a recommended gift or not, this job should be completed and your work is over. I will print all completed jobs on paper and do my work."_ - Santa continued.

Harry tried to elaborate on why printing all completed jobs isn't the best idea and how they could utilize a reactive stream on one of his Kafka topics to send a completed job on his email, but Santa didn't listen. He just wanted to get his work done.

# Let's start objectively

Our (f)sharp elf continued working hard on his domain model:

```fsharp
type Status = 
            private 
            | Created
            | VerifiedKidPolite
            | VerifiedKidImpolite
            | WithMachineLearningWishes
            | WithChildWish
            | WithCombinedWishes
            | WithRecommendedGift
            | CompletedJob
```
He expressed all possible scenarios Santa mentioned during one of their collaborative Santa Storming events. It looked really good.

## Happiness made easy

Harry continued exploring the domain through his fingers. He experienced an almost mythical, god-like feeling while he was transferring ideas into a bunch of characters displayed in his favorite IDE with [Ionide](https://github.com/ionide) installed, dark mode turned on:

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
```

_"That's what Dependency Injection is baby!"_ - thought Harry.

Then he started encoding the first part of the process - verification whether a child was behaving politely or not.

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite
```

_"Backward pipe operator. I knew I could use it somewhere."_ - Harry was feeling so smart when we observed this thought.

He fulfilled the requirements partially. There should be also a possibility to give Santa information about a child who was behaving impolitely.

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None
            let mutable impoliteBehavior: ImpoliteBehavior array = Array.empty<ImpoliteBehavior>

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite

            member this.VerifyIncorrect(impoliteBehaviors': ImpoliteBehavior array) = 
                impoliteBehavior <- impoliteBehaviors'
                status <- VerifiedKidImpolite
```

## Bring your wishlist

_"What's next on our list?"_ - Harry asked himself in his mind as if someone was sitting next to him. The most important part of the process was missing - tracking the wishes.

The friendly (f)sharp elf continued his internal dialogue between him and himself - _"What is the purpose of life if you don't have big dreams?"_.

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None
            let mutable impoliteBehavior: ImpoliteBehavior array = Array.empty<ImpoliteBehavior>
            let mutable childWish: Wish option = None

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite

            member this.VerifyIncorrect(impoliteBehaviors': ImpoliteBehavior array) = 
                impoliteBehavior <- impoliteBehaviors'
                status <- VerifiedKidImpolite

            member this.AddChildWish(wish: Wish) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add a child wish to the job when child was impolite"
                    else 
                        if status = WithMachineLearningWishes
                            then status <- WithCombinedWishes
                            else status <- WithChildWish
                        childWish <- Some wish
```
This looked correct. Whenever a child wasn't polite, a job cannot be further processed. Harry took into account state with cool machine learning wishes. He impatiently looked forward to working with all the great and so massively interesting new pieces of related technology.

## Wishes Recommendation Engine

Is there anything more precious than children's dreams? _"Yes, there is"_ - said Harry swimming in his thoughts with all the monumental technological components he can potentially work with. _"AI-powered wishlist generation, online learning engine that uses a multi-agent reinforcement learning tool, producing wishes with high accuracy, based on all the good deeds of the given child"_. Harry almost started crying but then he realized he has some code to write!

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None
            let mutable impoliteBehavior: ImpoliteBehavior array = Array.empty<ImpoliteBehavior>
            let mutable childWish: Wish option = None
            let mutable machineLearningWishes = Array.empty<MachineLearningWish>

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite

            member this.VerifyIncorrect(impoliteBehaviors': ImpoliteBehavior array) = 
                impoliteBehavior <- impoliteBehaviors'
                status <- VerifiedKidImpolite

            member this.AddChildWish(wish: Wish) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add a child wish to the job when child was impolite"
                    else 
                        if status = WithMachineLearningWishes
                            then status <- WithCombinedWishes
                            else status <- WishChildWish
                        childWish <- Some wish

            member this.AddMachineLearningWish(wishes: MachineLearningWish array) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add ML wishes to a job when child was impolite"
                    else 
                        if status = WishChildWish
                            then status <- WithCombinedWishes
                            else status <- WithMachineLearningWishes
                        machineLearningWishes <- wishes
```

## A dream come true

It looked like Harry handled almost all the parts of the gift preparation process! Now the funniest part - gifts. Who doesn't like them?

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None
            let mutable impoliteBehavior: ImpoliteBehavior array = Array.empty<ImpoliteBehavior>
            let mutable childWish: Wish option = None
            let mutable machineLearningWishes = Array.empty<MachineLearningWish>
            let mutable recommendedGift: Gift option = None

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite

            member this.VerifyIncorrect(impoliteBehaviors': ImpoliteBehavior array) = 
                impoliteBehavior <- impoliteBehaviors'
                status <- VerifiedKidImpolite

            member this.AddChildWish(wish: Wish) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add a child wish to the job when child was impolite"
                    else 
                        if status = WithMachineLearningWishes
                            then status <- WithCombinedWishes
                            else status <- WishChildWish
                        childWish <- Some wish

            member this.AddMachineLearningWish(wishes: MachineLearningWish array) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add ML wishes to a job when child was impolite"
                    else 
                        if status = WishChildWish
                            then status <- WithCombinedWishes
                            else status <- WithMachineLearningWishes
                        machineLearningWishes <- wishes

            member this.RecommendGift(gift: Gift) =
                if status = WithCombinedWishes 
                    then 
                        recommendedGift <- Some gift
                        status <- WithRecommendedGift
                    else failwith "Cannot recommend gift when one of the type of wishes is missing"
```

All the datasets flowing through the Kubernetes processing pipelines, petabytes of wishlists, toys, and hidden dreams of every child believing in Santa - Harry couldn't wait to write all those yaml files, oh boy! _"But let's quickly finish the domain part"_ he thought.

## Elvish job done, Santa's job starts

Finally, the last piece of the puzzle! Only one step was stopping Harry from working with all the HTTP handlers, controllers, databases, queues, and other interesting stuff. He recalled what Santa said about the job completeness - only when there are both types of wishes or when there is a recommended gift.

```fsharp
        type GiftPreparationJob(now: unit -> DateTime, guid: unit -> Guid) =
            let mutable status: Status = Created
            let id: Guid = guid()
            let createdAt: DateTime = now()
            let mutable verifiedAt: DateTime option = None
            let mutable impoliteBehavior: ImpoliteBehavior array = Array.empty<ImpoliteBehavior>
            let mutable childWish: Wish option = None
            let mutable machineLearningWishes = Array.empty<MachineLearningWish>
            let mutable recommendedGift: Gift option = None

            member this.VerifyCorrect() = 
                verifiedAt <- Some <| now()
                status <- VerifiedKidPolite

            member this.VerifyIncorrect(impoliteBehaviors': ImpoliteBehavior array) = 
                impoliteBehavior <- impoliteBehaviors'
                status <- VerifiedKidImpolite

            member this.AddChildWish(wish: Wish) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add a child wish to the job when child was impolite"
                    else 
                        if status = WithMachineLearningWishes
                            then status <- WithCombinedWishes
                            else status <- WishChildWish
                        childWish <- Some wish

            member this.AddMachineLearningWish(wishes: MachineLearningWish array) =
                if status = VerifiedKidImpolite
                    then failwith "Cannot add ML wishes to a job when child was impolite"
                    else 
                        if status = WishChildWish
                            then status <- WithCombinedWishes
                            else status <- WithMachineLearningWishes
                        machineLearningWishes <- wishes

            member this.RecommendGift(gift: Gift) =
                if status = WithCombinedWishes 
                    then 
                        recommendedGift <- Some gift
                        status <- WithRecommendedGift
                    else failwith "Cannot recommend gift when one of the type of wishes is missing"

            member this.Complete() = 
                if status = WithCombinedWishes or status = WithRecommendedGift
                    then status <- CompletedJob
                    else failwith "Cannot complete job without a recommended gift or without both type of wishes"
```
Looks like he nailed it. _"Kafka, now"_ - Harry smiled.


## Reality hits elves hard
The next day was full of meetings on how to deploy Harry's multi-cluster architecture to various regions taking into account important decisions that each engineer needs to make. Harry was so busy attending to the meetings so he tried to teach another friendly elf, Barry, how to proceed with the code he wrote.

Turned out that for Harry it was a day full of meeting, but not a day full of glory. Barry was from the warehousing department and didn't know about the gift preparation process so he misused all the methods exposed. He got a bunch of exceptions here and there, for example not knowing that he cannot recommend a gift if there are no wishes for a given job! 

## Elves do mistakes, too
Harry and Barry thought that there must be some other way of enchanting the domain into the code so that future elvish newcomers won't be able to break anything, or at least it will click for them relatively quickly.

Both elves agreed that Harry's idea of starting with all possible cases was good enough, but maybe they could prevent impossible scenarios. They started describing the process from the beginning.

## New start, new hope
Our friendly elves described the first step in the process:
```fsharp
module GiftPreparationJob =
            let create (now: unit -> DateTime, guid: unit -> Guid): Created = 
{ CreatedAt = now(); Id = guid() }
```
_"Easy peasy"_ - Barry thought. Compiler warned fellow elves with red squiggly lines that they are missing some types. Barry satisfied digital friend with the following code:
```fsharp
type Created = private {
            Id: Guid
            CreatedAt: DateTime
        }
```
_"Great Barry! Now let's verify if the child was polite or not."_ Harry suggested.
```fsharp
let verifyPolite (job: Created): VerifiedKidPolite = 
                { CreatedAt = job.CreatedAt; VerifiedAt = DateTime.UtcNow; Id = job.Id }

let verifyImpolite (impoliteBehaviors: ImpoliteBehavior array) (job: Created): VerifiedKidImpolite = 
                { CreatedAt = job.CreatedAt; VerifiedAt = DateTime.UtcNow; Id = job.Id; ImpoliteBehaviors = impoliteBehaviors }
```
Again, the mighty compiler threw errors reminding missing types. Elves can type really quickly and moments after each side of the keyboard was satisfied:
```fsharp
type VerifiedKidPolite = private {
            Id: Guid
            CreatedAt: DateTime
            VerifiedAt: DateTime
        }

        type VerifiedKidImpolite = private {
            Id: Guid
            CreatedAt: DateTime
            VerifiedAt: DateTime
            ImpoliteBehaviors: ImpoliteBehavior array
        }
```

## A type of wish
As Harry did previously, elves started with the child's wish part of the process. They thought about how to represent that child wish might be added to either the verified kid's politeness state or to state with machine learning wishes already added. Without any hesitation, they decided to use the least resistance approach and chose `Choice` type. Soon they landed with such process description:
```fsharp
let withChildWish (childWish: Wish) (job: Choice<VerifiedKidPolite, MachineLearningWishes>): Choice<ChildWish, WishesCombined> =
                match job with
                | Choice1Of2 verifiedCorrectJob -> Choice1Of2 { ChildWish = childWish; Id = verifiedCorrectJob.Id }
                | Choice2Of2 machineLearningResults -> Choice2Of2 { MachineLearningWishes = machineLearningResults.Wishes; ChildWish = childWish; Id = machineLearningResults.Id }
```
Now the compiler got really angry. `MachineLearningWishes`, `ChildWish`, and `WishesCombined` - those types were missing, at least that's what the compiler was suggesting.

Barry got tired so they decided that Harry will pick up where he left. Harry immediately started filling the gaps so that all red squiggles disappear.

```fsharp
type MachineLearningWishes = private {
            Id: Guid
            Wishes: MachineLearningWish array
        }

        type ChildWish = private {
            Id: Guid
            ChildWish: Wish
        }

        type private Wishes = {
            MachineLearningWishes: MachineLearningWish array
            ChildWish: Wish
        }

        type WishesCombined = private {
            Id: Guid
            Wishes: Wishes
        }
```

Tranquility. The mighty compiler is silent, meaning they could move forward. What almost naturally emerged was another part of the process. Harry noticed that it's so close to fulfilling another Santa's requirement - machine learning generated wishes.
```fsharp
            let withMachineLearningWishes (job: Choice<VerifiedKidPolite, ChildWish>) (wishes: MachineLearningWish array): Choice<WishesCombined, MachineLearningWishes> =
                match job with
                | Choice1Of2 verifiedKidPoliteJob -> Choice2Of2 { Wishes = wishes; Id = verifiedKidPoliteJob.Id }
                | Choice2Of2 childWishJob-> Choice1Of2 { Wishes = { MachineLearningWishes = wishes; ChildWish = childWishJob.ChildWish }; Id = childWishJob.Id }

```
_"It's a good choice to use `Choice`"_ - said Harry and both elves laughed so much. The time has come and now our cheerful, laughing elves could recommend some gifts!
```fsharp
            let recommendGift (job: WishesCombined) (gift: Gift): RecommendedGift =
                { Wishes = job.Wishes; RecommendedGift = gift; Id = job.Id }
```
A single red line of truth appeared - Harry rapidly added missing type:
```fsharp
        type RecommendedGift = private {
            Id: Guid
            RecommendedGift: Gift
            Wishes: Wishes
        }
```
And this action silenced the unbending compiler.


They were so close. Harry started dreaming about independently deployable microservices orbiting around deep learning models, but then Barry reminded him what's the most important thing right now - _"The domain"_ - Harry whispered. Elves needed to finish the process by completing the gift preparation job.
```fsharp
      let complete (job: Choice<WishesCombined, RecommendedGift>): Completed =
                match job with
                | Choice1Of2 wishesCombinedJob -> CompletedWithCombinedWishes wishesCombinedJob
                | Choice2Of2 recommendedGiftJob -> CompletedWithRecommendedGift recommendedGiftJob
```
Barry and Harry looked at each other, silently nodded, and did, without any doubt or unclear thought, what was required to be done. It was pure as a snowflake falling from the sky.
```fsharp
type Completed = CompletedWithCombinedWishes of WishesCombined | CompletedWithRecommendedGift of RecommendedGift
```
# Epilogue

The time passed like a flash and suddenly _A Type of Happiness Inc._ company became a large software firm, hiring hundreds of elvish programmers. Barry and Harry tried to incorporate the spirit of focusing on clearly expressing the domain firstly, combined with F# usage because it fitted so well.

One day, both Barry and Harry discussed some difficult part of multi-cluster, multi-agent reinforcement learning microservice-based component when suddenly Barry asked:
_- Harry, do you remember our first domain expressing session with fsharp?_
_- Of course Barry! Why do you ask?_
_- From that time our code evolved so much, but still, we're trying to use the compiler and our lovely language to document the domain, directly in the code. Although, we have some places in the codebase where we use object flavors of fsharp. Do you think the object-oriented way was wrong?_ - Barry continued.
_-Well...No. It's perfectly fine. You know why Barry?_- Harry paused for a minute to build the tension: 

 _**It's functional first, not functional only language**_