<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jakob Christensen</title>
    <description>The latest articles on DEV Community by Jakob Christensen (@t4rzsan).</description>
    <link>https://dev.to/t4rzsan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F16789%2F5a422a4d-0d37-469a-bd73-97bc9fb2278f.jpg</url>
      <title>DEV Community: Jakob Christensen</title>
      <link>https://dev.to/t4rzsan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/t4rzsan"/>
    <language>en</language>
    <item>
      <title>You owe it to your team to let them try F#</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Mon, 11 Apr 2022 21:55:00 +0000</pubDate>
      <link>https://dev.to/t4rzsan/you-owe-it-to-your-team-to-let-them-try-f-6l3</link>
      <guid>https://dev.to/t4rzsan/you-owe-it-to-your-team-to-let-them-try-f-6l3</guid>
      <description>&lt;p&gt;If you are the lead or senior of a team of developers that build applications in C#, this is for you.&lt;/p&gt;

&lt;p&gt;Let me just throw it out there for you: &lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You owe it to your team to let them try F#.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Give them time to write a new project or just a new component in F#.  They will thank you later.  And so will your management.&lt;/p&gt;

&lt;p&gt;This turned out to be a lengthy post, so here is a table of contents.&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C# feels good, but...&lt;/li&gt;
&lt;li&gt;
Fewer bugs

&lt;ul&gt;
&lt;li&gt;Make invalid state unrepresentable (and no more null-reference errors)&lt;/li&gt;
&lt;li&gt;Easier testing&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Faster time to market

&lt;ul&gt;
&lt;li&gt;Immutability&lt;/li&gt;
&lt;li&gt;Less code&lt;/li&gt;
&lt;li&gt;Better domain modelling&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
Faster onboarding of new employees
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  C# feels good, but...
&lt;/h2&gt;

&lt;p&gt;If you are like me, you have been building applications in C# for years. C# makes you comfortable.  C# makes you happy.  And your team can build just about anything in C#.&lt;/p&gt;

&lt;p&gt;But maybe, just maybe, you have a nagging feeling that some things didn't have to be so hard to understand, or requests for changes did not have to feel so overwhelming.&lt;/p&gt;

&lt;p&gt;Maybe that feeling comes after the umpteenth time your team has chased down a null reference error reported by a user.&lt;/p&gt;

&lt;p&gt;Maybe that feeling comes when the your team has spent way too much time adding a new feature, because it has some unexpected effect on the application.&lt;/p&gt;

&lt;p&gt;Or maybe that feeling comes when there is a bottle neck on your team because the steep learning curve for your code base makes it really hard to bring in new hires.&lt;/p&gt;

&lt;p&gt;As you have probably guessed by now, there is an easier way, and that way is F#.  F# will give you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fewer bugs&lt;/li&gt;
&lt;li&gt;Faster time to market&lt;/li&gt;
&lt;li&gt;Faster onboarding of team members&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I now this because we did it on our team.  Below I will walk you through these bullets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fewer bugs
&lt;/h2&gt;

&lt;p&gt;Applications without errors make happy users and if the users are happy, management and employees are happy.&lt;/p&gt;

&lt;p&gt;After working with F# for about a month, one of my co-workers, who is very experienced in C#, said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If F# compiles, it just works.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course, this is an exaggeration but there is a lot of truth in his statement.  F# helps you build robust software with no surprising runtime errors.  F# does that in a number of different ways, and I will walk you through two of those here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;F# gives you a way to make invalid state impossible, and&lt;/li&gt;
&lt;li&gt;F# makes it easier for you to test your code.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Make invalid state unrepresentable (and no more null-reference errors)
&lt;/h3&gt;

&lt;p&gt;Someone, and I'm not sure who, once coined the expression&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make invalid state unrepresentable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It means that you should model your code in a way so that parameters and values can never take on a state that is not allowed in your program.  One of the most dreaded errors in C# applications is the "object not set to a reference" error, which happens when you reference a null-object.  That is, an object is null which is an invalid value for that case.  For example, you might have some code that retrieves an object from storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// C#&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;GetUserByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"alice@acme.com"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;What you cannot tell from this short example is, what happens if there is no user for that email address?  Does &lt;code&gt;GetUserByEmail&lt;/code&gt; return null?  If it does, your code fails.  You could add a check for null but the signature for &lt;code&gt;GetUserByEmail&lt;/code&gt; does not give you any information on what to do.&lt;/p&gt;

&lt;p&gt;In F# it is to model that in a way so that it is clear what happens if the user does not exist.  In particular you could use the &lt;a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/options"&gt;&lt;code&gt;Option&lt;/code&gt;&lt;/a&gt; type and the F# code would look like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// F#&lt;/span&gt;
&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;getUserByEmail&lt;/span&gt; &lt;span class="s2"&gt;"bob@acme.com"&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"User not found."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Option&lt;/code&gt; can be either &lt;code&gt;None&lt;/code&gt; or &lt;code&gt;Some&lt;/code&gt; and the F# compiler will force you to handle both cases.  This makes it very clear what &lt;code&gt;getUserByEmail&lt;/code&gt; returns when there is no user and you won't accidentally reference a null-object. &lt;/p&gt;

&lt;p&gt;You can model your own types in the same way as &lt;code&gt;Option&lt;/code&gt;.  While we are at it we might as well expand &lt;code&gt;getUserByEmail&lt;/code&gt; so that we can identify users by other things than email, for example nick name.  For that we can create or own type:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;UserIdentifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Nick&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;getUser&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getUserByEmail&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Nick&lt;/span&gt; &lt;span class="n"&gt;nick&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;getUserByNick&lt;/span&gt; &lt;span class="n"&gt;nick&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As with &lt;code&gt;Option&lt;/code&gt;, the F# compiler will force you to match all cases of &lt;code&gt;UserIdentifier&lt;/code&gt; and hence you won't forget any cases and also, the caller of &lt;code&gt;getUser&lt;/code&gt; won't be able to pass in any invalid values.&lt;/p&gt;

&lt;p&gt;You can do even better.  Above, email is represented by a string.  But maybe you want to make sure that you only pass a validated email.  You could create a new type &lt;code&gt;ValidatedEmail&lt;/code&gt; that is guaranteed to only contain validated emails.  The &lt;code&gt;UserIdentifier&lt;/code&gt; type would then look like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;UserIdentifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nc"&gt;ValidatedEmail&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Nick&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;I will leave the implementation of &lt;code&gt;ValidatedEmail&lt;/code&gt; as an exercise for the reader 😏&lt;/p&gt;
&lt;h3&gt;
  
  
  Easier testing
&lt;/h3&gt;

&lt;p&gt;We have been taught for years to write code that adheres to principles like the Open Closed principle and the principle of Inversion of Control.  Personally, dependency injection has been my weapon of choice for so long, that I have lost sight of the why.  And testing with mocks and stubs does not exactly lend itself to readable tests.&lt;/p&gt;

&lt;p&gt;Functional programming has a different approach, since it is just... functions.  Functions are all about inputting data and getting some other data back, which you will input into another function and so on.&lt;/p&gt;

&lt;p&gt;You may visualize it like so with arrows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputData&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function1&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function2&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can think of it as data flowing through a pipeline of functions.  F# actually has a built-in "pipe" operator &lt;code&gt;|&amp;gt;&lt;/code&gt; that does exactly that.  It takes the output of one function and "pipes" it into the next:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; 
    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputData&lt;/span&gt; 
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function1&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function2&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;function3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Yes, the above it valid F# 😃&lt;/p&gt;

&lt;p&gt;You can even create new functions by composing other functions.  That lets you define workflows in your system as a pipeline composed of a series of functions.  That may look like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;doSomeBusinessLogic&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;validate&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateFoo&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateBar&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;pipeline&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;readInput&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;doSomeBusinessLogic&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;saveOutput&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here, the &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; is just some special operator that I have defined.  It composes two functions, hence &lt;code&gt;doSomeBusinessLogic&lt;/code&gt; and &lt;code&gt;pipeline&lt;/code&gt; are two functions composed from other functions.  &lt;/p&gt;

&lt;p&gt;You may want to read on one way to do that in one of my other entries here: &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/t4rzsan" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6_GLSo-R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--NgYwRfWX--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/16789/5a422a4d-0d37-469a-bd73-97bc9fb2278f.jpg" alt="t4rzsan"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/t4rzsan/impeachment-in-a-functional-way-14mj" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Impeachment in a functional way&lt;/h2&gt;
      &lt;h3&gt;Jakob Christensen ・ Jan 13 '20 ・ 4 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#functional&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#fsharp&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;The beauty here is that it is so easy to test.  &lt;/p&gt;

&lt;p&gt;You can choose to write tests for &lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;calculateFoo&lt;/code&gt;, and &lt;code&gt;calculateBar&lt;/code&gt; separately, or you can choose to test the entire &lt;code&gt;doSomeBusinessLogic&lt;/code&gt; function as a whole.  You can even go all in and just test &lt;code&gt;pipeline&lt;/code&gt;.  It is all up to you.&lt;/p&gt;

&lt;p&gt;Also, note that this model isolates your business logic from communication with external resources like web services or databases.  The boundaries towards externals are at the top and at the bottom of your pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Faster time to market
&lt;/h2&gt;

&lt;p&gt;My second point is that F# will help your team get products faster out the door.  Besides the better testing part that I went through above, I'd like to mention 3 ways that F# helps in this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutability&lt;/li&gt;
&lt;li&gt;Less code&lt;/li&gt;
&lt;li&gt;Better domain modelling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us go through these bullets:&lt;/p&gt;

&lt;h3&gt;
  
  
  Immutability
&lt;/h3&gt;

&lt;p&gt;Values in F# are immutable by default, that is you cannot change a value once it has been initialised.  For seasoned C# developers like you and I that may sound horrifying.  How can you get stuff done if you cannot change anything?  But if you think about it for at bit, immutability takes a massive burden off your shoulders because you know that you won't risk a massive rippling side effect in your application if you change a variable.&lt;/p&gt;

&lt;p&gt;That makes your code much more trustworthy and you can fearlessly add new features and fix bugs.&lt;/p&gt;

&lt;p&gt;Which gives you faster time to market...&lt;/p&gt;

&lt;h3&gt;
  
  
  Less code
&lt;/h3&gt;

&lt;p&gt;In F#, every line has a purpose.  It is very concise and succinct.  Constructs like the pipe operator &lt;code&gt;|&amp;gt;&lt;/code&gt; and &lt;a href="https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/type-inference"&gt;type inference&lt;/a&gt; saves you a lot of typing.&lt;/p&gt;

&lt;p&gt;Which gives you faster time to market...&lt;/p&gt;

&lt;p&gt;Less code also makes your code easier to understand which I will get back to.&lt;/p&gt;

&lt;h3&gt;
  
  
  Better domain modelling
&lt;/h3&gt;

&lt;p&gt;As C# developers, we have always been told to use class hierarchies to model our domain.  But if you think about it, domains really don't behave as mutable objects with some internal state and public functions that operate on that state, are they?  The usual OO example model of &lt;code&gt;Animal&lt;/code&gt; and &lt;code&gt;Monkey&lt;/code&gt; really show that this kind of modelling does not work 🐵&lt;/p&gt;

&lt;p&gt;No, if you want to model a domain with software, it is usually about modelling information (data) passing through a workflow (functions).&lt;/p&gt;

&lt;p&gt;Which is exactly what F# is very good at.&lt;/p&gt;

&lt;p&gt;Which gives you faster time to market...&lt;/p&gt;

&lt;h2&gt;
  
  
  Faster onboarding of new employees
&lt;/h2&gt;

&lt;p&gt;You finally made it to my third and last argument for switching to F#.&lt;/p&gt;

&lt;p&gt;I believe that F# is easier to learn than C#.  This is a bold statement, I know.  The thing is, C# is actually really hard to learn.  You need to learn about classes, public/protected/private/static, inheritance, constructors, interfaces, and more.  When that is done, you need to learn OOD, which is a never ending story.&lt;/p&gt;

&lt;p&gt;Of course, you can do all of this in F# as well, because it is built on .NET after all.  But you don't have to.  You can build every application in the world just by knowing &lt;em&gt;data and functions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I also claim that an F# codebase is easier to get to know for new hires.  The reason is that it has fewer abstractions.  You don't have to hunt down interface implementations that have been dependency injected via a .config file in a completely different assembly.  &lt;/p&gt;

&lt;p&gt;Granted, F# does has functional constructs, that are hard to understand, for example the &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; operator, I showed you above.  But you don't have to use stuff like that.  Actually, the F# community encourages you to use it sparingly and stick with functions, data and the &lt;code&gt;|&amp;gt;&lt;/code&gt; thing.&lt;/p&gt;

&lt;p&gt;F# is all about keeping it simple.  That is good news for juniors and new hires.&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>productivity</category>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Filtering lists in F# without throwing data away</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Fri, 21 Jan 2022 16:46:43 +0000</pubDate>
      <link>https://dev.to/t4rzsan/filtering-lists-in-f-without-throwing-data-away-a7o</link>
      <guid>https://dev.to/t4rzsan/filtering-lists-in-f-without-throwing-data-away-a7o</guid>
      <description>&lt;p&gt;A while back I got a new assignment that was a bit different from what we usually see.  We wanted to send letters to a select group of clients but we were not able to say who should get the letter.  We were only able to specify who should &lt;em&gt;not&lt;/em&gt; get the letter.&lt;/p&gt;

&lt;p&gt;So we started with a list of all clients and then we would remove people from that list in steps following a number of filter criteria.&lt;/p&gt;

&lt;p&gt;Usually for these kind of assignments you would just apply a number of suiting &lt;a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-listmodule.html#filter"&gt;&lt;code&gt;List.filter&lt;/code&gt;&lt;/a&gt; and that's it.  But not this time.&lt;/p&gt;

&lt;p&gt;What made this task a little quirky was that we were required to log for each step who we removed from the list. So we needed something different from &lt;code&gt;List.filter&lt;/code&gt;.  Instead we needed a way to filter the list while for each step logging the persons we filtered away.  It is like &lt;a href="https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-listmodule.html#partition"&gt;&lt;code&gt;List.partition&lt;/code&gt;&lt;/a&gt; on steroids.&lt;/p&gt;

&lt;p&gt;In pseudo-code the process looked like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;readListOfPersons&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filterOnCriterion1&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;writeExludedPersonsToFile&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filterOnCriterion2&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;writeExludedPersonsToFile&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;filterOnCriterion3&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;writeExludedPersonsToFile&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;writeIncludedPersonsToFile&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;I wrote a small module &lt;code&gt;Split&lt;/code&gt; that does exactly that.  I recently cleaned it up and made it public on Github &lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9-wwsHG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/t4rzsan"&gt;
        t4rzsan
      &lt;/a&gt; / &lt;a href="https://github.com/t4rzsan/fsharp-split"&gt;
        fsharp-split
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
F# Split&lt;/h1&gt;
&lt;p&gt;Split is an F# module for filtering lists but without losing the data that you filter away.  If you use F#'s built in List.filter function, you have no way of keeping track of the items in the list that you filter away.  With Split you can keep track of both the items that are included in the filter and the items that are excluded.&lt;/p&gt;
&lt;p&gt;Take a look at Demo.fsx for an example on how to use Split.  Also, check out this write-up on dev.to: &lt;a href="https://dev.to/t4rzsan/filtering-lists-in-f-without-throwing-data-away-a7o" rel="nofollow"&gt;https://dev.to/t4rzsan/filtering-lists-in-f-without-throwing-data-away-a7o&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;

  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/t4rzsan/fsharp-split"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
It is based on a simple type that holds a list for included items and a list of excluded items.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Included&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt;
      &lt;span class="nc"&gt;Excluded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;list&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You use the &lt;code&gt;Split.create&lt;/code&gt; function for splitting a list into included and excluded using a predicate function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;354&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// The result is:&lt;/span&gt;
&lt;span class="c1"&gt;// { Included = [10; 354; 234; 23; 45]&lt;/span&gt;
&lt;span class="c1"&gt;//   Excluded = [1; 2; 3] }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now rewrite the pseudo-code example above with real code from the &lt;code&gt;Split&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;finalListOfPersons&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;readListOfPersons&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="n"&gt;filterCriterion1&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="n"&gt;printer&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recreate&lt;/span&gt; &lt;span class="n"&gt;filterCriterion2&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="n"&gt;printer&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recreate&lt;/span&gt; &lt;span class="n"&gt;filterCriterion3&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="n"&gt;printer&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputIncluded&lt;/span&gt; &lt;span class="n"&gt;printer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;Split.recreate&lt;/code&gt; does a new split on the &lt;code&gt;Included&lt;/code&gt; list.&lt;/p&gt;

&lt;p&gt;The module also has a number of other functions for manipulating both the &lt;code&gt;Included&lt;/code&gt; and the &lt;code&gt;Excluded&lt;/code&gt; lists, like &lt;code&gt;Split.sort&lt;/code&gt;, &lt;code&gt;Split.map&lt;/code&gt; and &lt;code&gt;Split.filter&lt;/code&gt;.  You can also do things like appending &lt;code&gt;Excluded&lt;/code&gt; to &lt;code&gt;Included&lt;/code&gt; with &lt;code&gt;Split.merge&lt;/code&gt; and swap the two with &lt;code&gt;Split.swap&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally can get retrieve &lt;code&gt;Included&lt;/code&gt; with &lt;code&gt;Split.decompose&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is an example with numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;printer&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s2"&gt;"%s: %A"&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;finalList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;354&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;234&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printer&lt;/span&gt; &lt;span class="s2"&gt;"Less than 10"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recreate&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printer&lt;/span&gt; &lt;span class="s2"&gt;"Odd"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;swap&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;outputExcluded&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printer&lt;/span&gt; &lt;span class="s2"&gt;"Even"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Split&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decompose&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Less than 10: [1; 2; 3]
Odd: [23; 45]
Even: [10; 354; 234]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;code&gt;finalList&lt;/code&gt; ends up being &lt;code&gt;[23; 45]&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Go ahead, write your code in your native language</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Sat, 27 Nov 2021 07:53:50 +0000</pubDate>
      <link>https://dev.to/t4rzsan/go-ahead-write-your-code-in-your-native-language-1ahm</link>
      <guid>https://dev.to/t4rzsan/go-ahead-write-your-code-in-your-native-language-1ahm</guid>
      <description>&lt;p&gt;Most of us, the developers, probably write our code in English.  We name our functions, variables and types in English when we write code, and we write comments, git commit messages and pull request descriptions in English.  And it makes perfect sense since English is the number one language spoken by &lt;a href="https://en.wikipedia.org/wiki/List_of_languages_by_total_number_of_speakers"&gt;most people in the world&lt;/a&gt;.  So if we want our code to be read and understood by other people, English is a good choice.&lt;/p&gt;

&lt;p&gt;But English is not the number one language in the world when it comes to being the &lt;a href="https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers"&gt;first native language&lt;/a&gt;.  For many of us, English is a second language used only for communicating with people from other countries.  And for writing code.  For that reason, many of us learn English in school as part of our education to become citizens of the World.  &lt;/p&gt;

&lt;p&gt;I have the quite unpopular opionion that in some cases it is perfectly fine to write your code in your native language.  I'd even say that it is preferrable for some applications.  Not everybody agrees. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Denmark"&gt;Denmark&lt;/a&gt; is a small country and there are only about 5.8 million Danes on this planet.  We don't even make it on the list of top 100 most spoken languages.  Even though Danes have had quite an &lt;a href="https://en.wikipedia.org/wiki/Danelaw"&gt;influence on the English&lt;/a&gt; during the Viking Age, you should not expect a whole lot of people to understand your code if you write it in Danish.&lt;/p&gt;

&lt;p&gt;So how dare I suggest that we should write code in Danish?&lt;/p&gt;

&lt;p&gt;What I am talking about are Line of Business (LoB) applications.  That is, applications that you write mostly for internal use to solve some kind of business problem that is specific to your company.  I am not talking about libraries you publish on npm or Nuget.  &lt;/p&gt;

&lt;p&gt;I think in the long run you might be better off if you code LoBs in your native language.  You may not agree.  I know all my colleagues disagree.  We have been discussing this on my team and apparently, I am the only one who thinks it is a good idea.  But I stand my ground and let me tell you why:&lt;/p&gt;

&lt;p&gt;Everybody on the team are Danish (actually, I'd say that 98% of the entire company are Danish).  All our documents including specifications for in-house LoB applications are written in Danish.  We speak Danish with each other.  Our offices are located in Denmark.  The business terms are very specific to Denmark.  Some of us are so-so at written English.  &lt;/p&gt;

&lt;p&gt;So why on Earth would you not write code, git commit messages and pull request descriptions in Danish?&lt;/p&gt;

&lt;p&gt;When writing in your native language, there are less misunderstandings and there a is one-to-one relation between the terms used in your specifications and in your code.  There are no weird translations between business terms.&lt;/p&gt;

&lt;p&gt;The major counter-argument is, that there will be some weird mix of languages at "the boundaries" between your code and whatever framework or 3rd party libraries you use.  That is a valid argument.  However, overall I believe that the important code, that is the code that implements the difficult business rules, is easier to understand and reason about if it directly relatable to your specifications.&lt;/p&gt;

&lt;p&gt;So go ahead, write in your native language for your own good.&lt;/p&gt;

</description>
      <category>communication</category>
      <category>management</category>
      <category>discuss</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Performance for composition in F#</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Sun, 07 Nov 2021 22:51:43 +0000</pubDate>
      <link>https://dev.to/t4rzsan/performance-for-composition-in-f-3fl</link>
      <guid>https://dev.to/t4rzsan/performance-for-composition-in-f-3fl</guid>
      <description>&lt;p&gt;In a &lt;a href="https://dev.to/t4rzsan/real-life-performance-optimizations-in-f-3nep"&gt;previous post&lt;/a&gt; I talked about how we at work simplified composition in an F# project because we found that we took a performance hit when we used monadic binds in our code.&lt;/p&gt;

&lt;p&gt;I was a bit surprised how expensive the construct was so I wanted to dig deeper into it with this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;Composition in F# can take a number of forms.  Below we will take a closer look at the simple straight forward pipe (&lt;code&gt;|&amp;gt;&lt;/code&gt;) operator and compare it to the Kleisli composition and monadic bind composition operating on the &lt;a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/results" rel="noopener noreferrer"&gt;&lt;code&gt;Result&lt;/code&gt;&lt;/a&gt; type.&lt;/p&gt;

&lt;p&gt;In other words, we will compare the following operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Monadic bind&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="o"&gt;(&amp;gt;&amp;gt;=)&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; 

&lt;span class="c1"&gt;// Monadic bind inline&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="o"&gt;(&amp;gt;&amp;gt;==)&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; 

&lt;span class="c1"&gt;// Kleisli&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="o"&gt;(&amp;gt;=&amp;gt;)&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Ok&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To test we compose five different functions.  All the functions do is copy some data structure:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Property1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
      &lt;span class="nc"&gt;Property2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
      &lt;span class="nc"&gt;Property3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DateTime&lt;/span&gt; 
      &lt;span class="nc"&gt;Property4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;
      &lt;span class="nc"&gt;Property5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processA&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Property1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"some new string"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processB&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Property2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processC&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processResultA&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Property1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"some new string"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processResultB&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;Property2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;processResultC&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We use function &lt;code&gt;processData&lt;/code&gt; for testing &lt;code&gt;|&amp;gt;&lt;/code&gt; and &lt;code&gt;processDataResult&lt;/code&gt; for testing the compositions that operate on &lt;code&gt;Result&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we can create the functions we wish to time:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;withPiping&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processA&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processB&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processC&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processD&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processE&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;withBinding&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultA&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;processResultB&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;processResultC&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;processResultD&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="n"&gt;processResulte&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithoutInlining&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultA&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;processResultB&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;processResultC&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;processResultD&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;processResultE&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithInlining&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultA&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;==&lt;/span&gt; &lt;span class="n"&gt;processResultB&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;==&lt;/span&gt; &lt;span class="n"&gt;processResultC&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;==&lt;/span&gt; &lt;span class="n"&gt;processResultD&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;==&lt;/span&gt; &lt;span class="n"&gt;processResultE&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;withKleisli&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;processResultA&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultB&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultC&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultD&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;processResultE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Note that &lt;code&gt;withBinding&lt;/code&gt;, &lt;code&gt;withOperatorWithoutInlining&lt;/code&gt; and &lt;code&gt;withOperatorWithInlining&lt;/code&gt; are essentially the same but as we will see later there is a difference in performance between them.&lt;/p&gt;

&lt;p&gt;Let's also define a function for timing a number of calls to a function &lt;code&gt;f&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;timeFunction&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;sw&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Stopwatch&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Start&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;10_000_000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iter&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ignore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Stop&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;sw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nn"&gt;Elapsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TotalMilliseconds&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That way we can time 10 million calls to a function like so:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;timeFunction&lt;/span&gt; &lt;span class="n"&gt;withKleisli&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To get a smaller variance on those 10 millions calls, let us define a &lt;code&gt;run&lt;/code&gt; function that runs &lt;code&gt;timeFunction&lt;/code&gt; a number of times and prints the average:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;fName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;Property1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"some string"&lt;/span&gt;
          &lt;span class="nc"&gt;Property2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
          &lt;span class="nc"&gt;Property3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;DateTime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Today&lt;/span&gt;
          &lt;span class="nc"&gt;Property4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
          &lt;span class="nc"&gt;Property5&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;..&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;List&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;averageBy&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;timeFunction&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="n"&gt;fName&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we can write the final setup:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;withPiping&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&lt;/span&gt; &lt;span class="n"&gt;withPiping&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;withBinding&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&lt;/span&gt; &lt;span class="n"&gt;withBinding&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithInlining&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithInlining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithoutInlining&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&lt;/span&gt; &lt;span class="n"&gt;withOperatorWithoutInlining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;withKleisli&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nameof&lt;/span&gt; &lt;span class="n"&gt;withKleisli&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Granted, the setup is a bit lame but it does give some indication of differences in performance between the different types of composition.&lt;/p&gt;
&lt;h2&gt;
  
  
  The results are in
&lt;/h2&gt;

&lt;p&gt;They say that an image is worth a thousand words, so here is a graph for you.  I set &lt;code&gt;withPiping&lt;/code&gt; as index 100, so the graph shows how the other methods compare to &lt;code&gt;withPiping&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4htp8pfr36q403nmssll.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4htp8pfr36q403nmssll.jpg" alt="Graph showing relative performance for the different methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It shouldn't come as a surprise that &lt;code&gt;withPiping&lt;/code&gt; is the fastest.  &lt;/p&gt;

&lt;p&gt;I am a bit surprised that &lt;code&gt;withBinding&lt;/code&gt; is that much faster than &lt;code&gt;withOperatorWithoutInlining&lt;/code&gt;, i.e. the &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; operator.  &lt;/p&gt;

&lt;p&gt;Kleisli composition is surprisingly slow, but monadic bind is more useful in F# anyways so you would probably not use Kleisli that often.&lt;/p&gt;
&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;I put the code on Github if you want to have a look. I created it on .NET 6 RC using the preview of Visual Studio 2022.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/t4rzsan" rel="noopener noreferrer"&gt;
        t4rzsan
      &lt;/a&gt; / &lt;a href="https://github.com/t4rzsan/fsharp-performance-testing" rel="noopener noreferrer"&gt;
        fsharp-performance-testing
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>fsharp</category>
      <category>performance</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Real life performance optimizations in F#</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Wed, 20 Oct 2021 03:33:59 +0000</pubDate>
      <link>https://dev.to/t4rzsan/real-life-performance-optimizations-in-f-3nep</link>
      <guid>https://dev.to/t4rzsan/real-life-performance-optimizations-in-f-3nep</guid>
      <description>&lt;p&gt;At work we are building our first major F# application.  I work at a pension company, and the application is a simulation model to estimate our future liabilities in regards to our policy holders under different scenarios.&lt;/p&gt;

&lt;p&gt;The model will run 100 years into the future on at least 100,000 policies for perhaps 6,000 scenaries.  So the application needs to be fast!&lt;/p&gt;

&lt;p&gt;Bartosz Sypytkowski wrote a great &lt;a href="https://bartoszsypytkowski.com/writing-high-performance-f-code/" rel="noopener noreferrer"&gt;blog&lt;/a&gt; on high performance F# code and we deployed some of his advice.  Some of it worked and some of it didn't.  Remember, when optimizing for performance, you need to test thoroughly whether your changes actually work for the better.  One thing that didn't really work for us was changing our types to structs.&lt;/p&gt;

&lt;p&gt;Below is a list of what worked for us.  It might also work for you 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Single Case Discriminated Unions
&lt;/h2&gt;

&lt;p&gt;This is taken straight out of Bartosz Sypytkowski's blog.  It is quite common to wrap your simple types in Single Case Discriminated Unions in F#.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;PolicyId&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PolicyId&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This gives you nice code and better type safety but it kills performance.  Instead we settled for &lt;a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/units-of-measure" rel="noopener noreferrer"&gt;unit of measure&lt;/a&gt; and typedefs.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Measure&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;]&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;percent&lt;/span&gt;
&lt;span class="p"&gt;[&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Measure&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;]&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;kr&lt;/span&gt; &lt;span class="c1"&gt;// Danish monetary unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;percent&lt;/code&gt; unit is particularly nice as you never again have to ask how to specify it.  When you read it with a unit of measure, you intuitively know that you need to put it as &lt;code&gt;2.0&amp;lt;pct&amp;gt;&lt;/code&gt; and not &lt;code&gt;0.02&amp;lt;pct&amp;gt;&lt;/code&gt;.  You also know you need to divide by &lt;code&gt;100.0&amp;lt;pct&amp;gt;&lt;/code&gt; every time you use it, otherwise you will end up with a wrong unit.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;savings&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;kr&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;interestRate&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pct&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;interestBad&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savings&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;interestRate&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;kr*pct&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;interestGood&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savings&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;interestRate&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;pct&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;kr&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Don't use monadic binding
&lt;/h2&gt;

&lt;p&gt;Personally, I am a big fan of Kleisli composition and monadic binds.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/t4rzsan" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F16789%2F5a422a4d-0d37-469a-bd73-97bc9fb2278f.jpg" alt="t4rzsan"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/t4rzsan/impeachment-in-a-functional-way-14mj" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Impeachment in a functional way&lt;/h2&gt;
      &lt;h3&gt;Jakob Christensen ・ Jan 13 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#functional&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#fsharp&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
So naturally we started out with a workflow that looked something like this.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;workflow&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;policy&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculatePremium&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculatePayments&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateYield&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateNewSavings&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateFoo&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;calculateBar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each function had the same type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;calculateFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;InputParameters&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Policy&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Policy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;TError&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It was beautiful.  It was also slow.&lt;/p&gt;

&lt;p&gt;It turned out that almost all of the calculate functions returned &lt;code&gt;Ok&lt;/code&gt; and never &lt;code&gt;Error&lt;/code&gt;.  So we changed the whole thing to simple piping and added a validation function at the end that returns &lt;code&gt;Result&lt;/code&gt;.  This gave us much better performance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;workflow&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;policy&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculatePremium&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculatePayments&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculateYield&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculateNewSavings&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculateFoo&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;calculateBar&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
    &lt;span class="p"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;validatePolicy&lt;/span&gt; &lt;span class="n"&gt;inputParameters&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this we changed the type for each function to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;calculateFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;InputParameters&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Policy&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Policy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be fair, I later on found out that we had forgotten to add &lt;code&gt;inline&lt;/code&gt; to the &lt;code&gt;&amp;gt;&amp;gt;=&lt;/code&gt; operator and that may have had something to do with the bad performance.  Which brings me to:&lt;/p&gt;

&lt;h2&gt;
  
  
  Use inline
&lt;/h2&gt;

&lt;p&gt;Yes, don't forget to add &lt;a href="https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/inline-functions" rel="noopener noreferrer"&gt;inline&lt;/a&gt; to your generic small helper functions and operators 🙄&lt;/p&gt;

&lt;h2&gt;
  
  
  Only pass needed parameters
&lt;/h2&gt;

&lt;p&gt;As I said above, all our calculate functions had the same type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;calculateFunction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;InputParameters&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Policy&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Policy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we soon found out that not all functions actually used all of &lt;code&gt;InputParameters&lt;/code&gt;.  &lt;code&gt;InputParameters&lt;/code&gt; was quite large and that meant a performance hit for each call.  Therefore, we split &lt;code&gt;InputParameters&lt;/code&gt; into smaller logically grouped types and changed each calculate function to only take whatever it needed as parameter.&lt;/p&gt;

&lt;p&gt;That gave us a small gain on the performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memoize stuff, and consider using Dictionary instead of Map
&lt;/h2&gt;

&lt;p&gt;We added memoization where ever it made sense and it gave us a lot performance wise.  Our first try on a memoize function looked like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;memoize&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ref&lt;/span&gt; &lt;span class="nn"&gt;Map&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;
  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="nc"&gt;TryFind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
      &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Some&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;
      &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
           &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
           &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
           &lt;span class="n"&gt;res&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We found out that using the .NET type &lt;code&gt;Dictionary&amp;lt;,&amp;gt;&lt;/code&gt; gave us better performance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;memoize&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Dictionary&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;_,&lt;/span&gt; &lt;span class="o"&gt;_&amp;gt;()&lt;/span&gt;
  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
      &lt;span class="n"&gt;result&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
      &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;
      &lt;span class="n"&gt;res&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I both implementations originally came from [@dsymetweets] but I have been unable to find the links.&lt;/p&gt;

&lt;p&gt;The above memoization works for functions of one parameter only which led us to some "overloads" for functions with more than one parameter.  The following functions create memoized versions of functions with 2 or 3 parameters int that they create a tuple from the parameters and memoize based on that tuple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;memoize2&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memoize&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f'&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;memoize3&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memoize&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f'&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'd use them like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;addMemoized&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;memoize3&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addMemoized&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addMemoized&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// Result is now cached&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create extra allocations but we have not found a better way to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Map string types to integers
&lt;/h2&gt;

&lt;p&gt;In our line of business we use a lot of strings to identify business entities, like for example policy numbers and fund IDs.  This leads to a lot of comparison of strings which can hurt performance.  So we changed all IDs to integers and created dictionaries on the side to look up IDs before and after the calculations where done.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As I said, these optimizations worked for us but they may not work for you.  As we start using the model for real, we might even discover that we have to revert some of the things.  Specifically, we may one day find out that structs will work better for us.&lt;/p&gt;

&lt;p&gt;I don't recommend most of the things we did if your application is a LoB model that does not need to be very high performant.  In that case you should value beautiful code over high performance.&lt;/p&gt;

&lt;p&gt;The steps above gave us close to 90 percent improvement but it still might not be enough.  In that case we still have one more trick up our sleeves: The monster that goes by the name Mutability! :-)&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>performance</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Getting the "nuget" package manager working in Visual Studio 2019</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Tue, 01 Jun 2021 06:23:59 +0000</pubDate>
      <link>https://dev.to/t4rzsan/getting-the-nuget-package-manager-working-in-visual-studio-2019-2bjb</link>
      <guid>https://dev.to/t4rzsan/getting-the-nuget-package-manager-working-in-visual-studio-2019-2bjb</guid>
      <description>&lt;p&gt;The other day I was working with a collegue on some quick file manipulation with F# interactive (.fsx) in VSCode using the excellent Ionide extension. &lt;/p&gt;

&lt;p&gt;We ran into some trouble with Danish letters and encoding and I knew from previously that for reason Visual Studio seems to handle encoding problems in files somewhat better than VSCode.  So we decided to try VS instead.&lt;/p&gt;

&lt;p&gt;Don't worry, this post is not about encoding 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;However, we did not get very far with VS.  We used the new &lt;a href="https://docs.microsoft.com/en-us/dotnet/fsharp/tools/fsharp-interactive/#referencing-packages-in-f-interactive"&gt;F# 5.0 way of referencing Nuget packages&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="s2"&gt;"nuget: FSharp.Data"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But VS wouldn't let us have our way and it complained:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error FS3216: Package manager key 'nuget' was not registered in
...
[C:\program files (x86)\microsoft visual studio\2019\professional\common7\ide\commonextensions\microsoft\fsharp\Tools]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Intensive searching on the internet provided us with almost no help whatsoever, except for a bit help on &lt;a href="https://fsprojects.github.io/Paket/fsi-integration.html"&gt;Paket's page&lt;/a&gt; on fsi integration.  Apparently we were the only two people in the world who could not get this stuff working.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;A bit more searching also revealed that &lt;a href="https://www.nuget.org/packages/FSharp.DependencyManager.Nuget"&gt;FSharp.DependencyManager.Nuget&lt;/a&gt; is required.  It is the F# &lt;a href="https://github.com/fsharp/fslang-design/blob/dcc45b557f713a9aee75d85eae7555d41cd1cb0b/tooling/FST-1027-fsi-references.md"&gt;extension manager&lt;/a&gt; for Nuget which gives us the coolness of &lt;code&gt;#r "nuget"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That brings us to the final solution:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install FSharp.DependencyManager.Nuget with nuget.exe.&lt;/li&gt;
&lt;li&gt;Copy the file FSharp.DependencyManager.Nuget.dll to the directory mentioned in the error (in our case C:\program files (x86)\microsoft visual studio\2019\professional\common7\ide\commonextensions\microsoft\fsharp\Tools).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's it.  It took us only 2-3 hours to figure that out 🤦‍♂️🤦‍♂️.  &lt;/p&gt;

&lt;p&gt;I hope this can be helpful to the third person in the world that runs into this problem.&lt;/p&gt;

</description>
      <category>fsharp</category>
      <category>visualstudio</category>
      <category>nuget</category>
    </item>
    <item>
      <title>Impeachment in a functional way</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Mon, 13 Jan 2020 15:23:32 +0000</pubDate>
      <link>https://dev.to/t4rzsan/impeachment-in-a-functional-way-14mj</link>
      <guid>https://dev.to/t4rzsan/impeachment-in-a-functional-way-14mj</guid>
      <description>&lt;p&gt;Functional Programming (FP) did not really click with me until I saw how it utilizes composition of functions to model pipelines of tasks. Lots of sources on the internet mention immutability and algebraic types as great advantages of FP but it was composition that won me over.&lt;/p&gt;

&lt;p&gt;In particular, composition is perfect for describing workflows from the real world.&lt;/p&gt;

&lt;h2&gt;
  
  
  The impeachment process
&lt;/h2&gt;

&lt;p&gt;I came across such a real world workflow in &lt;a href="https://www.axios.com/the-process-of-impeachment-d4af789d-7166-4101-a1c8-dba6ffb8b420.html"&gt;this article&lt;/a&gt; on Axios. The process for impeachment and removal from office goes through a number of steps where each step has two possible outcomes: Either the process terminates or it continues.&lt;/p&gt;

&lt;p&gt;The steps are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Investigation in the House&lt;/li&gt;
&lt;li&gt;House vote&lt;/li&gt;
&lt;li&gt;Senate trial&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Our goal is to model the steps in a way that mimics the terminations and continuations of the process. We also want each step to report the reason for a termination.&lt;/p&gt;

&lt;h2&gt;
  
  
  The non-functional way
&lt;/h2&gt;

&lt;p&gt;In a non-functional language such as C# there are a number of ways you could model the process. One way would be to let each step throw an exception in case the process should be terminated. In that case the overall code would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RunImpeachmentProcess&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;InvestigateInHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;VoteInHouse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nf"&gt;TryInSenate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"The impeachment process stopped: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It works but it is not very elegant. Exceptions are for exceptional cases and stopping an impeachment process is not exceptional and this way is not very explicit.&lt;/p&gt;

&lt;p&gt;Another way would be to let each function return false or true indicating termination or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;TryRunImpeachmentProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;TryInvestigateInHouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;TryVoteInHouse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;TryTrialInSenate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This also works and it does not look too bad. However, if the number of steps increase above three it will end up as a mess that is hard to follow. There are other variations, for example you could mix in return statements to avoid the triangle of nested if-statement. You may also have to pass information from one step to the next as a parameter which only makes it harder and messier.&lt;/p&gt;

&lt;h2&gt;
  
  
  The functional way
&lt;/h2&gt;

&lt;p&gt;Enter composition. With composition your code could look something like this (I will be using F#):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;runImpeachmentProcess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;investigateInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;voteInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tryInSenate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is very explicit. The workflow is self-documenting and you can easily see the steps involved.&lt;/p&gt;

&lt;p&gt;Let’s see what the implementation looks like. We start with a type that holds the result for a step, whether the process continues or terminates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;ImpeachmentResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nc"&gt;ImpeachValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nc"&gt;Reason&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We could have used the built-in &lt;code&gt;Result&lt;/code&gt; type that can be either &lt;code&gt;Ok&lt;/code&gt; or &lt;code&gt;Error&lt;/code&gt;. Depending on your political standpoint, using &lt;code&gt;Error&lt;/code&gt; for ending the impeachment process may not be the correct term so I created my own &lt;code&gt;ImpeachmentResult&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;We now add the &lt;code&gt;&amp;gt;=&amp;gt;&lt;/code&gt; (fish-)operator for composing two functions that each return an &lt;code&gt;ImpeachmentResult&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="o"&gt;(&amp;gt;=&amp;gt;)&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The operator takes two functions, &lt;code&gt;a&lt;/code&gt;and &lt;code&gt;b&lt;/code&gt; with the following signatures:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ImpeachmentResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ImpeachmentResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hence, the &lt;code&gt;&amp;gt;=&amp;gt;&lt;/code&gt; unwraps the impeachment result of function &lt;code&gt;a&lt;/code&gt; and feeds it into function &lt;code&gt;b&lt;/code&gt;. The parameter &lt;code&gt;x&lt;/code&gt; can be of any type and it does not have to be the same type for each step. I.e. each step may return different results. The only requirement is that the next function takes the same type as parameter.&lt;/p&gt;

&lt;p&gt;Let’s add dummy implementations for the three steps (I’ll leave the actual implementation as an exercise for the reader). I have added a &lt;code&gt;exitAt&lt;/code&gt;parameter of type &lt;code&gt;ExitAtStep&lt;/code&gt; to each function to control which step the process should exit at. In the real world you won’t need such a parameter. However, it does show that you can pass parameters from on step to the next, all handled by the &lt;code&gt;&amp;gt;=&amp;gt;&lt;/code&gt;operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;ExitAtStep&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Investigation&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Vote&lt;/span&gt;
&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Trial&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;investigateInHouse&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Investigation&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="s2"&gt;"Investigation did not find enough evidence"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;voteInHouse&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Vote&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="s2"&gt;"Less than two-thirds voted for impeachment in House"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;tryInSenate&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Trial&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="s2"&gt;"Senate trial exonerated the President"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we get to the workflow function that is a composition of the three steps above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;runImpeachmentProcess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;investigateInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;voteInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tryInSenate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that for this example &lt;code&gt;runImpeachmentProcess&lt;/code&gt; is a function of type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;runImpeachmentProcess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;ExitAtStep&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ImpeachmentResult&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ExitAtStep&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let’s run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runImpeachmentProcess&lt;/span&gt; &lt;span class="n"&gt;exitAt&lt;/span&gt;

    &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Impeach&lt;/span&gt; &lt;span class="p"&gt;_&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s2"&gt;"Remove from office."&lt;/span&gt;
    &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;DontImpeach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Reason&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printfn&lt;/span&gt; &lt;span class="s2"&gt;"No impeachment for the following reason: %s."&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If all three steps return &lt;code&gt;Impeach&lt;/code&gt;, it will print out “Remove from office”. If at least one of the three steps return &lt;code&gt;DontImpeach&lt;/code&gt;, the code will print out the reason for the exited process with the reason for the first function that returns &lt;code&gt;DontImpeach&lt;/code&gt;. Subsequent steps will not be called if one returns &lt;code&gt;DontImpeach&lt;/code&gt;. Let’s have a look at the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nc"&gt;None&lt;/span&gt;
&lt;span class="c1"&gt;// Remove from office.&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nc"&gt;Investigation&lt;/span&gt;
&lt;span class="c1"&gt;// No impeachment for the following reason: Investigation did not find enough evidence.&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nc"&gt;Vote&lt;/span&gt;
&lt;span class="c1"&gt;// No impeachment for the following reason: Less than two-thirds voted for impeachment in House.&lt;/span&gt;

&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nc"&gt;Trial&lt;/span&gt;
&lt;span class="c1"&gt;// No impeachment for the following reason: Senate trial exonerated the President.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Composition gives us a very strong tool for creating explicit workflows for our processes. We are able to create a function composed of other functions that each represent a step in the workflow. The code for the composed function is in itself a documentation of what it does because it very clearly shows the steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;runImpeachmentProcess&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;investigateInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;voteInHouse&lt;/span&gt;
    &lt;span class="o"&gt;&amp;gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tryInSenate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I am in no way the inventor of this method. I just applied it to a world (in)famous ongoing process. Please have a look at the following sources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fsharpforfunandprofit.com/posts/recipe-part2/"&gt;F# for fun and profit: Railway oriented programming.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://fsharpforfunandprofit.com/posts/against-railway-oriented-programming/"&gt;F# for fun and profit: Against railway oriented programming&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/hmemcpy/milewski-ctfp-pdf"&gt;Category theory for programmers.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>functional</category>
      <category>fsharp</category>
    </item>
    <item>
      <title>Today, I passed a 1,000 followers on dev.to 🤗😁🐎🐴😎🍰</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Thu, 13 Jun 2019 19:18:20 +0000</pubDate>
      <link>https://dev.to/t4rzsan/today-i-passed-a-1-000-followers-on-dev-to-45gf</link>
      <guid>https://dev.to/t4rzsan/today-i-passed-a-1-000-followers-on-dev-to-45gf</guid>
      <description>&lt;p&gt;I know, I know... it is not all that impressive compared to many others here on dev.to but for me it is a small victory.  Allow me to rejoice just a little bit 🙂&lt;/p&gt;

</description>
      <category>happy</category>
    </item>
    <item>
      <title>Why you shouldn't wear headphones while working</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Fri, 07 Jun 2019 20:33:05 +0000</pubDate>
      <link>https://dev.to/t4rzsan/please-don-t-use-headphones-when-working-261g</link>
      <guid>https://dev.to/t4rzsan/please-don-t-use-headphones-when-working-261g</guid>
      <description>&lt;p&gt;There are lots of good reasons why you’d want to don headphones and listen to music while coding. They block out noise and help you concentrate. There are also a number of compelling reasons why you should not.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: Below is based on my own experience and may not be scientifically correct.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here are three reasons why you should leave those headphones off:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased sensibility to noise&lt;/strong&gt; : If you wear your noise reducing headphones all the time, eventually the noice free environment will be the new normal for you. It will be much harder for you to concentrate without your headphones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Missing out on networking&lt;/strong&gt; : At your workplace your colleagues will be talking, sharing knowledge and joking around. If you are wearing headphones you will miss out and you will create an invisible barrier between you and your peers making it harder for them to involve you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hearing loss&lt;/strong&gt; :This is the most important point. I know I sound like your father but using headphones will dramatically increase the risk of hearing loss and tinnitus and neither can be cured. When I started coding for what seems a very long time ago I would put on my headphones to block away noice and concentrate. Sadly, I now have tinnitus. It is not something that bothers me in everyday life but it gets worse when I am tired or stressed out and then it is very annoying. I know that when you are young you think that you are invincible but please be careful.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting on headphones may be a great help if you work in an open office and the noise gets overhand. But please, please please, listen to this grumpy old man, and do it as little as possible &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs.w.org%2Fimages%2Fcore%2Femoji%2F12.0.0-1%2F72x72%2F1f643.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fs.w.org%2Fimages%2Fcore%2Femoji%2F12.0.0-1%2F72x72%2F1f643.png" alt="🙃"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>programming</category>
      <category>workplace</category>
    </item>
    <item>
      <title>Do you pay for the photos you use as cover?</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Sun, 26 May 2019 19:19:26 +0000</pubDate>
      <link>https://dev.to/t4rzsan/do-you-pay-for-the-photos-you-use-as-cover-4h1p</link>
      <guid>https://dev.to/t4rzsan/do-you-pay-for-the-photos-you-use-as-cover-4h1p</guid>
      <description>&lt;p&gt;I have noticed that lots of you here on dev.to use cover photos on your articles.  Do you own or pay for those photos and do you credit the creator? (you should).&lt;/p&gt;

&lt;p&gt;What is dev.to's policy on copyrights?&lt;/p&gt;

&lt;p&gt;P.S. I only use my own photographs for my articles.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>meta</category>
    </item>
    <item>
      <title>I am old enough to remember COM</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Sun, 26 May 2019 10:26:25 +0000</pubDate>
      <link>https://dev.to/t4rzsan/i-am-old-enough-to-remember-com-5gj1</link>
      <guid>https://dev.to/t4rzsan/i-am-old-enough-to-remember-com-5gj1</guid>
      <description>&lt;p&gt;This question by &lt;a class="mentioned-user" href="https://dev.to/ben"&gt;@ben&lt;/a&gt; brought me back to the late 1990s where I first started coding professionally (yes, I am that old).&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/ben" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MJZF1ziD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--1M1qt9Sp--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/1/f451a206-11c8-4e3d-8936-143d0a7e65bb.png" alt="ben"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/ben/what-are-you-old-enough-to-remember-in-software-development-2e28" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;What are you "old enough to remember" in software development?&lt;/h2&gt;
      &lt;h3&gt;Ben Halpern ・ May 23 '19 ・ 1 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#discuss&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;I first started coding on my Sinclair ZX Spectrum by a lucky incident (I won in a drawing competition but that is for another story) but my first real job was as a very young actuary fresh out of university at a company that developed systems for pension companies.&lt;/p&gt;

&lt;p&gt;Back then the de facto way of building applications on Windows was C++ and &lt;a href="https://en.wikipedia.org/wiki/Component_Object_Model"&gt;COM&lt;/a&gt; using Microsoft Visual Studio C++ 6.0.&lt;/p&gt;

&lt;p&gt;I had no idea what I was doing when I first started and neither C++ nor COM are for the faint hearted so I was in for a steep learning curve.  COM can really be a challenge...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: Everything below is written from memory and my memory is not exactly state of the art.  Please bear with me.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;COM allows you to communicate between components that can live in the same process or in another process.  In some cases you can even use COM to communicate with components on other machines (i.e. Distributed COM).&lt;/p&gt;

&lt;p&gt;COM is based on interfaces.  If you want somebody else to call your code you have to define an interface for it (similar to interfaces in C#). In C++ you write your interfaces as abstract classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how, you may ask, do you create and object that may or may not live in another process?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is a good question.&lt;/p&gt;

&lt;p&gt;Obviously, you can't just &lt;code&gt;new&lt;/code&gt; up an object by calling its constructor.  Instead the good ol' Windows API has a special function for creating objects called &lt;a href="https://docs.microsoft.com/en-us/windows/desktop/api/combaseapi/nf-combaseapi-cocreateinstance"&gt;&lt;code&gt;CoCreateInstance&lt;/code&gt;&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;This raises a new question: How does &lt;code&gt;CoCreateInstance&lt;/code&gt; know where your component lives? This is one of the big WHY?s of COM: You need to register your COM components in the Windows Registry.  That's right!  You have to register your COM component in the Windows Registry with a unique ID (Guid) and where the dll og exe hosting your component is located. &lt;/p&gt;

&lt;p&gt;You can probably already imagine the kind of mess you can run into when trying to mess with the registry whenever you need to deploy new versions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But how, you may ask, do you clean up objects that may or may not live in another process?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is a great question.&lt;/p&gt;

&lt;p&gt;Naturally, you can't just &lt;code&gt;delete&lt;/code&gt; an object that you really don't know where lives.&lt;/p&gt;

&lt;p&gt;COM's solution to that is reference counting.  Each object keeps an internal count of the number of references that exist to the object.  Of course, C++ has no built-in ways of helping the developer with keeping track of references so there are a bunch of rules on how to be a COM developer.  The rules dictate when the developer has to manually add or subtract to the reference count.  You can do that through the &lt;a href="https://docs.microsoft.com/en-us/windows/desktop/api/unknwn/nn-unknwn-iunknown"&gt;IUnknown&lt;/a&gt; interface that every COM component must implement. &lt;/p&gt;

&lt;p&gt;If you forget to follow the rules, the memory leaks will start piling up. Here is some pseudo code on how to create a memory leak in no time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;IHorse&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;CoCreateInstance&lt;/span&gt;&lt;span class="p"&gt;(...,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;IHorse&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;CoCreateInstance&lt;/span&gt;&lt;span class="p"&gt;(...,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Oops! You forgot to release p2 before reassigning &lt;/span&gt;
&lt;span class="c1"&gt;// and to increase the ref. count for p1.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hunting down these kind of memory leaks is a real pain.  You practically have no idea where to begin.  At the company I worked for we created a special in-memory log for logging reference counting which was a great help and we would also use &lt;a href="https://docs.microsoft.com/en-us/cpp/cpp/how-to-create-and-use-ccomptr-and-ccomqiptr-instances?view=vs-2019"&gt;smart pointers&lt;/a&gt; to handle releases automatically.  But the pain though... it this makes you really appreciate the garbage collection in modern runtimes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are programming in Swift or Objective-C you may know reference counting as ARC.  ARC can get you in trouble but the runtime on Apple devices know about reference counting so it is a lot easier on the developer than COM.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;But how, you may ask, do you handle concurrency, when calling objects that may or may not live in another process?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is a really great question.&lt;/p&gt;

&lt;p&gt;Naturally, with objects living in different process you are going to have to worry about concurrency and COM tries to help you with that by putting the objects in "apartments".  &lt;/p&gt;

&lt;p&gt;Objects that are not thread safe should live in the Single Thread Apartment (STA) and objects that are thread safe may live in a Multiple Thread Apartment (MTA).  You have to decide where your object should live when registering it in the registry.&lt;/p&gt;

&lt;p&gt;When calling into another apartment your call needs to be serialized by a so-called marshaller.  Unless you are insane you want those marshallers to be created automatically which means that you can't write your interfaces in C++.  You need to define them in the &lt;a href="https://docs.microsoft.com/en-us/windows/desktop/midl/interface-definition-idl-file"&gt;Interface Definition Language (IDL)&lt;/a&gt; that will be transpiled into C++. &lt;/p&gt;

&lt;p&gt;As you may suspect by now, COM apartments may you lead you to serious self-inflicted injuries. They should come with a warning.  For example, consider the &lt;a href="https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/connection-object-ado?view=sql-server-2017"&gt;&lt;code&gt;ADODB.Connection&lt;/code&gt;&lt;/a&gt; type used for connecting to databases.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ADODB.Connection&lt;/code&gt; is an STA component, meaning that only one thread is allowed to access the object at the same time.  Not all developers were aware of this back in the 2000s so people would put the connection object in the session cache on classic ASP web sites to save time and speed up response times.  Of course, sending all requests to a web site through the same thread is usually not a good idea for your response times.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Back then many developers including Microsoft said that VB6 was the answer to all your problems with COM as VB6 would handle the reference counting for you.  That is true to some extent but you did not know the internal workings of reference counting you could very easily create memory leaks in VB6 and you would have no idea why!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;One of the gurus of COM back then, &lt;a href="https://en.wikipedia.org/wiki/Don_Box"&gt;Don Box&lt;/a&gt;, famously coined the term "COM is love" because of how COM was built on contracts.  But I can say for sure that I did not love COM all that much and I do not miss it at all. It only makes me appreciate .NET that much more.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;COM is still very much in use today: The Office applications rely heavily on COM. If you want to write a component with functions callable from within Excel cells or from VBA, you need a COM component.  Luckily, you can write it in C# and fairly easily register it as a COM component.  .NET will create as shim for you that handles the reference counting and all the other good stuff.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>com</category>
      <category>legacy</category>
      <category>dotnet</category>
      <category>windows</category>
    </item>
    <item>
      <title>Using single case union types for entity IDs in F# and making it work with Dapper</title>
      <dc:creator>Jakob Christensen</dc:creator>
      <pubDate>Sun, 12 May 2019 08:47:07 +0000</pubDate>
      <link>https://dev.to/t4rzsan/using-single-case-union-types-for-entity-ids-in-f-and-make-it-work-with-dapper-532p</link>
      <guid>https://dev.to/t4rzsan/using-single-case-union-types-for-entity-ids-in-f-and-make-it-work-with-dapper-532p</guid>
      <description>&lt;p&gt;In C# it is a common and popular pattern to use strongly typed entity IDs instead of using integers or the likes for IDs on your entities. Strongly typed entity IDs is a great help when trying to prevent you from mixing an Order ID with an OrderLine ID.&lt;/p&gt;

&lt;p&gt;Andrew Lock recently wrote a &lt;a href="https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/"&gt;series&lt;/a&gt; on the topic that you should go read now.&lt;/p&gt;

&lt;p&gt;A strongly typed entity ID may look something like this (the example used by Andrew Lock):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;OrderId&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IComparable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="n"&gt;IEquatable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;OrderId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;CompareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CompareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;ReferenceEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nf"&gt;Equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;GetHashCode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetHashCode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="p"&gt;==(&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CompareTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="p"&gt;!=(&lt;/span&gt;&lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OrderId&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;!(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a lot of code to write for every ID in your domain model and you should create a code snippet in Visual Studio to do it. However, as with many things you get this more or less for free in F# with &lt;a href="https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/"&gt;single case union types&lt;/a&gt;. All it takes is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;F# will give you equal operators and the other stuff for free, hence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;id1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;id2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;id1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id2&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As Scott Wlaschin points out on the legendary &lt;a href="https://fsharpforfunandprofit.com/posts/designing-with-types-single-case-dus/"&gt;F# for fun and profit&lt;/a&gt; you can use single case union types for anything, not just IDs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nc"&gt;Domain&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;CarMake&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CarMake&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;

    &lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Make&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CarMake&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Year&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am not saying this is always a good idea but don’t take it for any more than an example.&lt;/p&gt;

&lt;p&gt;What happens if you try to read a &lt;code&gt;Car&lt;/code&gt; from a database with Dapper?&lt;/p&gt;

&lt;h2&gt;
  
  
  Single case union types and Dapper
&lt;/h2&gt;

&lt;p&gt;Dapper uses reflection to create instances of your entity types either by calling the appropriate constructor or by setting properties. For F# record types you cannot set the properties so Dapper tries to call the constructor when querying the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="n"&gt;cn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getConnection&lt;/span&gt; &lt;span class="bp"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cars&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT CarID, Make, Year, Model FROM Car"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will fail because Dapper tries to find a constructor on &lt;code&gt;OrderLine&lt;/code&gt; with the signature &lt;code&gt;(int * string * int * string)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To help Dapper convert from &lt;code&gt;int&lt;/code&gt; to &lt;code&gt;CarID&lt;/code&gt;, from &lt;code&gt;string&lt;/code&gt; to &lt;code&gt;Make&lt;/code&gt; and so on you can register a type handler with Dapper. A type handler is a class that inherits &lt;code&gt;SqlMapper.TypeHandler&amp;lt;&amp;gt;&lt;/code&gt;. For reading &lt;code&gt;CarID&lt;/code&gt; from the database the type handler would look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;CarIDTypeHandler&lt;/span&gt;&lt;span class="bp"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;inherit&lt;/span&gt; &lt;span class="nn"&gt;SqlMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt;

        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IDbDataParameter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="bp"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; 
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Convert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToInt32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You register the type handler with Dapper with &lt;code&gt;SqlMapper.AddTypeHandler(typeof&amp;lt;CarID&amp;gt;, new CarIDTypeHandler())&lt;/code&gt;. It gets boring very quickly to write type handlers for every single single case union type you come up with (sorry about that strange sentence) and fortunately there is a way to write a generic type handler for single case union types using reflection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;SingleCaseUnionTypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;inherit&lt;/span&gt; &lt;span class="nn"&gt;SqlMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;()&lt;/span&gt;

        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IDbDataParameter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="bp"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cases&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;FSharpType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GetUnionCases&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typedefof&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;)&lt;/span&gt;
            &lt;span class="nn"&gt;FSharpValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MakeUnion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cases&lt;/span&gt;&lt;span class="o"&gt;.[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[|&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;|])&lt;/span&gt; &lt;span class="o"&gt;:?&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last line makes use of &lt;code&gt;FSharpValue.MakeUnion&lt;/code&gt; to create a union. It assumes that the union type &lt;code&gt;'T&lt;/code&gt; has one and only one case when using &lt;code&gt;cases.[0]&lt;/code&gt;. I have not found a way to restrict the generic type parameter to unions.&lt;/p&gt;

&lt;p&gt;Now you can register the type handler for all single case union types:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight fsharp"&gt;&lt;code&gt;&lt;span class="nn"&gt;SqlMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddTypeHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SingleCaseUnionTypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;())&lt;/span&gt;
&lt;span class="nn"&gt;SqlMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddTypeHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarMake&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SingleCaseUnionTypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CarMake&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;())&lt;/span&gt;
&lt;span class="nn"&gt;SqlMapper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AddTypeHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SingleCaseUnionTypeHandler&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Above we have seen how we can easily use single case union types for type safe handling of entity IDs and other values to prevent using them interchangeably. We have also created a generic Dapper type handler for single case union types.&lt;/p&gt;

&lt;p&gt;The generic type handler is only suitable for cases where you do not need extra logic or validation for creating your single union value types and you should also be careful when it comes to performance issues with the reflection in case you need to handle large amounts of instances.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>fsharp</category>
      <category>dapper</category>
    </item>
  </channel>
</rss>
